all repos — caroster @ 16746547e99b2cb64151c3e7e36a2b635b6dcc9d

[Octree] Group carpool to your event https://caroster.io

🐛 Apply locale redirection without URL locale
Simon Mulquin simon@octree.ch
Thu, 18 Apr 2024 11:24:15 +0000
commit

16746547e99b2cb64151c3e7e36a2b635b6dcc9d

parent

909da40dfacac3dc44510c641077653dfe6e7709

M frontend/.env.examplefrontend/.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/hooks/useShare.tsfrontend/hooks/useShare.ts

@@ -2,7 +2,7 @@ import {useTranslation} from 'react-i18next';

import {Enum_Userspermissionsuser_Lang as SupportedLocales} from '../generated/graphql'; import useToastStore from '../stores/useToastStore'; -const DEFAULT_LOCALE = process.env.DEFAULT_LOCALE || 'share'; +const FALLBACK_LANGUAGE = process.env.FALLBACK_LANGUAGE || 'en'; const navigatorHasShareCapability = typeof navigator !== 'undefined' && !!navigator.share;

@@ -23,7 +23,7 @@ const localeParamIndex = splittedUrl.findIndex(

member => SupportedLocales[member] ); const urlCopy = [...splittedUrl] - urlCopy[localeParamIndex] = DEFAULT_LOCALE; + urlCopy[localeParamIndex] = FALLBACK_LANGUAGE; const withDefaultLocaleURL = urlCopy.join('/'); const isPhone = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent); // If navigator share capability and a phone
M frontend/middleware.tsfrontend/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.jsfrontend/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.jsfrontend/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'], }, };