feat: update locales

This commit is contained in:
tresabhi committed 2025-01-03 12:15:14 -06:00
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 { Locale } from 'discord.js';
import { SUPPORTED_LOCALES } from '../localization/strings/constants'; import { SUPPORTED_LOCALES_DISCORD } from '../localization/strings/constants';
import { translator } from '../localization/translator'; import { translator } from '../localization/translator';
const validNameRegex = /^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u; const validNameRegex = /^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u;
@@ -9,7 +9,7 @@ export function localizationObject(
literals?: string[], literals?: string[],
strict = false, strict = false,
) { ) {
return SUPPORTED_LOCALES.reduce<Partial<Record<Locale, string>>>( return SUPPORTED_LOCALES_DISCORD.reduce<Partial<Record<Locale, string>>>(
(localizations, locale) => { (localizations, locale) => {
const { translate } = translator(locale); const { translate } = translator(locale);
const translation = translate(path, literals); const translation = translate(path, literals);
@@ -1,10 +1,23 @@
import { DEFAULT_LOCALE, SupportedLocale } from '@blitzkit/core';
import { Locale } from 'discord.js'; import { Locale } from 'discord.js';
export const SUPPORTED_LOCALES: Locale[] = [ const SUPPORTED_LOCALES_DISCORD_MAP: Record<SupportedLocale, Locale> = {
Locale.EnglishUS, en: Locale.EnglishUS,
Locale.SpanishES, es: Locale.SpanishES,
Locale.Croatian, hr: Locale.Croatian,
Locale.Russian, pt: Locale.PortugueseBR,
Locale.Ukrainian, ru: Locale.Russian,
Locale.ChineseCN, 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 { Locale } from 'discord.js';
import { SUPPORTED_LOCALES } from './constants'; import {
SUPPORTED_LOCALES_DISCORD,
SUPPORTED_LOCALES_DISCORD_MAP_INVERSE,
} from './constants';
export type TranslationFragmentTree = { export type TranslationFragmentTree = {
[key: string]: TranslationFragment; [key: string]: TranslationFragment;
@@ -8,12 +11,14 @@ export type TranslationFragmentTree = {
}; };
export type TranslationFragment = string | TranslationFragmentTree; export type TranslationFragment = string | TranslationFragmentTree;
export const translations = SUPPORTED_LOCALES.reduce< export const translations = SUPPORTED_LOCALES_DISCORD.reduce<
Partial<Record<Locale, TranslationFragment>> Partial<Record<Locale, TranslationFragment>>
>( >(
(table, locale) => ({ (table, locale) => ({
...table, ...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 { Locale } from 'discord.js';
import { translations } from './strings'; 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) { export function translator(localeRaw: Locale) {
const locale = const locale =
localeRaw !== Locale.EnglishUS && SUPPORTED_LOCALES.includes(localeRaw) localeRaw !== DEFAULT_LOCALE_DISCORD &&
SUPPORTED_LOCALES_DISCORD.includes(localeRaw)
? localeRaw ? localeRaw
: Locale.EnglishUS; : DEFAULT_LOCALE_DISCORD;
const strings = translations[locale]!; const strings = translations[locale]!;
function translate(path: string, literals?: string[]): string { function translate(path: string, literals?: string[]): string {
@@ -25,11 +29,11 @@ export function translator(localeRaw: Locale) {
if (typeof fragment === 'undefined') { if (typeof fragment === 'undefined') {
const message = `Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"`; const message = `Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"`;
if (locale === Locale.EnglishUS) { if (locale === DEFAULT_LOCALE_DISCORD) {
throw new Error(message); throw new Error(message);
} else { } else {
console.warn(`${message} falling back to en-US`); console.warn(`${message} falling back to en-US`);
return translator(Locale.EnglishUS).translate(path, literals); return translator(DEFAULT_LOCALE_DISCORD).translate(path, literals);
} }
} }
} }
+1 -1
View File
@@ -1,4 +1,4 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"include": ["src/**/*"] "include": ["src/**/*", "../core/src/blitzkit/supportedLocales.ts"]
} }
+1
View File
@@ -46,4 +46,5 @@ export * from './votes';
export * from './createDefaultProvisions'; export * from './createDefaultProvisions';
export * from './metaSortTank'; export * from './metaSortTank';
export * from './romanize'; export * from './romanize';
export * from './supportedLocales';
export * from './youtubers'; 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];