Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 5x 47x 47x 29x 47x 17x 27x 22x 5x 5x 27x 27x 17x 2x 2x 2x 19x 12x 12x 12x 5x 7x 1x 6x | import { catalog } from "./catalog";
import { defaultLocale, intlLocale, isLocale, type Locale } from "./locales";
export { intlLocale, localeNames, locales, type Locale } from "./locales";
type Params = Record<string, string | number>;
let active: Locale = defaultLocale;
function documentLocale(): Locale | null {
Eif (typeof document === "undefined") {
return null;
}
const lang = document.documentElement.lang;
return isLocale(lang) ? lang : null;
}
export function setActiveLocale(locale: Locale) {
active = locale;
}
export function getLocale(): Locale {
return documentLocale() ?? active;
}
export function getIntlLocale(): string {
return intlLocale(getLocale());
}
function interpolate(text: string, params?: Params) {
if (!params) {
return text;
}
return text.replace(/\{(\w+)\}/g, (match, name: string) =>
name in params ? String(params[name]) : match,
);
}
export function translate(source: string, locale: Locale, params?: Params): string {
const text = locale === "fr" ? source : (catalog[source]?.[locale] ?? source);
return interpolate(text, params);
}
export function t(source: string, params?: Params): string {
return translate(source, getLocale(), params);
}
export function tp(count: number, one: string, other: string, params?: Params): string {
const locale = getLocale();
const category = new Intl.PluralRules(intlLocale(locale)).select(count);
return translate(category === "one" ? one : other, locale, { count, ...params });
}
export function translated<T extends object>(value: T): T {
return new Proxy(value, {
get(target, key, receiver) {
const item: unknown = Reflect.get(target, key, receiver);
Iif (typeof key === "symbol") {
return item;
}
if (typeof item === "string") {
return t(item);
}
if (
item &&
typeof item === "object" &&
(Array.isArray(item) || Object.getPrototypeOf(item) === Object.prototype)
) {
return translated(item);
}
return item;
},
});
}
|