configured localization

This commit is contained in:
2024-02-16 15:46:48 +01:00
parent fdea05d4e3
commit 64bd026d46
5 changed files with 53 additions and 6 deletions

14
app/i18n.ts Normal file
View File

@@ -0,0 +1,14 @@
import {notFound} from 'next/navigation';
import {getRequestConfig} from 'next-intl/server';
// Can be imported from a shared config
const locales = ['en', 'hr'];
export default getRequestConfig(async ({locale}) => {
// Validate that the incoming `locale` parameter is valid
if (!locales.includes(locale as any)) notFound();
return {
messages: (await import(`../messages/${locale}.json`)).default
};
});

5
messages/en.json Normal file
View File

@@ -0,0 +1,5 @@
{
"Index": {
"title": "Welcome!"
}
}

5
messages/hr.json Normal file
View File

@@ -0,0 +1,5 @@
{
"Index": {
"title": "Dobrodošli!"
}
}

View File

@@ -4,10 +4,26 @@
*/ */
import { auth } from '@/app/lib/auth' import { auth } from '@/app/lib/auth'
import createIntlMiddleware from 'next-intl/middleware';
export default auth; // middleware will call NextAuth's `auth` method, which will in turn call) see `auth.ts` const locales = ['en', 'de'];
const publicPages = ['/', '/login'];
const intlMiddleware = createIntlMiddleware({
locales,
localePrefix: 'as-needed',
defaultLocale: 'hr'
});
export const config = { export const config = {
// midleware will NOT be called for paths: ['/api/auth/*', '/_next/static/*', '/_next/image*'] // midleware will NOT be called for paths: '/api/auth/*', '/_next/static/*', '/_next/image*', static files and public pages
matcher: ['/((?!api|policy|terms|_next/static|_next/image|.*\\.png$|.*\\.webm$).*)'], matcher: [
'/((?!api|_next/static|_next/image|.*\\.png$|.*\\.webm$|(en|hr)/(!?policy|terms|login)).*)'
],
}; };
// middleware will call NextAuth's `auth` method, which will in turn call) see `auth.ts`
export default auth((req) => {
// call the internalization middleware
return(intlMiddleware(req));
});

View File

@@ -1,3 +1,5 @@
import createNextIntlPlugin from 'next-intl/plugin';
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
// Possible options: // Possible options:
@@ -16,4 +18,9 @@ const nextConfig = {
} }
}; };
module.exports = nextConfig; const withNextIntl = createNextIntlPlugin();
const nextConfigIntl = withNextIntl(nextConfig);
export default nextConfigIntl;
// module.exports = nextConfigIntl;