lossless-cut/src/main/i18nCommon.ts

58 wiersze
1.9 KiB
TypeScript
Czysty Zwykły widok Historia

2023-01-15 10:08:38 +00:00
// intentionally disabled because I don't know the quality of the languages, so better to default to english
// const LanguageDetector = window.require('i18next-electron-language-detector');
2024-02-20 14:48:40 +00:00
// eslint-disable-next-line import/no-extraneous-dependencies
2024-03-21 15:28:25 +00:00
import { app } from 'electron';
import { join } from 'path';
import { InitOptions } from 'i18next';
2023-02-17 04:56:36 +00:00
2024-03-21 15:28:25 +00:00
let customLocalesPath: string | undefined;
export function setCustomLocalesPath(p: string) {
customLocalesPath = p;
}
2024-03-21 15:28:25 +00:00
function getLangPath(subPath: string) {
if (customLocalesPath != null) return join(customLocalesPath, subPath);
2024-03-21 15:28:25 +00:00
if (app.isPackaged) return join(process.resourcesPath, 'locales', subPath);
return join('locales', subPath);
}
// Weblate hardcodes different lang codes than electron
2021-07-23 15:26:21 +00:00
// https://www.electronjs.org/docs/api/app#appgetlocale
// https://source.chromium.org/chromium/chromium/src/+/master:ui/base/l10n/l10n_util.cc
2024-03-21 15:28:25 +00:00
const mapLang = (lng: string) => ({
nb: 'nb_NO',
no: 'nb_NO',
zh: 'zh_Hans',
2021-07-23 15:26:15 +00:00
// https://source.chromium.org/chromium/chromium/src/+/master:ui/base/l10n/l10n_util.cc;l=354
'zh-CN': 'zh_Hans', // Chinese simplified (mainland China)
'zh-TW': 'zh_Hant', // Chinese traditional (Taiwan)
fr: 'fr',
'fr-CA': 'fr',
'fr-CH': 'fr',
'fr-FR': 'fr',
it: 'it',
'it-CH': 'it',
'it-IT': 'it',
'ru-RU': 'ru',
}[lng] || lng);
2024-03-21 15:28:25 +00:00
export const fallbackLng = 'en';
2024-03-21 15:28:25 +00:00
export const commonI18nOptions: InitOptions = {
fallbackLng,
// debug: isDev,
// saveMissing: isDev,
// updateMissing: isDev,
// saveMissingTo: 'all',
2024-03-21 15:28:25 +00:00
// Keep in sync between i18next-parser.config.js and i18nCommon.js:
// TODO improve keys?
// Maybe do something like this: https://stackoverflow.com/a/19405314/6519037
keySeparator: false,
nsSeparator: false,
};
2024-03-21 15:28:25 +00:00
export const loadPath = (lng: string, ns: string) => getLangPath(`${mapLang(lng)}/${ns}.json`);
export const addPath = (lng: string, ns: string) => getLangPath(`${mapLang(lng)}/${ns}.missing.json`);