From f2f27a9ea7c4cc091fce2fd8a480b842919bb9e1 Mon Sep 17 00:00:00 2001 From: nomi-san Date: Sun, 9 Jul 2023 19:54:55 +0700 Subject: [PATCH] Fix locales matching --- src/i18n.ts | 27 ++++++++++++++++----------- src/trans.json | 28 ++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/i18n.ts b/src/i18n.ts index 46def34..27388bf 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -1,21 +1,26 @@ import { fallback, translations } from './trans.json'; -type Trans = typeof translations['en']; -type TransId = keyof Trans; +type Translation = Record; +type TranslationKey = keyof typeof translations[0]; -let current: Trans; +let _T: Translation; -export function loadTranslation() { - let lang = document.body.dataset['lang'] as string; - if (lang === 'vn') lang = 'vi'; - if (lang === 'zh') lang = 'zh-CN'; +function findTranslation(locale: string) { + locale = locale.toLocaleLowerCase(); + for (const trans of translations) { + if (trans._locales.some(l => l.toLowerCase() === locale)) { + return trans as any; + } + } +} - // @ts-ignore - current = translations[lang] || translations[fallback]; +export function loadTranslation() { + const locale = document.body.dataset['locale'] as string; + _T = findTranslation(locale) || findTranslation(fallback); } -export function _t(id: TransId, next?: any) { - const text: string = current[id] || `{{${id}}}`; +export function _t(key: TranslationKey, next?: any) { + const text: string = _T[key] || `{{${key}}}`; if (next) return `${text} ${next}`; return text; } \ No newline at end of file diff --git a/src/trans.json b/src/trans.json index 996beae..caab3b8 100644 --- a/src/trans.json +++ b/src/trans.json @@ -1,7 +1,12 @@ { - "fallback": "en", - "translations": { - "en": { + "fallback": "en-US", + "translations": [ + { + "_locales": [ + "en-US", + "en-GB", + "en-AU" + ], "title": "BALANCE STATS", "no_stats": "No buffs/nerfs yet.", "dmg_dealt": "Damage dealt", @@ -15,9 +20,13 @@ "movement_speed": "Movement speed", "tenacity": "Tenacity" }, - "vi": { + { + "_locales": [ + "vi-VN", + "vn-VN" + ], "title": "CHỈ SỐ CÂN BẰNG", - "no_stats":"Không có tăng/giảm nào.", + "no_stats": "Không có tăng/giảm nào.", "dmg_dealt": "Sát thương gây ra", "dmg_taken": "Sát thương nhận vào", "healing": "Hồi máu", @@ -29,7 +38,10 @@ "movement_speed": "Tốc độ di chuyển", "tenacity": "Kháng hiệu ứng" }, - "zh-CN":{ + { + "_locales": [ + "zh-CN" + ], "title": "平衡数据", "no_stats": "暂无增益/减益", "dmg_dealt": "造成伤害", @@ -43,5 +55,5 @@ "movement_speed": "移动速度", "tenacity": "韧性" } - } -} + ] +} \ No newline at end of file