memos/web/src/i18n.ts
olavlan 8bdd1ec009
feat: add norwegian bokmål (nb) translation (#4561)
* add locale file for norwegian (nb)

* correct spelling
2025-03-27 13:14:12 +08:00

75 lines
1.3 KiB
TypeScript

import i18n, { BackendModule, FallbackLng, FallbackLngObjList } from "i18next";
import { orderBy } from "lodash-es";
import { initReactI18next } from "react-i18next";
import { findNearestMatchedLanguage } from "./utils/i18n";
export const locales = orderBy([
"ar",
"cs",
"de",
"en",
"en-GB",
"es",
"fa",
"fr",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"ka-GE",
"ko",
"mr",
"nb",
"nl",
"pl",
"pt-PT",
"pt-BR",
"ru",
"sl",
"sv",
"th",
"tr",
"uk",
"vi",
"zh-Hans",
"zh-Hant",
]);
const fallbacks = {
"zh-HK": ["zh-Hant", "en"],
"zh-TW": ["zh-Hant", "en"],
zh: ["zh-Hans", "en"],
} as FallbackLngObjList;
const LazyImportPlugin: BackendModule = {
type: "backend",
init: function () {},
read: function (language, _, callback) {
const matchedLanguage = findNearestMatchedLanguage(language);
import(`./locales/${matchedLanguage}.json`)
.then((translation: any) => {
callback(null, translation);
})
.catch(() => {
// Fallback to English.
});
},
};
i18n
.use(LazyImportPlugin)
.use(initReactI18next)
.init({
detection: {
order: ["navigator"],
},
fallbackLng: {
...fallbacks,
...{ default: ["en"] },
} as FallbackLng,
});
export default i18n;
export type TLocale = (typeof locales)[number];