diff --git a/Dockerfile b/Dockerfile index 5077cf7..e99645d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,12 +10,26 @@ RUN npm run build FROM nginx:alpine +ENV DEFAULT_LANG=en + COPY --from=build /app/dist /usr/share/nginx/html RUN sed -i 's/application\/javascript.*js;/application\/javascript js mjs;/' /etc/nginx/mime.types RUN sed -i 's|index index.html index.htm;|index index.html index.htm;\n try_files $uri $uri/ /index.html;|' /etc/nginx/conf.d/default.conf +COPY <<'EOF' /docker-entrypoint.d/30-inject-default-lang.sh +#!/bin/sh +set -e + +find /usr/share/nginx/html -name "*.html" -exec sed -i \ + "s||\n |" {} \; + +echo "Injected DEFAULT_LANG=${DEFAULT_LANG} into HTML files" +EOF + +RUN chmod +x /docker-entrypoint.d/30-inject-default-lang.sh + EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] diff --git a/src/@types/i18n.d.ts b/src/@types/i18n.d.ts index a0554fa..d5307d5 100644 --- a/src/@types/i18n.d.ts +++ b/src/@types/i18n.d.ts @@ -31,3 +31,9 @@ declare module 'i18next' { }; } } + +declare global { + interface Window { + DEFAULT_LANG?: string; + } +} diff --git a/src/i18n/index.ts b/src/i18n/index.ts index fbec4a1..91aef60 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -23,6 +23,30 @@ export type FullI18nKey = { [K in I18nNamespaces]: `${K}:${ParseKeys}`; }[I18nNamespaces]; +const currentLang = localStorage.getItem('lang'); +const userHasChosenLang = localStorage.getItem('userChosenLang'); // Flag to track if user manually selected + +if ( + window.DEFAULT_LANG && + (!currentLang || (!userHasChosenLang && currentLang === 'en')) +) { + const supportedLangs = [ + 'en', + 'de', + 'es', + 'fr', + 'pt', + 'ja', + 'hi', + 'nl', + 'ru', + 'zh' + ]; + if (supportedLangs.includes(window.DEFAULT_LANG)) { + localStorage.setItem('lang', window.DEFAULT_LANG); + } +} + i18n .use(Backend) .use(LanguageDetector)