Skip to content

Commit

Permalink
Fix locales matching
Browse files Browse the repository at this point in the history
  • Loading branch information
nomi-san committed Jul 9, 2023
1 parent 05281c7 commit f2f27a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
27 changes: 16 additions & 11 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { fallback, translations } from './trans.json';

type Trans = typeof translations['en'];
type TransId = keyof Trans;
type Translation = Record<string, string>;
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;
}
28 changes: 20 additions & 8 deletions src/trans.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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": "造成伤害",
Expand All @@ -43,5 +55,5 @@
"movement_speed": "移动速度",
"tenacity": "韧性"
}
}
}
]
}

0 comments on commit f2f27a9

Please sign in to comment.