Skip to content

Commit

Permalink
fix: preserve casing for dynamic route parameter names
Browse files Browse the repository at this point in the history
  • Loading branch information
nbouvrette committed Oct 31, 2022
1 parent 3add5f3 commit 9f43bcd
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 120 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ To make tracking of to-dos easier, this file can be used to track progress on th
- Translation Export/import CLI
- Profiling, package size optimization (e.g. intl-messageformat strip down)
- Look to see if we can use middleware to redirect default (fake) locale: https://nextjs.org/docs/advanced-features/i18n-routing
- bug: the Babel plugin does not check if an hijack target (import) is used before injecting. This cause the import to be removed for optimization and cause a 500 error when trying to inject the non-existing import.

- Check if we can add `title` attributes on `Link` components (not supported by Next.js?) (ref: https://backlinko.com/google-ranking-factors)
- Add automated test:
- Test when a string file changes, the page is updated (developer experience?)
Expand All @@ -42,6 +42,7 @@ To make tracking of to-dos easier, this file can be used to track progress on th

### Done ✔️

- bug: the Babel plugin does not check if an hijack target (import) is used before injecting. This cause the import to be removed for optimization and cause a 500 error when trying to inject the non-existing import.
- Add support for catch-all dynamic routes
- Improve overall locales values access with new APIs
- Add proper support for localized dynamic route parameters
Expand Down
9 changes: 9 additions & 0 deletions example/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ overrides:
- field
- constructor
- method
# Filenames: Next.js pages must be `kebabCase` other than dynamic routes
- files:
- 'pages/**/*'
rules:
'unicorn/filename-case':
- error
- case: kebabCase
ignore:
- ^(.*\/)?(\[.+\])\.(jsx|tsx)$
# Filenames: components must be `PascalCase`
- files:
- 'src/components/**/*'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ export const getStaticPaths: GetStaticPaths = async (context) => {
const paths: MultilingualStaticPath[] = []
const { locales } = getStaticPathsLocales(context)
locales.forEach((locale) => {
// Create paths without parameters (this is an optional catch-all route).
paths.push({
params: {
category: [],
},
locale,
})
// Create localized paths.
const messages = getMessages(locale)
messages.getAll().forEach((firstMessages) => {
if (firstMessages.key.startsWith('categoryParameter')) {
Expand Down
34 changes: 19 additions & 15 deletions example/src/components/language-switcher/LanguageSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@ export const LanguageSwitcher: React.FC<LanguageSwitcherProps> = ({ localizedRou
<div className={isOver ? styles.over : ''}>
{locales
.filter((locale) => locale !== currentLocale)
.map((locale) => (
<Link
key={locale}
href={href}
locale={locale}
localizedRouteParameters={localizedRouteParameters}
data-cy="language-switcher-link"
onClick={() => {
setCookieLocale(locale)
}}
lang={normalizeLocale(locale)}
>
{(localeStrings as KeyValueObject)[normalizeLocale(locale)]}
</Link>
))}
.map((locale) => {
const normalizedLocale = normalizeLocale(locale)
return (
<Link
key={locale}
href={href}
locale={locale}
localizedRouteParameters={localizedRouteParameters}
data-cy="language-switcher-link"
onClick={() => {
setCookieLocale(locale)
}}
lang={normalizedLocale}
hrefLang={normalizedLocale}
>
{(localeStrings as KeyValueObject)[normalizedLocale]}
</Link>
)
})}
</div>
</div>
)
Expand Down
Loading

0 comments on commit 9f43bcd

Please sign in to comment.