( key: string, paramsOrLocale?: TranslationParams | Language, legacyParams?: Record<string, string | number>, )
| 164 | type TranslationParams = Record<string, string | number> & { lng?: Language }; |
| 165 | |
| 166 | export const t = ( |
| 167 | key: string, |
| 168 | paramsOrLocale?: TranslationParams | Language, |
| 169 | legacyParams?: Record<string, string | number>, |
| 170 | ): string => { |
| 171 | const params = legacyParams ?? (typeof paramsOrLocale === "string" ? undefined : paramsOrLocale); |
| 172 | const loc: Language = typeof paramsOrLocale === "string" |
| 173 | ? paramsOrLocale |
| 174 | : isLanguage(params?.lng) |
| 175 | ? params.lng |
| 176 | : locale(); |
| 177 | |
| 178 | const lookupKey = |
| 179 | typeof params?.count === "number" ? resolvePluralKey(loc, key, params.count) : key; |
| 180 | |
| 181 | const result = lookupEntry(loc, lookupKey); |
| 182 | if (result === null) return key; |
| 183 | |
| 184 | if (!params) return result; |
| 185 | |
| 186 | let out = result; |
| 187 | for (const [k, v] of Object.entries(params)) { |
| 188 | if (k === "lng") continue; |
| 189 | out = out.replace(`{${k}}`, String(v)); |
| 190 | } |
| 191 | return out; |
| 192 | }; |
| 193 | |
| 194 | /** |
| 195 | * Initialize locale from localStorage |
no test coverage detected