(source: string)
| 9 | * @returns {Promise<string>} translated string |
| 10 | */ |
| 11 | const Translate = async (source: string): Promise<string> => { |
| 12 | // Read available language |
| 13 | if (!localesInformation?.LOCALES) { |
| 14 | localesInformation = new LocalesAPI(); |
| 15 | await localesInformation.build(); |
| 16 | } |
| 17 | const lang = (await Storage.get('preference'))?.language ?? navigator.language; |
| 18 | for (const locale of Object.values(localesInformation.AVAILABLE_LOCALES)) { |
| 19 | // Check if the inputed lang available |
| 20 | if (locale === lang) { |
| 21 | if (localesInformation.LOCALES[locale]) { |
| 22 | // Replace all text |
| 23 | Object.keys(localesInformation.LOCALES[locale]).forEach((key) => { |
| 24 | if (source === key) source = localesInformation.LOCALES[locale][key]; |
| 25 | }); |
| 26 | } |
| 27 | // Return translated text |
| 28 | return source; |
| 29 | } |
| 30 | } |
| 31 | return source; // Return translated text |
| 32 | }; |
| 33 | |
| 34 | export default Translate; |
no test coverage detected