From 64bd026d46f95638376e82d4c3fd54d4266cec8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Fri, 16 Feb 2024 15:46:48 +0100 Subject: [PATCH] configured localization --- app/i18n.ts | 14 ++++++++++++++ messages/en.json | 5 +++++ messages/hr.json | 5 +++++ middleware.ts | 26 +++++++++++++++++++++----- next.config.js | 9 ++++++++- 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 app/i18n.ts create mode 100644 messages/en.json create mode 100644 messages/hr.json diff --git a/app/i18n.ts b/app/i18n.ts new file mode 100644 index 0000000..58fae1f --- /dev/null +++ b/app/i18n.ts @@ -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 + }; +}); \ No newline at end of file diff --git a/messages/en.json b/messages/en.json new file mode 100644 index 0000000..874b86c --- /dev/null +++ b/messages/en.json @@ -0,0 +1,5 @@ +{ + "Index": { + "title": "Welcome!" + } + } \ No newline at end of file diff --git a/messages/hr.json b/messages/hr.json new file mode 100644 index 0000000..4fb1e97 --- /dev/null +++ b/messages/hr.json @@ -0,0 +1,5 @@ +{ + "Index": { + "title": "Dobrodošli!" + } + } \ No newline at end of file diff --git a/middleware.ts b/middleware.ts index fa7dc5d..8e5e85f 100644 --- a/middleware.ts +++ b/middleware.ts @@ -4,10 +4,26 @@ */ import { auth } from '@/app/lib/auth' +import createIntlMiddleware from 'next-intl/middleware'; + +const locales = ['en', 'de']; +const publicPages = ['/', '/login']; -export default auth; // middleware will call NextAuth's `auth` method, which will in turn call) see `auth.ts` - +const intlMiddleware = createIntlMiddleware({ + locales, + localePrefix: 'as-needed', + defaultLocale: 'hr' +}); + export const config = { - // midleware will NOT be called for paths: ['/api/auth/*', '/_next/static/*', '/_next/image*'] - matcher: ['/((?!api|policy|terms|_next/static|_next/image|.*\\.png$|.*\\.webm$).*)'], -}; \ No newline at end of file + // midleware will NOT be called for paths: '/api/auth/*', '/_next/static/*', '/_next/image*', static files and public pages + 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)); +}); diff --git a/next.config.js b/next.config.js index 17c5061..0bdc028 100644 --- a/next.config.js +++ b/next.config.js @@ -1,3 +1,5 @@ +import createNextIntlPlugin from 'next-intl/plugin'; + /** @type {import('next').NextConfig} */ const nextConfig = { // Possible options: @@ -16,4 +18,9 @@ const nextConfig = { } }; -module.exports = nextConfig; +const withNextIntl = createNextIntlPlugin(); + +const nextConfigIntl = withNextIntl(nextConfig); +export default nextConfigIntl; + +// module.exports = nextConfigIntl; \ No newline at end of file