refactor ux language handling
This commit is contained in:
parent
39e6a81b62
commit
55afc4d9d4
8 changed files with 125 additions and 81 deletions
95
src/js/services/uxLanguage.js
Normal file
95
src/js/services/uxLanguage.js
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
'use strict';
|
||||
angular.module('copayApp.services')
|
||||
.factory('uxLanguage', function languageService($log, lodash, gettextCatalog, amMoment, configService) {
|
||||
var root = {};
|
||||
|
||||
root.availableLanguages = [{
|
||||
name: 'English',
|
||||
isoCode: 'en',
|
||||
}, {
|
||||
name: 'Français',
|
||||
isoCode: 'fr',
|
||||
}, {
|
||||
name: 'Italiano',
|
||||
isoCode: 'it',
|
||||
}, {
|
||||
name: 'Deutsch',
|
||||
isoCode: 'de',
|
||||
}, {
|
||||
name: 'Español',
|
||||
isoCode: 'es',
|
||||
}, {
|
||||
name: 'Português',
|
||||
isoCode: 'pt',
|
||||
}, {
|
||||
name: 'Ελληνικά',
|
||||
isoCode: 'el',
|
||||
}, {
|
||||
name: '日本語',
|
||||
isoCode: 'ja',
|
||||
}, {
|
||||
name: 'Pусский',
|
||||
isoCode: 'ru',
|
||||
}];
|
||||
|
||||
root.currentLanguage = null;
|
||||
|
||||
root._detect = function() {
|
||||
// Auto-detect browser language
|
||||
var userLang, androidLang;
|
||||
|
||||
if (navigator && navigator.userAgent && (androidLang = navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) {
|
||||
userLang = androidLang[1];
|
||||
} else {
|
||||
// works for iOS and Android 4.x
|
||||
userLang = navigator.userLanguage || navigator.language;
|
||||
}
|
||||
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
|
||||
|
||||
return userLang;
|
||||
};
|
||||
|
||||
root._set = function(lang) {
|
||||
$log.debug('Setting default language: ' + lang);
|
||||
gettextCatalog.setCurrentLanguage(lang);
|
||||
amMoment.changeLocale(lang);
|
||||
root.currentLanguage = lang;
|
||||
};
|
||||
|
||||
root.getCurrentLanguage = function() {
|
||||
return root.currentLanguage;
|
||||
};
|
||||
|
||||
root.getCurrentLanguageName = function() {
|
||||
return root.getName(root.currentLanguage);
|
||||
};
|
||||
|
||||
root.getLanguages = function() {
|
||||
return root.availableLanguages;
|
||||
};
|
||||
|
||||
root.init = function() {
|
||||
root._set(root._detect());
|
||||
};
|
||||
|
||||
root.update = function() {
|
||||
var userLang = configService.getSync().wallet.settings.defaultLanguage;
|
||||
|
||||
if (!userLang) {
|
||||
userLang = root._detect();
|
||||
}
|
||||
|
||||
if (userLang != gettextCatalog.getCurrentLanguage()) {
|
||||
root._set(userLang);
|
||||
}
|
||||
return userLang;
|
||||
};
|
||||
|
||||
root.getName = function(lang) {
|
||||
return lodash.result(lodash.find(root.availableLanguages, {
|
||||
'isoCode': lang
|
||||
}), 'name');
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue