Skip to content

Commit

Permalink
fix: support like 'enAU' 'en_AU' locales in IntlMessageFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
apades committed Oct 14, 2024
1 parent cd3d7a2 commit a97424c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
46 changes: 46 additions & 0 deletions examples/example-app-router/messages/enAU.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Error": {
"description": "<p>We've unfortunately encountered an error.</p><p>You can try to <retry>reload the page</retry> you were visiting.</p>",
"title": "Something went wrong!"
},
"IndexPage": {
"description": "This is a basic example that demonstrates the usage of <code>next-intl</code> with the Next.js App Router. Try changing the locale in the top right corner and see how the content changes.",
"title": "next-intl example"
},
"LocaleLayout": {
"title": "next-intl example"
},
"LocaleSwitcher": {
"label": "Change language",
"locale": "{locale, select, de {🇩🇪 Deutsch} en {🇺🇸 English} other {Unknown}}"
},
"Manifest": {
"name": "next-intl example"
},
"Navigation": {
"home": "Home",
"pathnames": "Pathnames"
},
"NotFoundPage": {
"description": "Please double-check the browser address bar or use the navigation to go to a known page.",
"title": "Page not found"
},
"PageLayout": {
"links": {
"docs": {
"description": "Learn more about next-intl in the official docs.",
"href": "https://next-intl-docs.vercel.app/",
"title": "Docs"
},
"source": {
"description": "Browse the source code of this example on GitHub.",
"href": "https://github.com/amannn/next-intl/tree/main/examples/example-app-router",
"title": "Source code"
}
}
},
"PathnamesPage": {
"description": "<p>The pathnames are internationalized too.</p><p>If you're using the default language English, you'll see <code>/en/pathnames</code> in the browser address bar on this page.</p><p>If you change the locale to German, the URL is localized accordingly (<code>/de/pfadnamen</code>).</p>",
"title": "Pathnames"
}
}
46 changes: 46 additions & 0 deletions examples/example-app-router/messages/en_US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Error": {
"description": "<p>We've unfortunately encountered an error.</p><p>You can try to <retry>reload the page</retry> you were visiting.</p>",
"title": "Something went wrong!"
},
"IndexPage": {
"description": "This is a basic example that demonstrates the usage of <code>next-intl</code> with the Next.js App Router. Try changing the locale in the top right corner and see how the content changes.",
"title": "next-intl example"
},
"LocaleLayout": {
"title": "next-intl example"
},
"LocaleSwitcher": {
"label": "Change language",
"locale": "{locale, select, de {🇩🇪 Deutsch} en {🇺🇸 English} other {Unknown}}"
},
"Manifest": {
"name": "next-intl example"
},
"Navigation": {
"home": "Home",
"pathnames": "Pathnames"
},
"NotFoundPage": {
"description": "Please double-check the browser address bar or use the navigation to go to a known page.",
"title": "Page not found"
},
"PageLayout": {
"links": {
"docs": {
"description": "Learn more about next-intl in the official docs.",
"href": "https://next-intl-docs.vercel.app/",
"title": "Docs"
},
"source": {
"description": "Browse the source code of this example on GitHub.",
"href": "https://github.com/amannn/next-intl/tree/main/examples/example-app-router",
"title": "Source code"
}
}
},
"PathnamesPage": {
"description": "<p>The pathnames are internationalized too.</p><p>If you're using the default language English, you'll see <code>/en/pathnames</code> in the browser address bar on this page.</p><p>If you change the locale to German, the URL is localized accordingly (<code>/de/pfadnamen</code>).</p>",
"title": "Pathnames"
}
}
6 changes: 4 additions & 2 deletions examples/example-app-router/src/i18n/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import {createLocalizedPathnamesNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
locales: ['en', 'de'],
locales: ['en', 'de', 'enAU', 'en_US'],
defaultLocale: 'en',
pathnames: {
'/': '/',
'/pathnames': {
en: '/pathnames',
de: '/pfadnamen'
de: '/pfadnamen',
enAU: '/pathnames',
en_US: '/pathnames'
}
}
});
Expand Down
25 changes: 24 additions & 1 deletion packages/use-intl/src/core/createBaseTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ export default function createBaseTranslator<
});
}

const localeFormattedMap: Record<string,string> = {}
function arrayInsert<T>(tArr: T[], index: number, arr: T[]): T[] {
const _tArr = [...tArr];
const left = _tArr.splice(0, index),
right = _tArr;
return [...left, ...arr, ...right];
}

function formatLocale(locale: string) {
if (localeFormattedMap[locale]) return localeFormattedMap[locale];
let hasUnderline = locale.indexOf('_') !== -1;
// If locale is like 'en_US', transform to 'en-US'
locale = locale.replaceAll('_', '-');
const localeArr = [...locale];
const camelCaseSplitPos = localeArr.findIndex(v=>v.toUpperCase() === v);
if (!hasUnderline && camelCaseSplitPos !== -1) {
// If locale is like 'enUS', transform to 'en-US'
locale = arrayInsert(localeArr, camelCaseSplitPos, ['-']).join('');
localeFormattedMap[locale] = locale;
}
return locale;
}

function createBaseTranslatorImpl<
Messages extends AbstractIntlMessages,
NestedKey extends NestedKeyOf<Messages>
Expand Down Expand Up @@ -284,7 +307,7 @@ function createBaseTranslatorImpl<
try {
messageFormat = formatters.getMessageFormat(
message,
locale,
formatLocale(locale),
convertFormatsToIntlMessageFormat(
{...globalFormats, ...formats},
timeZone
Expand Down

0 comments on commit a97424c

Please sign in to comment.