mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(package/locale): update for parity with lua
Support for nested locales (flattening) and getLocale function.
This commit is contained in:
@@ -1 +1,8 @@
|
|||||||
export * from './resource';
|
export * from './resource';
|
||||||
|
|
||||||
|
// https://www.raygesualdo.com/posts/flattening-object-keys-with-typescript-types/
|
||||||
|
export type FlattenObjectKeys<T extends Record<string, unknown>, Key = keyof T> = Key extends string
|
||||||
|
? T[Key] extends Record<string, unknown>
|
||||||
|
? `${Key}.${FlattenObjectKeys<T[Key]>}`
|
||||||
|
: `${Key}`
|
||||||
|
: never;
|
||||||
|
|||||||
@@ -3,6 +3,18 @@ import { printf } from 'fast-printf';
|
|||||||
|
|
||||||
const dict: Record<string, string> = {};
|
const dict: Record<string, string> = {};
|
||||||
|
|
||||||
|
function flattenDict(source: Record<string, any>, target: Record<string, string>, prefix?: string) {
|
||||||
|
for (const key in source) {
|
||||||
|
const fullKey = prefix ? `${prefix}.${key}` : key;
|
||||||
|
const value = source[key];
|
||||||
|
|
||||||
|
if (typeof value === 'object') flattenDict(value, target, fullKey);
|
||||||
|
else target[fullKey] = String(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
export const locale = (str: string, ...args: any[]) => {
|
export const locale = (str: string, ...args: any[]) => {
|
||||||
const lstr = dict[str];
|
const lstr = dict[str];
|
||||||
|
|
||||||
@@ -21,15 +33,28 @@ export const locale = (str: string, ...args: any[]) => {
|
|||||||
|
|
||||||
export const getLocales = () => dict;
|
export const getLocales = () => dict;
|
||||||
|
|
||||||
|
export function getLocale(resource: string, key: string) {
|
||||||
|
let locale = dict[key];
|
||||||
|
|
||||||
|
if (locale) console.warn(`overwritin existing locale '${key} (${locale})`);
|
||||||
|
|
||||||
|
locale = exports[resource].getLocale(key);
|
||||||
|
dict[key] = locale;
|
||||||
|
|
||||||
|
if (!locale) console.warn(`no locale exists with key '${key} in resource '${resource}`);
|
||||||
|
|
||||||
|
return locale;
|
||||||
|
}
|
||||||
|
|
||||||
export const initLocale = () => {
|
export const initLocale = () => {
|
||||||
const lang = GetConvar('ox:locale', 'en');
|
const lang = GetConvar('ox:locale', 'en');
|
||||||
let locales: unknown = JSON.parse(LoadResourceFile(cache.resource, `locales/${lang}.json`));
|
let locales: typeof dict = JSON.parse(LoadResourceFile(cache.resource, `locales/${lang}.json`));
|
||||||
|
|
||||||
if (!locales) {
|
if (!locales) {
|
||||||
console.warn(`could not load 'locales/${lang}.json'`);
|
console.warn(`could not load 'locales/${lang}.json'`);
|
||||||
|
|
||||||
if (lang !== 'en') {
|
if (lang !== 'en') {
|
||||||
locales = LoadResourceFile(cache.resource, 'locales/en.json');
|
locales = JSON.parse(LoadResourceFile(cache.resource, 'locales/en.json'));
|
||||||
|
|
||||||
if (!locales) {
|
if (!locales) {
|
||||||
console.warn(`could not load 'locales/en.json'`);
|
console.warn(`could not load 'locales/en.json'`);
|
||||||
@@ -39,7 +64,9 @@ export const initLocale = () => {
|
|||||||
if (!locales) return;
|
if (!locales) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let [k, v] of Object.entries(locales)) {
|
const flattened = flattenDict(locales, {});
|
||||||
|
|
||||||
|
for (let [k, v] of Object.entries(flattened)) {
|
||||||
if (typeof v === 'string') {
|
if (typeof v === 'string') {
|
||||||
const regExp = new RegExp(/\$\{([^}]+)\}/g);
|
const regExp = new RegExp(/\$\{([^}]+)\}/g);
|
||||||
|
|
||||||
@@ -49,7 +76,7 @@ export const initLocale = () => {
|
|||||||
for (const match of matches) {
|
for (const match of matches) {
|
||||||
if (!match) break;
|
if (!match) break;
|
||||||
const variable = match.substring(2, match.length - 1) as keyof typeof locales;
|
const variable = match.substring(2, match.length - 1) as keyof typeof locales;
|
||||||
let locale: string = locales[variable];
|
let locale: string = flattened[variable];
|
||||||
|
|
||||||
if (locale) {
|
if (locale) {
|
||||||
v = v.replace(match, locale);
|
v = v.replace(match, locale);
|
||||||
|
|||||||
Reference in New Issue
Block a user