mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 02:37:05 +02:00
feat: update locales
This commit is contained in:
1 parent
7cd9131b60
commit
4deee2d709
7 files changed
+55
-19
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { Locale } from 'discord.js';
|
||||
import { SUPPORTED_LOCALES } from '../localization/strings/constants';
|
||||
import { SUPPORTED_LOCALES_DISCORD } from '../localization/strings/constants';
|
||||
import { translator } from '../localization/translator';
|
||||
|
||||
const validNameRegex = /^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u;
|
||||
@@ -9,7 +9,7 @@ export function localizationObject(
|
||||
literals?: string[],
|
||||
strict = false,
|
||||
) {
|
||||
return SUPPORTED_LOCALES.reduce<Partial<Record<Locale, string>>>(
|
||||
return SUPPORTED_LOCALES_DISCORD.reduce<Partial<Record<Locale, string>>>(
|
||||
(localizations, locale) => {
|
||||
const { translate } = translator(locale);
|
||||
const translation = translate(path, literals);
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
import { DEFAULT_LOCALE, SupportedLocale } from '@blitzkit/core';
|
||||
import { Locale } from 'discord.js';
|
||||
|
||||
export const SUPPORTED_LOCALES: Locale[] = [
|
||||
Locale.EnglishUS,
|
||||
Locale.SpanishES,
|
||||
Locale.Croatian,
|
||||
Locale.Russian,
|
||||
Locale.Ukrainian,
|
||||
Locale.ChineseCN,
|
||||
];
|
||||
const SUPPORTED_LOCALES_DISCORD_MAP: Record<SupportedLocale, Locale> = {
|
||||
en: Locale.EnglishUS,
|
||||
es: Locale.SpanishES,
|
||||
hr: Locale.Croatian,
|
||||
pt: Locale.PortugueseBR,
|
||||
ru: Locale.Russian,
|
||||
uk: Locale.Ukrainian,
|
||||
zh: Locale.ChineseCN,
|
||||
};
|
||||
|
||||
export const SUPPORTED_LOCALES_DISCORD_MAP_INVERSE = Object.fromEntries(
|
||||
Object.entries(SUPPORTED_LOCALES_DISCORD_MAP).map(([k, v]) => [v, k]),
|
||||
) as Record<Locale, SupportedLocale>;
|
||||
|
||||
export const SUPPORTED_LOCALES_DISCORD = Object.values(
|
||||
SUPPORTED_LOCALES_DISCORD_MAP,
|
||||
);
|
||||
|
||||
export const DEFAULT_LOCALE_DISCORD =
|
||||
SUPPORTED_LOCALES_DISCORD_MAP[DEFAULT_LOCALE];
|
||||
@@ -1,5 +1,8 @@
|
||||
import { Locale } from 'discord.js';
|
||||
import { SUPPORTED_LOCALES } from './constants';
|
||||
import {
|
||||
SUPPORTED_LOCALES_DISCORD,
|
||||
SUPPORTED_LOCALES_DISCORD_MAP_INVERSE,
|
||||
} from './constants';
|
||||
|
||||
export type TranslationFragmentTree = {
|
||||
[key: string]: TranslationFragment;
|
||||
@@ -8,12 +11,14 @@ export type TranslationFragmentTree = {
|
||||
};
|
||||
export type TranslationFragment = string | TranslationFragmentTree;
|
||||
|
||||
export const translations = SUPPORTED_LOCALES.reduce<
|
||||
export const translations = SUPPORTED_LOCALES_DISCORD.reduce<
|
||||
Partial<Record<Locale, TranslationFragment>>
|
||||
>(
|
||||
(table, locale) => ({
|
||||
...table,
|
||||
[locale]: require(`../../../../../core/lang/${locale}.json`),
|
||||
[locale]: require(
|
||||
`../../../../../core/lang/${SUPPORTED_LOCALES_DISCORD_MAP_INVERSE[locale]}.json`,
|
||||
),
|
||||
}),
|
||||
{},
|
||||
);
|
||||
@@ -1,12 +1,16 @@
|
||||
import { Locale } from 'discord.js';
|
||||
import { translations } from './strings';
|
||||
import { SUPPORTED_LOCALES } from './strings/constants';
|
||||
import {
|
||||
DEFAULT_LOCALE_DISCORD,
|
||||
SUPPORTED_LOCALES_DISCORD,
|
||||
} from './strings/constants';
|
||||
|
||||
export function translator(localeRaw: Locale) {
|
||||
const locale =
|
||||
localeRaw !== Locale.EnglishUS && SUPPORTED_LOCALES.includes(localeRaw)
|
||||
localeRaw !== DEFAULT_LOCALE_DISCORD &&
|
||||
SUPPORTED_LOCALES_DISCORD.includes(localeRaw)
|
||||
? localeRaw
|
||||
: Locale.EnglishUS;
|
||||
: DEFAULT_LOCALE_DISCORD;
|
||||
const strings = translations[locale]!;
|
||||
|
||||
function translate(path: string, literals?: string[]): string {
|
||||
@@ -25,11 +29,11 @@ export function translator(localeRaw: Locale) {
|
||||
if (typeof fragment === 'undefined') {
|
||||
const message = `Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"`;
|
||||
|
||||
if (locale === Locale.EnglishUS) {
|
||||
if (locale === DEFAULT_LOCALE_DISCORD) {
|
||||
throw new Error(message);
|
||||
} else {
|
||||
console.warn(`${message} falling back to en-US`);
|
||||
return translator(Locale.EnglishUS).translate(path, literals);
|
||||
return translator(DEFAULT_LOCALE_DISCORD).translate(path, literals);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"include": ["src/**/*"]
|
||||
"include": ["src/**/*", "../core/src/blitzkit/supportedLocales.ts"]
|
||||
}
|
||||
@@ -46,4 +46,5 @@ export * from './votes';
|
||||
export * from './createDefaultProvisions';
|
||||
export * from './metaSortTank';
|
||||
export * from './romanize';
|
||||
export * from './supportedLocales';
|
||||
export * from './youtubers';
|
||||
@@ -0,0 +1,13 @@
|
||||
export const SUPPORTED_LOCALES = [
|
||||
'en',
|
||||
'es',
|
||||
'hr',
|
||||
'pt',
|
||||
'ru',
|
||||
'uk',
|
||||
'zh',
|
||||
] as const;
|
||||
|
||||
export const DEFAULT_LOCALE = SUPPORTED_LOCALES[0];
|
||||
|
||||
export type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
|
||||
Reference in new issue
Block a user