From 7edde87d3b86d5e62e948350c020e629d8f1b661 Mon Sep 17 00:00:00 2001 From: Luke <39926192+LukeWasTakenn@users.noreply.github.com> Date: Tue, 7 Nov 2023 18:51:51 +0100 Subject: [PATCH] feat(package): locales system --- package/package.json | 1 + package/pnpm-lock.yaml | 18 +++++++ package/shared/resource/index.ts | 1 + package/shared/resource/locale/index.ts | 63 +++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 package/shared/resource/locale/index.ts diff --git a/package/package.json b/package/package.json index 626dd69..22e31f3 100644 --- a/package/package.json +++ b/package/package.json @@ -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": { diff --git a/package/pnpm-lock.yaml b/package/pnpm-lock.yaml index 135d0a8..4c2c079 100644 --- a/package/pnpm-lock.yaml +++ b/package/pnpm-lock.yaml @@ -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 diff --git a/package/shared/resource/index.ts b/package/shared/resource/index.ts index c654b73..711d413 100644 --- a/package/shared/resource/index.ts +++ b/package/shared/resource/index.ts @@ -1,2 +1,3 @@ export * from './version'; export * from './cache'; +export * from './locale' diff --git a/package/shared/resource/locale/index.ts b/package/shared/resource/locale/index.ts new file mode 100644 index 0000000..c21543a --- /dev/null +++ b/package/shared/resource/locale/index.ts @@ -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; + } +};