Skip to content

Commit

Permalink
fix: i18n get language by electron locale
Browse files Browse the repository at this point in the history
  • Loading branch information
agalwood committed May 18, 2021
1 parent 6baef42 commit 2bc4a8c
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/shared/locales/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,29 @@ function checkLngIsAvailable (locale) {
/**
* getLanguage
* @param { String } locale
* https://electronjs.org/docs/api/locales
* https://www.electronjs.org/docs/api/app#appgetlocale
*
* Only these locales need to add a `startsWith` fallback
* when there are with the same prefix
*
* ar, ar-XB
* de, de-AT, de-CH, de-DE
* en, en-AU, en-CA, en-GB, en-NZ, en-US, en-ZA
* es, es-419
* en, en-AU, en-CA, en-GB, en-IN, en-NZ, en-US, en-XA, en-ZA
* es, es-419, es-AR, es-CL, es-CO, es-CR, es-ES, es-HN, es-MX, es-PE, es-US, es-UY, es-VE
* fr, fr-CA, fr-CH, fr-FR
* it, it-CH, it-IT
* pt, pt-BR, pt-PT
* zh, zh-CN, zh-TW
* zh, zh-CN, zh-HK, zh-TW
*/
export function getLanguage (locale = 'en-US') {
if (checkLngIsAvailable(locale)) {
return locale
}

if (locale.startsWith('ar')) {
return 'ar'
}

if (locale.startsWith('de')) {
return 'de'
}
Expand All @@ -141,17 +146,25 @@ export function getLanguage (locale = 'en-US') {
return 'es'
}

if (locale.startsWith('fr')) {
return 'fr'
}

if (locale.startsWith('it')) {
return 'it'
}

// If there is a pt-PT translation in the future,
// here will fallback to pt-PT.
if (locale.startsWith('pt')) {
return 'pt-BR'
}

if (locale.startsWith('zh')) {
return 'zh-CN'
if (locale === 'zh-HK') {
return 'zh-TW'
}

if (locale.startsWith('fr')) {
return 'fr'
if (locale.startsWith('zh')) {
return 'zh-CN'
}
}

0 comments on commit 2bc4a8c

Please sign in to comment.