Skip to content

Commit

Permalink
Option to provide non translated strings in a fallback locale (#1659) (
Browse files Browse the repository at this point in the history
…#1672)

Co-authored-by: Riccardo Tempesta (aka The BigRick) <[email protected]>
  • Loading branch information
chanceaclark and thebigrick authored Nov 21, 2024
1 parent 6966f19 commit ffefc61
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 103 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-papayas-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

If a string is not provided in the selected locale, the translation system will fallback to "en" for that specific entry.
21 changes: 19 additions & 2 deletions core/i18n/request.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import deepmerge from 'deepmerge';
import { notFound } from 'next/navigation';
import { getRequestConfig } from 'next-intl/server';

import { locales } from './routing';

// The language to fall back to if the requested message string is not available.
const fallbackLocale = 'en';

export default getRequestConfig(async ({ requestLocale }) => {
const locale = await requestLocale;

if (!locale || !locales.includes(locale)) {
notFound();
}

if (locale === fallbackLocale) {
return {
locale,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
messages: (await import(`../messages/${locale}.json`)).default,
};
}

return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
messages: (await import(`../messages/${locale}.json`)).default,
locale,
messages: deepmerge(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access
(await import(`../messages/${fallbackLocale}.json`)).default,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access
(await import(`../messages/${locale}.json`)).default,
),
};
});
1 change: 1 addition & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"content-security-policy-builder": "^2.2.0",
"deepmerge": "^4.3.1",
"embla-carousel-react": "8.4.0",
"focus-trap-react": "^10.3.1",
"gql.tada": "^1.8.10",
Expand Down
106 changes: 5 additions & 101 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ffefc61

Please sign in to comment.