(
key: K,
...args: GetParams<GetValue<Translations, K>> extends void
? []
: [GetParams<GetValue<Translations, K>>]
)
| 165 | * t('sessionInfo.agentState') // "Agent State" or "Состояние агента" |
| 166 | */ |
| 167 | export function t<K extends TranslationKey>( |
| 168 | key: K, |
| 169 | ...args: GetParams<GetValue<Translations, K>> extends void |
| 170 | ? [] |
| 171 | : [GetParams<GetValue<Translations, K>>] |
| 172 | ): string { |
| 173 | try { |
| 174 | // Get current language translations |
| 175 | const currentTranslations = translations[currentLanguage]; |
| 176 | |
| 177 | // Navigate to the value using dot notation |
| 178 | const keys = key.split('.'); |
| 179 | let value: any = currentTranslations; |
| 180 | |
| 181 | for (const k of keys) { |
| 182 | value = value[k]; |
| 183 | if (value === undefined) { |
| 184 | console.warn(`Translation missing: ${key}`); |
| 185 | return key; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // If it's a function, call it with the provided parameters |
| 190 | if (typeof value === 'function') { |
| 191 | const params = args[0]; |
| 192 | return value(params); |
| 193 | } |
| 194 | |
| 195 | // If it's a string constant, return it directly |
| 196 | if (typeof value === 'string') { |
| 197 | return value; |
| 198 | } |
| 199 | |
| 200 | // Fallback for unexpected types |
| 201 | console.warn(`Invalid translation value type for key: ${key}`); |
| 202 | return key; |
| 203 | } catch (error) { |
| 204 | console.error(`Translation error for key: ${key}`, error); |
| 205 | return key; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get the currently active language |
no test coverage detected