🐛 Apply locale redirection without URL locale
Simon Mulquin simon@octree.ch
Thu, 18 Apr 2024 11:24:15 +0000
5 files changed,
20 insertions(+),
31 deletions(-)
M
frontend/.env.example
→
frontend/.env.example
@@ -14,7 +14,8 @@
# Other configs SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js -# DEFAULT_LOCALE=share + MAPBOX_TOKEN= MAPBOX_URL=https://api.mapbox.com/ -# DEFAULT_LOCALE=share + +# FALLBACK_LANGUAGE=en
M
frontend/middleware.ts
→
frontend/middleware.ts
@@ -8,7 +8,7 @@ import {print} from 'graphql/language/printer';
import {getCookie} from './lib/cookies'; const PUBLIC_FILE = /\.(.*)$/; -const DEFAULT_LOCALE = process.env.DEFAULT_LOCALE || 'share'; +const FALLBACK_LANGUAGE = process.env.FALLBACK_LANGUAGE || 'en'; export async function middleware(req: NextRequest) { const isIgnoredPath =@@ -19,24 +19,12 @@ PUBLIC_FILE.test(req.nextUrl.pathname);
if (isIgnoredPath) return null; - const registeredUserLanguage = await getRegisteredUserLanguage(req); - - if (registeredUserLanguage && req.nextUrl.locale !== registeredUserLanguage) { - return NextResponse.redirect( - new URL( - `/${registeredUserLanguage}${req.nextUrl.pathname}${ - req.nextUrl.search || '' - }`, - req.url - ) - ); - } else if (req.nextUrl.locale === DEFAULT_LOCALE) { - const NEXT_LOCALE = getCookie('NEXT_LOCALE', req.headers.get('cookie')); - const browserPreferredSupportedLanguage = - getBrowserPreferredSupportedLanguage(req); + const locale = await getRegisteredUserLanguage(req) || + getCookie('NEXT_LOCALE', req.headers.get('cookie')) || + getBrowserPreferredSupportedLanguage(req) || + FALLBACK_LANGUAGE; - const locale = NEXT_LOCALE || browserPreferredSupportedLanguage || 'fr'; - + if (req.nextUrl.locale !== locale) { return NextResponse.redirect( new URL( `/${locale}${req.nextUrl.pathname}${req.nextUrl.search || ''}`,@@ -46,7 +34,7 @@ );
} } -const getRegisteredUserLanguage = async req => { +const getRegisteredUserLanguage = async (req) => { const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET,@@ -62,13 +50,13 @@ },
body: JSON.stringify({query: print(ProfileDocument)}), }) .then(async response => { - const {data} = await response.json(); + const {data} = await response.json(); return data?.me?.profile?.lang; }) .catch(console.error); }; -const getBrowserPreferredSupportedLanguage = req => { +const getBrowserPreferredSupportedLanguage = (req): SupportedLocales => { const browserAcceptedLanguages = req.headers .get('accept-language') ?.split(',');
M
frontend/next.config.js
→
frontend/next.config.js
@@ -3,7 +3,7 @@ const {
NODE_ENV, DEV_TILES_URL, STRAPI_URL = 'http://localhost:1337', - DEFAULT_LOCALE = 'share', + FALLBACK_LANGUAGE = 'en', MAPBOX_TOKEN, MAPBOX_URL, } = process.env;@@ -24,7 +24,7 @@ },
env: { STRAPI_URL, DEV_TILES_URL, - DEFAULT_LOCALE, + FALLBACK_LANGUAGE, MAPBOX_CONFIGURED: !!MAPBOX_TOKEN && !!MAPBOX_URL, }, i18n,
M
frontend/react-i18next.config.js
→
frontend/react-i18next.config.js
@@ -1,15 +1,15 @@
-const {DEFAULT_LOCALE = 'share'} = process.env; +const {FALLBACK_LANGUAGE = 'en'} = process.env; module.exports = { i18n: { - defaultLocale: DEFAULT_LOCALE, - locales: [DEFAULT_LOCALE, 'en', 'fr'], + defaultLocale: FALLBACK_LANGUAGE, + locales: [...new Set([FALLBACK_LANGUAGE, 'en', 'fr'])], localeDetection: false, }, trailingSlash: true, fallbackLng: { - default: ['fr'], + default: FALLBACK_LANGUAGE, 'fr-CH': ['fr'], }, };