Commit f2ca2ca157f5185508d7d9ac0fc3e92532a793cd
Merge branch 'i18n-BCP47' of https://github.com/ssbc/patchwork
Matt McKegg committed on 10/28/2017, 10:36:26 AMParent: 42288cf44aa8aaf32d1036942ee30c6730803575
Parent: e56f598e317e0f73881bc6004495c39c60bccf65
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