Skip to content

Commit

Permalink
fix: check if translations exist before getting translation
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed May 15, 2023
1 parent 8ebc187 commit ece5257
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions assets/mods/i18n/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ export default class Translator {

private getTranslations() {
const lang = this.getLang()
return lang in this.translations ? this.translations[this.getLang()] : this.translations[this.fallback]
return this.translations[lang] ?? this.getFallbackTranslations()
}

private getFallbackTranslations() {
return this.translations[this.fallback]
}

private getFallbackTranslation(key: string) {
return this.translations[this.fallback][key] ?? ''
const translations = this.getFallbackTranslations()
return translations[key] ?? ''
}

translate(key: string, ctx?: Context, fallback = ''): string {
const translations = this.getTranslations()
const translation = translations[key] ?? this.getFallbackTranslation(key)
if (!translations) {
return fallback === '' ? key : fallback
}

const translation = translations[key] ?? this.getFallbackTranslation(key)
if (!translation) {
return fallback === '' ? key : fallback
}
Expand Down

0 comments on commit ece5257

Please sign in to comment.