2024-06-16 16:42:40 +02:00
|
|
|
import Vue from 'vue'
|
2022-11-09 00:10:08 +01:00
|
|
|
import enUsStrings from '../strings/en-us.json'
|
2022-11-09 01:09:07 +01:00
|
|
|
import { supplant } from './utils'
|
2022-11-07 00:56:44 +01:00
|
|
|
|
|
|
|
const defaultCode = 'en-us'
|
|
|
|
|
2022-11-09 01:09:07 +01:00
|
|
|
const languageCodeMap = {
|
2024-06-16 16:42:40 +02:00
|
|
|
bg: { label: 'Български', dateFnsLocale: 'bg' },
|
|
|
|
bn: { label: 'বাংলা', dateFnsLocale: 'bn' },
|
|
|
|
cs: { label: 'Čeština', dateFnsLocale: 'cs' },
|
|
|
|
da: { label: 'Dansk', dateFnsLocale: 'da' },
|
|
|
|
de: { label: 'Deutsch', dateFnsLocale: 'de' },
|
2023-01-30 15:07:36 +01:00
|
|
|
'en-us': { label: 'English', dateFnsLocale: 'enUS' },
|
2024-06-16 16:42:40 +02:00
|
|
|
es: { label: 'Español', dateFnsLocale: 'es' },
|
|
|
|
et: { label: 'Eesti', dateFnsLocale: 'et' },
|
|
|
|
fi: { label: 'Suomi', dateFnsLocale: 'fi' },
|
|
|
|
fr: { label: 'Français', dateFnsLocale: 'fr' },
|
|
|
|
he: { label: 'עברית', dateFnsLocale: 'he' },
|
|
|
|
hr: { label: 'Hrvatski', dateFnsLocale: 'hr' },
|
|
|
|
it: { label: 'Italiano', dateFnsLocale: 'it' },
|
|
|
|
lt: { label: 'Lietuvių', dateFnsLocale: 'lt' },
|
|
|
|
hu: { label: 'Magyar', dateFnsLocale: 'hu' },
|
|
|
|
nl: { label: 'Nederlands', dateFnsLocale: 'nl' },
|
|
|
|
no: { label: 'Norsk', dateFnsLocale: 'no' },
|
|
|
|
pl: { label: 'Polski', dateFnsLocale: 'pl' },
|
2024-02-16 15:57:54 +01:00
|
|
|
'pt-br': { label: 'Português (Brasil)', dateFnsLocale: 'ptBR' },
|
2024-06-16 16:42:40 +02:00
|
|
|
ru: { label: 'Русский', dateFnsLocale: 'ru' },
|
|
|
|
sv: { label: 'Svenska', dateFnsLocale: 'sv' },
|
|
|
|
uk: { label: 'Українська', dateFnsLocale: 'uk' },
|
2024-03-10 03:09:08 +01:00
|
|
|
'vi-vn': { label: 'Tiếng Việt', dateFnsLocale: 'vi' },
|
2023-01-30 15:42:56 +01:00
|
|
|
'zh-cn': { label: '简体中文 (Simplified Chinese)', dateFnsLocale: 'zhCN' },
|
2024-03-18 01:19:52 +01:00
|
|
|
'zh-tw': { label: '正體中文 (Traditional Chinese)', dateFnsLocale: 'zhTW' }
|
2022-11-09 01:09:07 +01:00
|
|
|
}
|
2024-06-16 16:42:40 +02:00
|
|
|
Vue.prototype.$languageCodeOptions = Object.keys(languageCodeMap).map((code) => {
|
2022-11-09 01:09:07 +01:00
|
|
|
return {
|
2023-01-30 15:07:36 +01:00
|
|
|
text: languageCodeMap[code].label,
|
2022-11-09 01:09:07 +01:00
|
|
|
value: code
|
|
|
|
}
|
|
|
|
})
|
2024-02-17 20:24:49 +01:00
|
|
|
|
|
|
|
// iTunes search API uses ISO 3166 country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
|
2024-01-04 04:52:45 +01:00
|
|
|
const podcastSearchRegionMap = {
|
2024-06-16 16:42:40 +02:00
|
|
|
br: { label: 'Brasil' },
|
|
|
|
be: { label: 'België / Belgique / Belgien' },
|
|
|
|
cz: { label: 'Česko' },
|
|
|
|
dk: { label: 'Danmark' },
|
|
|
|
de: { label: 'Deutschland' },
|
|
|
|
ee: { label: 'Eesti' },
|
|
|
|
es: { label: 'España / Espanya / Espainia' },
|
|
|
|
fr: { label: 'France' },
|
|
|
|
hr: { label: 'Hrvatska' },
|
|
|
|
il: { label: 'ישראל / إسرائيل' },
|
|
|
|
it: { label: 'Italia' },
|
|
|
|
lu: { label: 'Luxembourg / Luxemburg / Lëtezebuerg' },
|
|
|
|
hu: { label: 'Magyarország' },
|
|
|
|
nl: { label: 'Nederland' },
|
|
|
|
no: { label: 'Norge' },
|
|
|
|
at: { label: 'Österreich' },
|
|
|
|
pl: { label: 'Polska' },
|
|
|
|
pt: { label: 'Portugal' },
|
|
|
|
ru: { label: 'Россия' },
|
|
|
|
ch: { label: 'Schweiz / Suisse / Svizzera' },
|
|
|
|
se: { label: 'Sverige' },
|
|
|
|
vn: { label: 'Việt Nam' },
|
|
|
|
ua: { label: 'Україна' },
|
|
|
|
gb: { label: 'United Kingdom' },
|
|
|
|
us: { label: 'United States' },
|
|
|
|
cn: { label: '中国' }
|
2024-01-04 04:52:45 +01:00
|
|
|
}
|
2024-06-16 16:42:40 +02:00
|
|
|
Vue.prototype.$podcastSearchRegionOptions = Object.keys(podcastSearchRegionMap).map((code) => {
|
2024-01-04 04:52:45 +01:00
|
|
|
return {
|
|
|
|
text: podcastSearchRegionMap[code].label,
|
|
|
|
value: code
|
|
|
|
}
|
|
|
|
})
|
2022-11-09 01:09:07 +01:00
|
|
|
|
|
|
|
Vue.prototype.$languageCodes = {
|
2024-04-19 00:06:12 +02:00
|
|
|
default: defaultCode, // en-us
|
|
|
|
current: defaultCode, // Current language code in use
|
|
|
|
local: null, // Language code set at user level
|
|
|
|
server: null // Language code set at server level
|
2022-11-08 01:27:17 +01:00
|
|
|
}
|
|
|
|
|
2024-04-19 00:06:12 +02:00
|
|
|
// Currently loaded strings (default enUS)
|
2022-11-09 01:09:07 +01:00
|
|
|
Vue.prototype.$strings = { ...enUsStrings }
|
2022-11-10 01:00:20 +01:00
|
|
|
|
2024-04-19 00:06:12 +02:00
|
|
|
/**
|
|
|
|
* Get string and substitute
|
2024-06-16 16:42:40 +02:00
|
|
|
*
|
|
|
|
* @param {string} key
|
|
|
|
* @param {string[]} subs
|
2024-04-19 00:06:12 +02:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-11-08 01:27:17 +01:00
|
|
|
Vue.prototype.$getString = (key, subs) => {
|
|
|
|
if (!Vue.prototype.$strings[key]) return ''
|
2023-07-01 00:30:15 +02:00
|
|
|
if (subs?.length && Array.isArray(subs)) {
|
2022-11-08 01:27:17 +01:00
|
|
|
return supplant(Vue.prototype.$strings[key], subs)
|
|
|
|
}
|
|
|
|
return Vue.prototype.$strings[key]
|
|
|
|
}
|
2022-11-07 00:56:44 +01:00
|
|
|
|
2024-04-19 00:30:06 +02:00
|
|
|
Vue.prototype.$formatNumber = (num) => {
|
|
|
|
return Intl.NumberFormat(Vue.prototype.$languageCodes.current).format(num)
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:06:12 +02:00
|
|
|
const translations = {
|
2022-11-09 00:10:08 +01:00
|
|
|
[defaultCode]: enUsStrings
|
|
|
|
}
|
2022-11-07 00:56:44 +01:00
|
|
|
|
|
|
|
function loadTranslationStrings(code) {
|
|
|
|
return new Promise((resolve) => {
|
2024-06-16 16:42:40 +02:00
|
|
|
import(`../strings/${code}`)
|
|
|
|
.then((fileContents) => {
|
|
|
|
resolve(fileContents.default)
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed to load i18n strings', code, error)
|
|
|
|
resolve(null)
|
|
|
|
})
|
2022-11-07 00:56:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadi18n(code) {
|
2022-11-09 01:09:07 +01:00
|
|
|
if (!code) return false
|
|
|
|
if (Vue.prototype.$languageCodes.current == code) {
|
2022-11-07 00:56:44 +01:00
|
|
|
// already set
|
2022-11-09 01:09:07 +01:00
|
|
|
return false
|
2022-11-07 00:56:44 +01:00
|
|
|
}
|
2022-11-09 01:09:07 +01:00
|
|
|
|
2024-06-16 16:42:40 +02:00
|
|
|
const strings = translations[code] || (await loadTranslationStrings(code))
|
2022-11-09 00:10:08 +01:00
|
|
|
if (!strings) {
|
|
|
|
console.warn(`Invalid lang code ${code}`)
|
2022-11-09 01:09:07 +01:00
|
|
|
return false
|
2022-11-09 00:10:08 +01:00
|
|
|
}
|
2022-11-07 00:56:44 +01:00
|
|
|
|
|
|
|
translations[code] = strings
|
2022-11-09 01:09:07 +01:00
|
|
|
Vue.prototype.$languageCodes.current = code
|
|
|
|
localStorage.setItem('lang', code)
|
2022-11-07 00:56:44 +01:00
|
|
|
|
|
|
|
for (const key in Vue.prototype.$strings) {
|
|
|
|
Vue.prototype.$strings[key] = strings[key] || translations[defaultCode][key]
|
|
|
|
}
|
2023-05-03 14:52:32 +02:00
|
|
|
|
2023-01-30 15:07:36 +01:00
|
|
|
Vue.prototype.$setDateFnsLocale(languageCodeMap[code].dateFnsLocale)
|
2022-11-09 01:09:07 +01:00
|
|
|
|
2024-02-16 16:12:47 +01:00
|
|
|
this?.$eventBus?.$emit('change-lang', code)
|
2022-11-09 01:09:07 +01:00
|
|
|
return true
|
2022-11-07 00:56:44 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 01:09:07 +01:00
|
|
|
Vue.prototype.$setLanguageCode = loadi18n
|
|
|
|
|
|
|
|
// Set the servers default language code, does not override users local language code
|
|
|
|
Vue.prototype.$setServerLanguageCode = (code) => {
|
|
|
|
if (!code) return
|
|
|
|
|
|
|
|
if (!languageCodeMap[code]) {
|
|
|
|
console.warn('invalid server language in', code)
|
|
|
|
} else {
|
|
|
|
Vue.prototype.$languageCodes.server = code
|
|
|
|
if (!Vue.prototype.$languageCodes.local && code !== defaultCode) {
|
|
|
|
loadi18n(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize with language code in localStorage if valid
|
|
|
|
async function initialize() {
|
|
|
|
const localLanguage = localStorage.getItem('lang')
|
|
|
|
if (!localLanguage) return
|
|
|
|
|
|
|
|
if (!languageCodeMap[localLanguage]) {
|
|
|
|
console.warn('Invalid local language code', localLanguage)
|
|
|
|
localStorage.setItem('lang', defaultCode)
|
2022-11-10 01:00:20 +01:00
|
|
|
} else {
|
2022-11-09 01:09:07 +01:00
|
|
|
Vue.prototype.$languageCodes.local = localLanguage
|
|
|
|
loadi18n(localLanguage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initialize()
|