Skip to content

Conversation

@Scorpio3310
Copy link

@Scorpio3310 Scorpio3310 commented Aug 26, 2025

Summary

This PR addresses critical content collection schema errors and improves i18n routing functionality:

  1. Content collection configuration: Fixed schema errors by properly configuring postsGlob collection with glob loader
  2. Collection reference consistency: Resolved InvalidContentEntryFrontmatterError by fixing collection naming and references
  3. Enhanced parameter mapping: Improved switchLocalePath function for better cross-locale parameter translation
  4. Parent route lookups: Enables getLocalePath("/news") to work when only child routes like /news/[...page] exist
  5. Missing parameter handling: Prevents crashes in contexts where route parameters aren't available (e.g., head components)

Issues Fixed

  • ✅ Content collection schema errors that prevented dev server from starting
  • ✅ InvalidContentEntryFrontmatterError for French localization post references
  • ✅ Undefined collection lookup errors in content layer
  • ✅ Inconsistent collection naming between definition and export
  • ✅ Improved parameter mapping between locales in route switching
  • Parent route lookups: getLocalePath("/news") now finds child routes like/news/[...page] and returns the correct translated base path
  • Robust parameter handling: switchLocalePath no longer crashes with "Must provide [param] param" errors in head components
  • Template string compatibility: Improved handling when users accidentally use template strings instead of parameter objects

Technical Details

File: demo/src/content/config.ts

  • Added postsGlob collection with glob loader for src/data/posts directory
  • Fixed collection variable naming consistency (blogsGlobCollection → postsGlobCollection)
  • Corrected defaultLocaleVersion reference to use proper collection name

File: demo/astro.config.ts

  • Enabled experimental contentLayer feature required for glob loaders

File: demo/src/routes/news/[...page].astro

  • Updated to use postsGlob collection instead of posts
  • Added handleI18nSlug for proper slug handling
  • Improved link generation for news articles

File: package/assets/stubs/virtual.mjs

  • Enhanced parameter mapping logic in switchLocalePath
  • Added cross-locale parameter value translation
  • Improved fallback handling for missing parameter mappings
  • Enhanced getLocalePath with parent route discovery for cases where only child routes exist
  • Improved switchLocalePath parameter validation to gracefully handle missing parameters

Content Structure

Enhanced content organization with:

  • ✅ Properly configured glob-based collection for src/data/posts
  • ✅ Fixed reference relationships between translated content
  • ✅ Consistent schema validation across all content types

Use Cases

These changes enable several important scenarios:

  1. Navigation menus: getLocalePath("/blog") works even if only /blog/[slug] routes exist
  2. Head components: getSwitcherData() works reliably in <head> context without parameter errors
  3. Breadcrumbs: Can generate parent links without knowing specific dynamic parameters
  4. API consistency: Both functions now handle edge cases gracefully instead of throwing errors

…utes

Summary
This PR fixes two critical issues with dynamic route handling and language switching:
1. Dynamic route translation: Fixed parent route pattern matching for routes like /news/[...page] that weren’t being properly translated to localized paths like /fr/actualites/[...page].
2. Language switching preservation: Fixed switchLocalePath to preserve current page parameters when switching languages on paginated routes.

Issues Fixed
- Routes like /news/[...page] now correctly translate to /fr/actualites/[...page] instead of /fr/news/[...page].
- Language switching from /fr/actualites/2 now correctly goes to /it/notizie/2 instead of redirecting to home page.
- Handles complex route names with hyphens correctly (e.g., /news-copy → /actualites-copy → /notizie-copy).
- Works for both paginated routes (preserves page numbers) and non-paginated routes (preserves exact paths).

Technical Details
File: package/src/routing/register.ts
- Improved parent pattern matching logic in generateRoute function.
- Fixed edge case where segments.slice(0, i).join('/') || '/' was incorrectly falling back to root path.

File: package/assets/stubs/virtual.mjs
- Enhanced switchLocalePath function to extract current page parameters from URL.
- Added distinction between exact path matches and paginated route matches.
- Fixed parameter extraction for rest routes to prevent incorrect URL segments like /notizie/-copy.

Demo Updates and Playground
To facilitate testing, the demo has been enhanced with:
- Added new blog posts in all locales (second-post.md, deuxieme-article.md, secondo-articolo.md).
- Created /news/[...page] route with pagination functionality.
- Added route translations for /news → /actualites (FR) / /notizie (IT).
- Configured pagination with pageSize: 1 to easily test multiple pages.
- Added navigation links in header for easy testing.

Test Cases
- /fr/actualites/2 → switch to IT → /it/notizie/2.
- /fr/actualites-copy/3 → switch to IT → /it/notizie-copy/3.
- /fr/actualites-copy → switch to IT → /it/notizie-copy.
- Static routes continue to work as expected.
@netlify
Copy link

netlify bot commented Aug 26, 2025

Deploy Preview for astro-i18n ready!

Name Link
🔨 Latest commit a327d90
🔍 Latest deploy log https://app.netlify.com/projects/astro-i18n/deploys/68b2ec5f37fec40008d0cb90
😎 Deploy Preview https://deploy-preview-46--astro-i18n.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Aug 26, 2025

Deploy Preview for astro-i18n-demo ready!

Name Link
🔨 Latest commit a327d90
🔍 Latest deploy log https://app.netlify.com/projects/astro-i18n-demo/deploys/68b2ec5f6222f90008236a06
😎 Deploy Preview https://deploy-preview-46--astro-i18n-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@Scorpio3310
Copy link
Author

#2 This issue has now been resolved: the library supports rest parameter translations as well
@florian-lefebvre

Copy link
Author

@Scorpio3310 Scorpio3310 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Summary
- Fixed content collection schema errors by adding postsGlob collection with proper
  references
- mproved parameter mapping in switchLocalePath for better route translation
- Updated news pagination to use new glob-based collection

  Test plan
  - Verify dev server starts without content collection errors
  - Test news pagination pages load correctly
  - Verify language switching works on paginated routes
@Scorpio3310
Copy link
Author

@florian-lefebvre by the way, I see that you are part of the core Astro team.
Do you have any plans to support such a solution out of the box instead of through a library? that would be great!

@florian-lefebvre
Copy link
Member

Hi, thanks a lot for sending a PR! To be honest, given I do not have the capacity to maintain this package, I'm not super confident merging this because we don't have tests so I wouldn't want to break existing projects. I'll share it in the #integrations channel on Discord to see if other maintainers wanna review it too

Do you have any plans to support such a solution out of the box instead of through a library?

We talked about it in withastro/roadmap#1176 but there's no consensus yet, I'd love to see it tackled but realistically, that won't happen before a while (if it happens)

@Scorpio3310
Copy link
Author

@florian-lefebvre Yes, it would be great if someone else could test it as well. I’m already working on my own starter and have tested it on my side. I also found Advanced-Astro-i18n, which uses this library, and the recent update is already working on that project. If someone else can take a look, even better! Tnx

@florian-lefebvre
Copy link
Member

I'll take the time next week 👍, can you run the formatter? I think that would reduce the diff a little bit

@Scorpio3310
Copy link
Author

Great. Thank you Florian!

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't touched this code in a long time so I trust you! You said you tested it on the i18n starter, the demo works so I think it's fine. Just some little questions

});
}) satisfies GetStaticPaths;
const { page } = Astro.props as { page: Page; authors: any[] };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the type assertion here?

}),
});

export const collections = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me what's the difference between the 2 collections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants