From cba93b3d556c7a7cc0667de27066e98210a42d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:13:22 -0600 Subject: [PATCH] feat: translate funcs --- .github/workflows/ci-version.yml | 32 ---------------------- src/core/localization/strings.ts | 17 ++++++++++++ src/core/localization/translator.ts | 42 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 32 deletions(-) delete mode 100644 .github/workflows/ci-version.yml create mode 100644 src/core/localization/strings.ts create mode 100644 src/core/localization/translator.ts diff --git a/.github/workflows/ci-version.yml b/.github/workflows/ci-version.yml deleted file mode 100644 index 3e567d42d..000000000 --- a/.github/workflows/ci-version.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: CI Version - -on: push - -jobs: - host: - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Use node - uses: actions/setup-node@v3 - with: - node-version: 18 - cache: 'npm' - - - name: Install Yarn - run: npm install --location=global yarn - - - name: Use node_modules cache - uses: actions/cache@v3 - with: - path: '**/node_modules' - key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} - - - name: Install dependencies - run: yarn - - - name: Generate leaderboard - run: yarn lint:version diff --git a/src/core/localization/strings.ts b/src/core/localization/strings.ts new file mode 100644 index 000000000..f372d8a45 --- /dev/null +++ b/src/core/localization/strings.ts @@ -0,0 +1,17 @@ +const LANGUAGES = ['en-US', 'es-ES', 'ja-JP'] as const; + +export type Language = (typeof LANGUAGES)[number]; +export type TranslationFragmentTree = { + [key: string]: TranslationFragment; +} & { + $?: string; +}; +export type TranslationFragment = string | TranslationFragmentTree; + +export const translations = LANGUAGES.reduce( + (table, language) => ({ + ...table, + [language]: import(`../../lang/${language}.json`), + }), + {}, +) as Record>; diff --git a/src/core/localization/translator.ts b/src/core/localization/translator.ts new file mode 100644 index 000000000..3f3e204a4 --- /dev/null +++ b/src/core/localization/translator.ts @@ -0,0 +1,42 @@ +import { Language, translations } from './strings'; + +export async function translator(language: Language) { + const strings = await translations[language]; + + function translate(key: string) { + const path = key.split('.'); + let fragment = strings; + + for (const pathItem in path) { + if (typeof fragment === 'string') { + throw new SyntaxError( + `Attempted to access sub-string ("${pathItem}" in "${path}") from a string instead of a tree`, + ); + } else { + fragment = fragment[pathItem]; + + if (typeof fragment === 'undefined') { + throw new Error( + `Undefined translation for key "${key}" (in "${path}") in language "${language}"`, + ); + } + } + } + + if (typeof fragment === 'string') { + return fragment; + } else if (fragment.$) { + return fragment.$!; + } else { + throw new Error( + `Unresolved tree ending for path "${path}" in language "${language}"`, + ); + } + } + + function t(strings: TemplateStringsArray) { + return translate(strings[0]); + } + + return { translate, t }; +}