Wallet/src/js/services/uxLanguage.js

111 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-08-27 12:07:13 -03:00
'use strict';
angular.module('copayApp.services')
2015-12-20 19:42:25 -03:00
.factory('uxLanguage', function languageService($log, $timeout, lodash, gettextCatalog, amMoment, configService) {
2015-08-27 12:07:13 -03:00
var root = {};
root.availableLanguages = [{
name: 'English',
isoCode: 'en',
}, {
name: 'Français',
isoCode: 'fr',
}, {
name: 'Deutsch',
isoCode: 'de',
}, {
name: 'Español',
isoCode: 'es',
}, {
name: '日本語',
isoCode: 'ja',
2015-09-18 10:22:10 -03:00
useIdeograms: true,
2015-08-27 12:07:13 -03:00
}, {
name: 'Pусский',
isoCode: 'ru',
}];
root.currentLanguage = null;
2015-12-20 19:42:25 -03:00
root._detect = function(cb) {
2015-08-27 12:07:13 -03:00
2015-12-20 19:42:25 -03:00
var userLang, androidLang;
if (navigator && navigator.globalization) {
navigator.globalization.getPreferredLanguage(function(preferedLanguage) {
// works for iOS and Android 4.x
userLang = preferedLanguage.value;
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
return cb(userLang);
});
2015-08-27 12:07:13 -03:00
} else {
2015-12-20 19:42:25 -03:00
// Auto-detect browser language
2015-08-27 12:07:13 -03:00
userLang = navigator.userLanguage || navigator.language;
2015-12-20 19:42:25 -03:00
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
return cb(userLang);
2015-08-27 12:07:13 -03:00
}
2015-12-20 19:42:25 -03:00
2015-08-27 12:07:13 -03:00
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
2016-01-08 11:03:56 -03:00
// Set only available languages
userLang = lodash.find(root.availableLanguages, {
'isoCode': userLang
}) ? userLang : 'en';
2015-08-27 12:07:13 -03:00
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);
};
2015-09-18 10:22:10 -03:00
root.getCurrentLanguageInfo = function() {
return lodash.find(root.availableLanguages, {
'isoCode': root.currentLanguage
});
};
2015-08-27 12:07:13 -03:00
root.getLanguages = function() {
return root.availableLanguages;
};
root.init = function() {
2015-12-20 19:42:25 -03:00
root._detect(function(lang) {
root._set(lang);
});
2015-08-27 12:07:13 -03:00
};
root.update = function() {
var userLang = configService.getSync().wallet.settings.defaultLanguage;
if (!userLang) {
2015-12-20 19:42:25 -03:00
root._detect(function(lang) {
userLang = lang;
if (userLang != root.currentLanguage) {
root._set(lang);
}
return userLang;
});
2015-08-27 12:07:13 -03:00
}
};
root.getName = function(lang) {
return lodash.result(lodash.find(root.availableLanguages, {
'isoCode': lang
}), 'name');
};
return root;
});