Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Fix missing language translations bug #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,27 @@ const format = (origStr, ...args) => Array.from(args).reduce(
(str, arg, i) => str.replace(new RegExp(`\\{${i}\\}`, 'g'), arg), origStr
)

// Find a supported locale for the translated language names. Test the locale
// support with the whole locale string, then without a dialect, then without a
// region. If none is supported, return 'en'.
// A support for a locale is determined by having a translation for the 'auto'
// string, this means that a new locale must be complete, all the language
// names must be translated.
const getSupportedLocale = (languages) => {
let locale = ps.getLocalized('general.useragent.locale', 'en')
while (locale.length > 0) {
if (languages.auto[locale]) {
return locale
}
locale = locale.replace(/-?[^-]+$/, '')
}
return 'en'
}

// Get the From language from the preferences
const currentFrom = languages => {
const langCode = sp.prefs.langFrom
const locale = ps.getLocalized('general.useragent.locale', 'en')
const locale = getSupportedLocale(languages)
const lang = languages[langCode][locale]
return {
code: langCode,
Expand All @@ -54,12 +71,13 @@ const currentFrom = languages => {
// Get the To language from the preferences
const currentTo = languages => {
let langCode = sp.prefs.langTo
const locale = ps.getLocalized('general.useragent.locale', 'en')
if (langCode === 'auto') {
if (!locale.startsWith('zh')) {
langCode = locale.replace(/-[a-zA-Z]+$/, '')
langCode = ps.getLocalized('general.useragent.locale', 'en')
if (!langCode.startsWith('zh')) {
langCode = langCode.replace(/-[a-zA-Z]+$/, '')
}
}
const locale = getSupportedLocale(languages)
const lang = languages[langCode][locale]
return {
code: langCode,
Expand All @@ -85,7 +103,7 @@ const cmpLanguages = (a, b, languages, locale) => {
}

const langToItems = (languages, doc) => {
const locale = ps.getLocalized('general.useragent.locale', 'en')
const locale = getSupportedLocale(languages)
return Object.keys(languages)
.filter(lang => !languages[lang].onlyFrom)
.sort((a, b) => {
Expand All @@ -103,7 +121,7 @@ const langToItems = (languages, doc) => {
const langFromMenus = (languages, doc) => {
const toItemsPopup = doc.createElement('menupopup')
langToItems(languages, doc).forEach(item => toItemsPopup.appendChild(item))
const locale = ps.getLocalized('general.useragent.locale', 'en')
const locale = getSupportedLocale(languages)
return Object.keys(languages)
.filter(lang => !languages[lang].onlyTo)
.sort((a, b) => {
Expand Down Expand Up @@ -217,7 +235,7 @@ const initMenu = (win, languages) => {

// Update the languages menu label (“Change Languages […]”)
const updateLangMenuLabel = detected => {
const locale = ps.getLocalized('general.useragent.locale', 'en')
const locale = getSupportedLocale(languages)
const from = detected ? languages[detected][locale] : currentFrom(languages)
const to = currentTo(languages)
langMenu.setAttribute('label', format(
Expand Down