Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Determine available languages in web app via core/i18n (#2434)

Leendert de Borst8d25948a5a54
diff --git a/apps/web/src/i18n/config.ts b/apps/web/src/i18n/config.ts
index c0c55706f..162a3c6e5 100644
--- a/apps/web/src/i18n/config.ts
+++ b/apps/web/src/i18n/config.ts
@@ -1,22 +1,10 @@
/**
- * Central configuration for i18n languages.
+ * The UI languages of the web app. The translations live in core/i18n (@aliasvault/i18n).
*/
+import { DEFAULT_LANGUAGE as CORE_DEFAULT_LANGUAGE, LANGUAGE_CODES as CORE_LANGUAGE_CODES } from '@aliasvault/i18n';
import { getLanguageInfo } from '@aliasvault/models/defaults';
-import enTranslations from './locales/en.json';
-
-/**
- * Create a map of all available languages and their resources for i18n.
- * When adding a new language, add the translation JSON file to the locales folder and add the language to the map here.
- * TODO: add all languages here once the Crowdin integration is (re)enabled for this web app.
- */
-export const LANGUAGE_RESOURCES = {
- en: {
- translation: enTranslations
- },
-};
-
/**
* A UI language.
*/
@@ -29,18 +17,17 @@ export interface ILanguageConfig {
/**
* List of all available UI languages with their code, native name and flag.
*/
-export const AVAILABLE_LANGUAGES: ILanguageConfig[] =
- (Object.keys(LANGUAGE_RESOURCES) as Array<keyof typeof LANGUAGE_RESOURCES>).map((code) => {
- const info = getLanguageInfo(code);
- return { code, nativeName: info.label, flag: info.flag };
- });
+export const AVAILABLE_LANGUAGES: ILanguageConfig[] = CORE_LANGUAGE_CODES.map((code) => {
+ const info = getLanguageInfo(code);
+ return { code, nativeName: info.label, flag: info.flag };
+});
/**
* Default language that is used when no language is set in the browser or when a localized string is not found for the current language.
*/
-export const DEFAULT_LANGUAGE = 'en';
+export const DEFAULT_LANGUAGE = CORE_DEFAULT_LANGUAGE;
-export const LANGUAGE_CODES = AVAILABLE_LANGUAGES.map(lang => lang.code);
+export const LANGUAGE_CODES: string[] = [...CORE_LANGUAGE_CODES];
/**
* Get language config by code.
diff --git a/apps/web/src/i18n/i18n.ts b/apps/web/src/i18n/i18n.ts
index 3f5efd7cd..220aac80d 100644
--- a/apps/web/src/i18n/i18n.ts
+++ b/apps/web/src/i18n/i18n.ts
@@ -1,10 +1,11 @@
+import { englishTranslations, loadTranslations, type TranslationTree } from '@aliasvault/i18n';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import { getLocalPreference, setLocalPreference } from '@/utils/LocalPreferences';
import { LocalPreferenceKeys } from '@/utils/StorageKeys';
-import { DEFAULT_LANGUAGE, LANGUAGE_CODES, LANGUAGE_RESOURCES } from './config';
+import { DEFAULT_LANGUAGE, LANGUAGE_CODES } from './config';
/**
* Detect the user's preferred language: the saved preference, else the browser language, else English.
@@ -19,12 +20,23 @@ export const detectLanguage = (): string => {
return LANGUAGE_CODES.includes(browserLang) ? browserLang : DEFAULT_LANGUAGE;
};
+/**
+ * Load the translations of a language into i18next, once. Each language is a separate chunk.
+ * @param code - the language code
+ */
+const ensureLanguageLoaded = async (code: string): Promise<void> => {
+ if (!i18n.hasResourceBundle(code, 'translation')) {
+ i18n.addResourceBundle(code, 'translation', await loadTranslations(code));
+ }
+};
+
/**
* Switch the UI language and remember the choice.
* @param code - the language code
*/
export const changeLanguage = async (code: string): Promise<void> => {
setLocalPreference(LocalPreferenceKeys.APP_LANGUAGE, code);
+ await ensureLanguageLoaded(code);
await i18n.changeLanguage(code);
};
@@ -36,11 +48,16 @@ export const initI18n = async (): Promise<void> => {
return;
}
+ const language = detectLanguage();
+ const resources: Record<string, { translation: TranslationTree }> = { [DEFAULT_LANGUAGE]: { translation: englishTranslations } };
+ if (language !== DEFAULT_LANGUAGE) {
+ resources[language] = { translation: await loadTranslations(language) };
+ }
await i18n
.use(initReactI18next)
.init({
- resources: LANGUAGE_RESOURCES,
- lng: detectLanguage(),
+ resources,
+ lng: language,
fallbackLng: DEFAULT_LANGUAGE,
debug: false,
interpolation: {
diff --git a/core/i18n/src/index.ts b/core/i18n/src/index.ts
index 374e252f9..8ccd5f563 100644
--- a/core/i18n/src/index.ts
+++ b/core/i18n/src/index.ts
@@ -18,7 +18,27 @@ export const DEFAULT_LANGUAGE = 'en';
/**
* The UI languages the apps offer. Keep in sync with the target languages in the Crowdin project settings.
*/
-export const LANGUAGE_CODES = ['en', 'da', 'de', 'es', 'fi', 'fr', 'ga', 'he', 'hu', 'id', 'it', 'nl', 'pl', 'pt', 'ro', 'ru', 'sv', 'uk', 'zh'] as const;
+export const LANGUAGE_CODES = [
+ 'en', // English
+ 'da', // Danish
+ 'de', // German
+ 'es', // Spanish
+ 'fi', // Finnish
+ 'fr', // French
+ 'ga', // Irish
+ 'he', // Hebrew
+ 'hu', // Hungarian
+ 'id', // Indonesian
+ 'it', // Italian
+ 'nl', // Dutch
+ 'pl', // Polish
+ 'pt', // Portuguese
+ 'ro', // Romanian
+ 'ru', // Russian
+ 'sv', // Swedish
+ 'uk', // Ukrainian
+ 'zh', // Chinese
+] as const;
/**
* A UI language code.