forked from nomi-san/balance-buff-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters