feat(docker): set DEFAULT_LANG at runtime from Docker

This commit is contained in:
sheikhlimon 2025-09-16 12:54:41 +06:00
commit 0f460d81a1
3 changed files with 42 additions and 0 deletions

View file

@ -18,6 +18,18 @@ RUN sed -i 's/application\/javascript.*js;/application\/javascript
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|<head>|<head>\n <script>window.DEFAULT_LANG='${DEFAULT_LANG}';</script>|" {} \;
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;"]

View file

@ -31,3 +31,9 @@ declare module 'i18next' {
};
}
}
declare global {
interface Window {
DEFAULT_LANG?: string;
}
}

View file

@ -23,6 +23,30 @@ export type FullI18nKey = {
[K in I18nNamespaces]: `${K}:${ParseKeys<K>}`;
}[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)