memos/web/src/utils/i18n.ts
Lincoln Nogueira 557278fac0
feat: improve i18n support as a whole (#1526)
* feat: improve i18n support as a whole

- Remove dayjs in favor of /helpers/datetime.ts, which uses
Intl.DateTimeFormat and Date. Dayjs is not exactly i18n friendly
and has several locale related opened issues.

- Move/refactor date/time code from /helpers/utils.ts to
/helpers/datetime.ts.

- Fix Daily Review weekday not changing according to selected date.

- Localize Daily review weekday and month.

- Load i18n listed strings from /locales/{locale}.json in a dynamic way.
This makes much easier to add new locales, by just adding a properly
named json file and listing it only in /web/src/i18n.ts and
/api/user_setting.go.

- Fallback languages are now set in /web/src/i18n.ts.

- Full language codes are now preffered, but they fallback to 2-letter
codes when not available.

- The locale dropdown is now populated dynamically from the available
locales. Locale names are populated by the browser via
Intl.DisplayNames(locale).

- /web/src/i18n.ts now exports a type TLocale from availableLocales
array. This is used only by findNearestLanguageMatch(). As I was unable
to use this type in ".d.ts" files, I switched the Locale type from
/web/src/types/i18n.d.ts to string.

- Move pretty much all hardcoded text strings to i18n strings.

- Add pt-BR translation.

- Remove site.ts and move its content to a i18n string.

- Rename zh.json to zh-Hans.json to get the correct language name on
selector dropdown.

- Remove pt_BR.json and replace with pt-BR.json.

- Some minor layout spacing fixes to accommodate larger texts.

- Improve some error messages.

* Delete .yarnrc.yml

* Delete package-lock.json

* fix: 158:28  error  Insert `⏎`  prettier/prettier
2023-04-15 08:56:03 +08:00

35 lines
1.3 KiB
TypeScript

import i18n, { TLocale, availableLocales } from "@/i18n";
import { FallbackLngObjList } from "i18next";
export const findNearestLanguageMatch = (codename: string): Locale => {
// Find existing translations for full codes (e.g. "en-US", "zh-Hant")
if (codename.length > 2 && availableLocales.includes(codename as TLocale)) {
return codename as Locale;
}
// Find fallback in src/i18n.ts
const i18nfallbacks = Object.entries(i18n.store.options.fallbackLng as FallbackLngObjList);
for (const [main, fallbacks] of i18nfallbacks) {
if (codename === main) {
return fallbacks[0] as Locale;
}
}
const shortCode = codename.substring(0, 2);
// Match existing short code translation
if (availableLocales.includes(shortCode as TLocale)) {
return shortCode as Locale;
}
// Try to match "xx-YY" to existing translation for "xx-ZZ" as a last resort
// If some match is undesired, it can be overriden in src/i18n.ts `fallbacks` option
for (const existing of availableLocales) {
if (shortCode == existing.substring(0, 2)) {
return existing as Locale;
}
}
// should be "en", so the selector is not empty if there isn't a translation for current user's language
return (i18n.store.options.fallbackLng as FallbackLngObjList).default[0] as Locale;
};