Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMB-14] feat: setup i18n #6

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"files.associations": {
"*.css": "tailwindcss"
},
"editor.formatOnSave": true
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
8 changes: 8 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import en from './messages/en.json';

type Messages = typeof en;

declare global {
// Use type safe message keys with `next-intl`
interface IntlMessages extends Messages {}
}
1 change: 1 addition & 0 deletions messages/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions messages/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 5 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import createNextIntlPlugin from 'next-intl/plugin';

const withNextIntl = createNextIntlPlugin();

/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
Expand Down Expand Up @@ -40,4 +44,4 @@ const nextConfig = {
},
};

export default nextConfig;
export default withNextIntl(nextConfig);
103 changes: 103 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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"lucide-react": "^0.395.0",
"lwk_wasm": "^0.6.3",
"next": "^14.2.4",
"next-intl": "^3.15.3",
"next-qrcode": "^2.5.1",
"next-themes": "^0.3.0",
"nostr-tools": "^2.7.0",
Expand Down
39 changes: 23 additions & 16 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import './globals.css';
import type { Metadata } from 'next';
import { Noto_Sans } from 'next/font/google';
import { cookies } from 'next/headers';
import { NextIntlClientProvider } from 'next-intl';
import { getLocale, getMessages } from 'next-intl/server';

import { Toaster } from '@/components/ui/toaster';
import { ApolloWrapper } from '@/lib/apollo/wrapper';
Expand All @@ -17,7 +19,7 @@ export const metadata: Metadata = {
description: 'Banco',
};

export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
Expand All @@ -29,24 +31,29 @@ export default function RootLayout({
const accessToken = cookieStore.get('amboss_banco_access_token')?.value;
const refreshToken = cookieStore.get('amboss_banco_refresh_token')?.value;

const locale = await getLocale();
const messages = await getMessages();

return (
<html lang="en" suppressHydrationWarning>
<html lang={locale} suppressHydrationWarning>
<body className={font.className}>
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
<ApolloWrapper
serverUrl={serverUrl}
accessToken={accessToken}
refreshToken={refreshToken}
<NextIntlClientProvider messages={messages}>
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
{children}
<Toaster />
</ApolloWrapper>
</ThemeProvider>
<ApolloWrapper
serverUrl={serverUrl}
accessToken={accessToken}
refreshToken={refreshToken}
>
{children}
<Toaster />
</ApolloWrapper>
</ThemeProvider>
</NextIntlClientProvider>
</body>
</html>
);
Expand Down
32 changes: 32 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { cookies, headers } from 'next/headers';
import { getRequestConfig } from 'next-intl/server';

type SupportedLanguage = 'en' | 'es';
const defaultLocale = 'en';

export default getRequestConfig(async () => {
let locale: SupportedLanguage;

const cookieStore = cookies();
const localeCookie = cookieStore.get('locale') as
| SupportedLanguage
| undefined;

const headersList = headers();
const localeHeader = headersList.get('Accept-Language');

if (localeCookie) {
locale = localeCookie;
} else if (localeHeader?.includes('es')) {
locale = 'es';
} else if (localeHeader?.includes('en')) {
locale = 'en';
} else {
locale = defaultLocale;
}

return {
locale,
messages: (await import(`../messages/${locale}.json`)).default,
};
});
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": [
"next-env.d.ts",
"global.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": ["node_modules"]
}
Loading