* Translate a key with optional interpolation * @param key - Translation key * @param values - Values for interpolation (optional) * @returns Translated string with interpolated values
(key: TranslationKey, values?: TranslationValues)
| 33 | * @returns Translated string with interpolated values |
| 34 | */ |
| 35 | t(key: TranslationKey, values?: TranslationValues): string { |
| 36 | const translation = |
| 37 | this.translations[this.currentLocale][ |
| 38 | key as keyof (typeof this.translations)[typeof this.currentLocale] |
| 39 | ]; |
| 40 | |
| 41 | if (!translation) { |
| 42 | // Fallback to the other locale if key not found |
| 43 | const fallbackLocale = this.currentLocale === 'zh' ? 'en' : 'zh'; |
| 44 | const fallbackTranslation = |
| 45 | this.translations[fallbackLocale][ |
| 46 | key as keyof (typeof this.translations)[typeof fallbackLocale] |
| 47 | ]; |
| 48 | |
| 49 | if (!fallbackTranslation) { |
| 50 | // If still not found, return the key itself |
| 51 | return String(key); |
| 52 | } |
| 53 | |
| 54 | return this.interpolate(fallbackTranslation, values); |
| 55 | } |
| 56 | |
| 57 | return this.interpolate(translation, values); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Interpolate values into a string template |
no test coverage detected