0
我正在格式化貨幣格式的數字,用小數點和逗號/點標記數千。angularjs根據所選翻譯語言指示貨幣格式化
app.directive('format', ['$filter', function ($filter) {
return {
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
return $filter(attrs.format)(ctrl.$modelValue)
});
elem.bind('blur', function(event) {
var plainNumber = elem.val().replace(/[^\d|\-+|\.+]/g, '');
elem.val($filter(attrs.format)(plainNumber));
});
}
};
}]);
我的應用程序有兩種語言界面,德語和英語,它們與貨幣相關的數字表示有所不同。
我該如何考慮用戶選擇的語言,以便格式符合用戶的選擇?
這裏是我的語言翻譯代碼:
app.config(['$translateProvider',
function ($translateProvider) {
$translateProvider.useStaticFilesLoader({
prefix: 'assets/i18n/',
suffix: '.json'
});
$translateProvider.preferredLanguage('en');
$translateProvider.useLocalStorage();
}]);
主控制器:
$scope.language = {
listIsOpen: false,
available: {
'en': 'English',
'de_DE': 'Deutsch'
},
init: function() {
var proposedLanguage = $translate.proposedLanguage() || $translate.use();
var preferredLanguage = $translate.preferredLanguage();
$scope.language.selected = $scope.language.available[(proposedLanguage || preferredLanguage)];
},
set: function (localeId, ev) {
$translate.use(localeId);
$scope.language.selected = $scope.language.available[localeId];
$scope.language.listIsOpen = !$scope.language.listIsOpen;
}
};
$rootScope.$on('$translateChangeSuccess', function() {
var lang = $translate.use();
$http.get('assets/i18n/'+lang+'.json').success(function(data) {
$rootScope.i18nTranslationJSON = data;
$rootScope.$emit('myTranslationsLoaded');
});
});
$scope.language.init();
https://github.com/lgalfaso/angular-dynamic-locale – Phil