根据选择动态加载i18n Angular语言环境 [英] Dynamically load i18n Angular locale depending on select
本文介绍了根据选择动态加载i18n Angular语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用带有所有国家/地区列表的select,并使用外部json-File进行填充.在此文件中,我有一个i18n语言环境语言-国家/地区"的密钥,例如."zh-cn".
I use an select with a list of all countries, populating with an external json-File. In this File, I have a key for the i18n locale 'language - Country' for e.g. 'en-us'.
更新选择后,我得到了语言环境.有没有办法动态加载来自目录 https://code.angularjs的i18n angular.org/1.2.10/i18n/ angular-locale_XX-XX.js?谢谢您的提示
I get the locale after updating the select. Is there a way to dynamicall load the i18n angular, coming from the directory https://code.angularjs.org/1.2.10/i18n/ angular-locale_XX-XX.js ? Thank you for your tips
HTML
<select ng-change="updateCountry()" ng-disabled="!data.locations.countries.$resolved" ng-model="selectionCountry" ng-options="country.name for country in data.locations.countries"></select>
脚本
.controller('MyCtrl', ['$scope','$filter', '$http', '$timeout', '$locale', function($scope, $filter, $http, $timeout, $locale) {
$scope.data = {
locations: {
countries: []
}
};
// set default Country
$scope.data.locations.countries.$default = 'United States';
$scope.data.locations.countries.$resolved = false;
// Populate countries.json in Country Select
$http.get('l10n/countries.json').success(function(countries) {
$scope.data.locations.countries.length = 0;
// actually filter is set to none. to activate choose for e.g. (countries, 'name')
Array.prototype.push.apply($scope.data.locations.countries, $filter('orderBy')(countries, ''));
$scope.selectionCountry || ($scope.selectionCountry = $filter('filter')($scope.data.locations.countries, {name: $scope.data.locations.countries.$default})[0]);
$scope.data.locations.countries.$resolved = true;
});
// get the i18n locale for the selected option
$scope.updateCountry = function() {
var selFormat=$scope.selectionCountry.i18n;
console.log(selFormat);
};
推荐答案
好.我找到了解决方案:
O.k. I got the solution:
var imported = document.createElement('script');
var fileImport = 'angular-locale_' + selFormat + '.js';
imported.src = 'https://code.angularjs.org/1.2.10/i18n/' + fileImport;
document.head.appendChild(imported);
这篇关于根据选择动态加载i18n Angular语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文