Skip to content

Commit

Permalink
feat: add i18n support for client component and typesafety for i18n keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ixartz committed Dec 3, 2023
1 parent 1f43eb2 commit 2d86247
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .vscode/extensions.json
Expand Up @@ -7,6 +7,8 @@
"Orta.vscode-jest",
"humao.rest-client",
"yoavbls.pretty-ts-errors",
"ms-playwright.playwright"
"ms-playwright.playwright",
"github.vscode-github-actions",
"lokalise.i18n-ally"
]
}
4 changes: 3 additions & 1 deletion .vscode/settings.json
Expand Up @@ -23,5 +23,7 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"prettier.ignorePath": ".gitignore" // Don't run prettier for files listed in .gitignore
"prettier.ignorePath": ".gitignore", // Don't run prettier for files listed in .gitignore
"i18n-ally.localesPaths": ["messages"],
"i18n-ally.keystyle": "nested"
}
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"postbuild": "next-sitemap"
},
"dependencies": {
"@clerk/localizations": "^1.26.10",
"@clerk/nextjs": "^4.27.2",
"@hookform/resolvers": "^3.3.2",
"@libsql/client": "^0.3.6",
Expand Down
9 changes: 0 additions & 9 deletions src/app/(auth)/layout.tsx

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions src/app/[locale]/(auth)/layout.tsx
@@ -0,0 +1,18 @@
import { enUS, frFR } from '@clerk/localizations';
import { ClerkProvider } from '@clerk/nextjs';

export default function AuthLayout({
children, // will be a page or nested layout
params: { locale },
}: {
children: React.ReactNode;
params: { locale: string };
}) {
let clerkLocale = enUS;

if (locale === 'fr') {
clerkLocale = frFR;
}

return <ClerkProvider localization={clerkLocale}>{children}</ClerkProvider>;
}
File renamed without changes.
10 changes: 9 additions & 1 deletion src/app/[locale]/layout.tsx
Expand Up @@ -2,6 +2,7 @@ import '@/styles/global.css';

import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { NextIntlClientProvider, useMessages } from 'next-intl';

import { AppConfig } from '@/utils/AppConfig';

Expand Down Expand Up @@ -42,9 +43,16 @@ export default function RootLayout({
// Validate that the incoming `locale` parameter is valid
if (!AppConfig.locales.includes(locale)) notFound();

// Using internationalization in Client Components
const messages = useMessages();

return (
<html lang={locale}>
<body>{children}</body>
<body>
<NextIntlClientProvider locale={locale} messages={messages}>
{children}
</NextIntlClientProvider>
</body>
</html>
);
}
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/i18n.ts
@@ -1,5 +1,6 @@
import { getRequestConfig } from 'next-intl/server';

// Using internationalization in Server Components
export default getRequestConfig(async ({ locale }) => ({
messages: (await import(`../messages/${locale}.json`)).default,
}));
4 changes: 4 additions & 0 deletions src/types/global.d.ts
@@ -0,0 +1,4 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
// Use type safe message keys with `next-intl`
type Messages = typeof import('../../messages/en.json');
declare interface IntlMessages extends Messages {}

0 comments on commit 2d86247

Please sign in to comment.