mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
feat(package): locales system
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
"@nativewrappers/client": "^1.7.33",
|
||||
"@types/node": "16.9.1",
|
||||
"@types/react": "^18.0.26",
|
||||
"fast-printf": "^1.6.9",
|
||||
"typescript": "^4.9.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
18
package/pnpm-lock.yaml
generated
18
package/pnpm-lock.yaml
generated
@@ -1,5 +1,9 @@
|
||||
lockfileVersion: '6.0'
|
||||
|
||||
settings:
|
||||
autoInstallPeers: true
|
||||
excludeLinksFromLockfile: false
|
||||
|
||||
dependencies:
|
||||
'@citizenfx/client':
|
||||
specifier: 2.0.5885-1
|
||||
@@ -22,6 +26,9 @@ dependencies:
|
||||
'@types/react':
|
||||
specifier: ^18.0.26
|
||||
version: 18.0.26
|
||||
fast-printf:
|
||||
specifier: ^1.6.9
|
||||
version: 1.6.9
|
||||
typescript:
|
||||
specifier: ^4.9.4
|
||||
version: 4.9.4
|
||||
@@ -433,6 +440,10 @@ packages:
|
||||
resolve: 1.22.2
|
||||
dev: false
|
||||
|
||||
/boolean@3.2.0:
|
||||
resolution: {integrity: sha512-d0II/GO9uf9lfUHH2BQsjxzRJZBdsjgsBiW4BvhWk/3qoKwQFjIDVN19PfX8F2D/r9PCMTtLWjYVCFrpeYUzsw==}
|
||||
dev: false
|
||||
|
||||
/callsites@3.1.0:
|
||||
resolution: {integrity: sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==}
|
||||
engines: {node: '>=6'}
|
||||
@@ -505,6 +516,13 @@ packages:
|
||||
engines: {node: '>=10'}
|
||||
dev: false
|
||||
|
||||
/fast-printf@1.6.9:
|
||||
resolution: {integrity: sha512-FChq8hbz65WMj4rstcQsFB0O7Cy++nmbNfLYnD9cYv2cRn8EG6k/MGn9kO/tjO66t09DLDugj3yL+V2o6Qftrg==}
|
||||
engines: {node: '>=10.0'}
|
||||
dependencies:
|
||||
boolean: 3.2.0
|
||||
dev: false
|
||||
|
||||
/find-root@1.1.0:
|
||||
resolution: {integrity: sha512-NKfW6bec6GfKc0SGx1e07QZY9PE99u0Bft/0rzSD5k3sO/vwkVUpDUKVm5Gpp5Ue3YfShPFTX2070tDs5kB9Ng==}
|
||||
dev: false
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './version';
|
||||
export * from './cache';
|
||||
export * from './locale'
|
||||
|
||||
63
package/shared/resource/locale/index.ts
Normal file
63
package/shared/resource/locale/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { cache } from '../cache/index';
|
||||
import { printf } from 'fast-printf';
|
||||
|
||||
const dict: { [key: string]: string | number } = {};
|
||||
|
||||
export const locale = (str: keyof typeof dict, ...args: any[]) => {
|
||||
const lstr = dict[str];
|
||||
|
||||
if (!lstr) return str;
|
||||
|
||||
if (lstr) {
|
||||
if (typeof lstr !== 'string') return lstr;
|
||||
|
||||
if (args.length > 0) {
|
||||
return printf(lstr, ...args);
|
||||
}
|
||||
|
||||
return lstr;
|
||||
}
|
||||
};
|
||||
|
||||
export const getLocales = () => dict;
|
||||
|
||||
export const initLocale = () => {
|
||||
const lang = GetConvar('ox:locale', 'en');
|
||||
let locales: unknown = JSON.parse(LoadResourceFile(cache.resource, `locales/${lang}.json`));
|
||||
|
||||
if (!locales) {
|
||||
console.warn(`could not load 'locales/${lang}.json'`);
|
||||
|
||||
if (lang !== 'en') {
|
||||
locales = LoadResourceFile(cache.resource, 'locales/en.json');
|
||||
|
||||
if (!locales) {
|
||||
console.warn(`could not load 'locales/en.json'`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!locales) return;
|
||||
}
|
||||
|
||||
for (let [k, v] of Object.entries(locales)) {
|
||||
if (typeof v === 'string') {
|
||||
const regExp = new RegExp(/\$\{([^}]+)\}/g);
|
||||
|
||||
const matches = v.match(regExp);
|
||||
|
||||
if (matches) {
|
||||
for (const match of matches) {
|
||||
if (!match) break;
|
||||
const variable = match.substring(2, match.length - 1) as keyof typeof locales;
|
||||
let locale: string = locales[variable];
|
||||
|
||||
if (locale) {
|
||||
v = v.replace(match, locale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dict[k] = v;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user