|
| 1 | + |
| 2 | +import { callAdminForthApi } from '@/utils'; |
| 3 | + |
| 4 | + |
| 5 | +const messagesCache: Record< |
| 6 | + string, |
| 7 | + { |
| 8 | + ts: number; |
| 9 | + messages: Record<string, string>; |
| 10 | + } |
| 11 | +> = {}; |
| 12 | + |
| 13 | +// cleanup messages after a 2 minutes (cache for instant switching) |
| 14 | +setInterval(() => { |
| 15 | + const now = Date.now(); |
| 16 | + for (const lang in messagesCache) { |
| 17 | + if (now - messagesCache[lang].ts > 10 * 60 * 1000) { |
| 18 | + delete messagesCache[lang]; |
| 19 | + } |
| 20 | + } |
| 21 | +}, 60 * 1000); |
| 22 | + |
| 23 | +// i18n is vue-i18n instance |
| 24 | +export async function setLang({ setLocaleMessage, locale }: any, pluginInstanceId: string, langIso: string) { |
| 25 | + |
| 26 | + if (!messagesCache[langIso]) { |
| 27 | + const messages = await callAdminForthApi({ |
| 28 | + path: `/plugin/${pluginInstanceId}/frontend_messages?lang=${langIso}`, |
| 29 | + method: 'GET', |
| 30 | + }); |
| 31 | + messagesCache[langIso] = { |
| 32 | + ts: Date.now(), |
| 33 | + messages: messages |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + // set locale and locale message |
| 38 | + setLocaleMessage(langIso, messagesCache[langIso].messages); |
| 39 | + |
| 40 | + // set the language |
| 41 | + locale.value = langIso; |
| 42 | + |
| 43 | + document.querySelector('html').setAttribute('lang', langIso); |
| 44 | + setLocalLang(langIso); |
| 45 | +} |
| 46 | + |
| 47 | +// only remap the country code for the languages where language code is different from the country code |
| 48 | +// don't include es: es, fr: fr, etc, only include the ones where language code is different from the country code |
| 49 | +const countryISO31661ByLangISO6391 = { |
| 50 | + en: 'us', // English → United States |
| 51 | + zh: 'cn', // Chinese → China |
| 52 | + hi: 'in', // Hindi → India |
| 53 | + ar: 'sa', // Arabic → Saudi Arabia |
| 54 | + ko: 'kr', // Korean → South Korea |
| 55 | + ja: 'jp', // Japanese → Japan |
| 56 | + uk: 'ua', // Ukrainian → Ukraine |
| 57 | + ur: 'pk', // Urdu → Pakistan |
| 58 | +}; |
| 59 | + |
| 60 | +export function getCountryCodeFromLangCode(langCode) { |
| 61 | + return countryISO31661ByLangISO6391[langCode] || langCode; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +const LS_LANG_KEY = `afLanguage`; |
| 66 | + |
| 67 | +export function getLocalLang(supportedLanguages: {code}[]): string { |
| 68 | + let lsLang = localStorage.getItem(LS_LANG_KEY); |
| 69 | + // if someone screwed up the local storage or we stopped language support, lets check if it is in supported languages |
| 70 | + if (lsLang && !supportedLanguages.find((l) => l.code == lsLang)) { |
| 71 | + lsLang = null; |
| 72 | + } |
| 73 | + if (lsLang) { |
| 74 | + return lsLang; |
| 75 | + } |
| 76 | + // read lang from navigator and try find what we have in supported languages |
| 77 | + const lang = navigator.language.split('-')[0]; |
| 78 | + const foundLang = supportedLanguages.find((l) => l.code == lang); |
| 79 | + if (foundLang) { |
| 80 | + return foundLang.code; |
| 81 | + } |
| 82 | + return supportedLanguages[0].code; |
| 83 | +} |
| 84 | + |
| 85 | +export function setLocalLang(lang: string) { |
| 86 | + localStorage.setItem(LS_LANG_KEY, lang); |
| 87 | +} |
| 88 | + |
| 89 | + |
0 commit comments