Commit e56f598e317e0f73881bc6004495c39c60bccf65
i18n - Try BCP47 codes, otherwise load family language if exist.
Marcos Gutierrez committed on 10/26/2017, 1:46:36 PMParent: 37f1f5982ffb6d545b8e4e7156aefa8285ecdc6b
Files changed
plugs/intl/sync/i18n.js | changed |
plugs/intl/sync/i18n.js | ||
---|---|---|
@@ -91,12 +91,19 @@ | ||
91 | 91 | defaultLocale: 'en' |
92 | 92 | }) |
93 | 93 | |
94 | 94 | watch(api.settings.obs.get('patchwork.lang', navigator.language), currentLocale => { |
95 | - i18nL.setLocale(getSubLocal(currentLocale)) | |
95 | + var locales = i18nL.getLocales() | |
96 | 96 | |
97 | - // Only refresh if the language has already been selected once. | |
98 | - // This will prevent the update loop | |
97 | + // Try BCP47 codes, otherwise load family language if exist | |
98 | + if (locales.indexOf(currentLocale) !== -1) { | |
99 | + i18nL.setLocale(currentLocale) | |
100 | + } else { | |
101 | + i18nL.setLocale(getSimilar(locales, currentLocale)) | |
102 | + } | |
103 | + | |
104 | + // Only refresh if the language has already been selected once. | |
105 | + // This will prevent the update loop | |
99 | 106 | if (_locale) { |
100 | 107 | electron.remote.getCurrentWebContents().reloadIgnoringCache() |
101 | 108 | } |
102 | 109 | }) |
@@ -108,4 +115,17 @@ | ||
108 | 115 | // For now get only global languages |
109 | 116 | function getSubLocal (loc) { |
110 | 117 | return loc.split('-')[0] |
111 | 118 | } |
119 | + | |
120 | +function getSimilar (locales, option) { | |
121 | + var reindexed = {} | |
122 | + locales.forEach(function (local) { | |
123 | + (reindexed[getSubLocal(local)]) | |
124 | + ? reindexed[getSubLocal(local)].concat(local) | |
125 | + : reindexed[getSubLocal(local)] = [local] | |
126 | + }, this) | |
127 | + if (reindexed[getSubLocal(option)]) { | |
128 | + return reindexed[getSubLocal(option)][0] | |
129 | + } | |
130 | + return option | |
131 | +} |
Built with git-ssb-web