From 995afaaab5d3196fdc05d5d1ac9194499ac31563 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 12 May 2025 13:22:31 +0200 Subject: [PATCH] feat(webapp): default language configurable (#8546) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * make the default and fallback language configurable * fixes --------- Co-authored-by: Wolfgang Huß --- backend/src/config/index.ts | 5 ++++ backend/src/emails/sendEmail.ts | 2 +- webapp/config/index.js | 6 +++++ webapp/plugins/i18n.js | 46 ++++++++++++++------------------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index a079c2ae5..6dfd5a808 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -124,6 +124,10 @@ const options = { CATEGORIES_ACTIVE: process.env.CATEGORIES_ACTIVE === 'true' || false, } +const language = { + LANGUAGE_DEFAULT: process.env.LANGUAGE_DEFAULT ?? 'en', +} + // Check if all required configs are present Object.entries(required).map((entry) => { if (!entry[1]) { @@ -141,6 +145,7 @@ export default { ...redis, ...s3, ...options, + ...language, } export { nodemailerTransportOptions } diff --git a/backend/src/emails/sendEmail.ts b/backend/src/emails/sendEmail.ts index c8e14d74d..6ed6d611f 100644 --- a/backend/src/emails/sendEmail.ts +++ b/backend/src/emails/sendEmail.ts @@ -39,7 +39,7 @@ const email = new Email({ transport, i18n: { locales: ['en', 'de'], - defaultLocale: 'en', + defaultLocale: CONFIG.LANGUAGE_DEFAULT, retryInDefaultLocale: false, directory: path.join(__dirname, 'locales'), updateFiles: false, diff --git a/webapp/config/index.js b/webapp/config/index.js index 074cb0736..2981c9329 100644 --- a/webapp/config/index.js +++ b/webapp/config/index.js @@ -40,11 +40,17 @@ const options = { NETWORK_NAME: process.env.NETWORK_NAME || 'Ocelot.social', } +const language = { + LANGUAGE_DEFAULT: process.env.LANGUAGE_DEFAULT || 'en', + LANGUAGE_FALLBACK: process.env.LANGUAGE_FALLBACK || 'en', +} + const CONFIG = { ...environment, ...server, ...sentry, ...options, + ...language, } // override process.env with the values here since they contain default values diff --git a/webapp/plugins/i18n.js b/webapp/plugins/i18n.js index 1382c2728..288ce2fe9 100644 --- a/webapp/plugins/i18n.js +++ b/webapp/plugins/i18n.js @@ -48,6 +48,7 @@ export default ({ app, req, cookie, store }) => { }) } + /* const user = store.getters['auth/user'] const token = store.getters['auth/token'] // persist language if it differs from last value @@ -57,54 +58,47 @@ export default ({ app, req, cookie, store }) => { // uiLanguage: localeInStore // }, { root: true }) } + */ } - // const i18nStore = new Vuex.Store({ - // strict: debug - // }) - Vue.use(vuexI18n.plugin, store, { - onTranslationNotFound: function (locale, key) { - if (debug) { + onTranslationNotFound: + debug && + function (locale, key) { /* eslint-disable-next-line no-console */ console.warn(`vuex-i18n :: Key '${key}' not found for locale '${locale}'`) - } - }, + }, }) - let userLocale = 'en' + let userLocale = app.$env.LANGUAGE_DEFAULT const localeCookie = app.$cookies.get(key) - /* const userSettings = store.getters['auth/userSettings'] - if (userSettings && userSettings.uiLanguage) { - // try to get saved user preference - userLocale = userSettings.uiLanguage - } else */ + if (!isEmpty(localeCookie)) { userLocale = localeCookie } else { try { - userLocale = process.browser - ? navigator.language || navigator.userLanguage - : req.headers['accept-language'].split(',')[0] + userLocale = ( + process.browser + ? navigator.language || navigator.userLanguage + : req.headers['accept-language'].split(',')[0] + ).substr(0, 2) } catch (err) {} - - if (userLocale && !isEmpty(userLocale.language)) { - userLocale = userLocale.language.substr(0, 2) - } } const availableLocales = locales.filter((lang) => !!lang.enabled) - const locale = find(availableLocales, ['code', userLocale]) ? userLocale : 'en' + const locale = find(availableLocales, ['code', userLocale]) + ? userLocale + : app.$env.LANGUAGE_DEFAULT - // register the fallback locales - registerTranslation({ Vue, locale: 'en' }) - if (locale !== 'en') { + // register locales + registerTranslation({ Vue, locale: app.$env.LANGUAGE_FALLBACK }) + if (locale !== app.$env.LANGUAGE_FALLBACK) { registerTranslation({ Vue, locale }) } // Set the start locale to use Vue.i18n.set(locale) - Vue.i18n.fallback('en') + Vue.i18n.fallback(app.$env.LANGUAGE_FALLBACK) if (process.browser) { store.subscribe((mutation) => {