我无法在angular-translate的文档或api中找到它。如何通过角度翻译检索所有加载的语言?
假设我有一个像这样的LanguageCtrl:
angular.module('myApp')
.controller('LanguageCtrl', ['$translate', '$scope',
function ($translate, $scope) {
$scope.switchLang = function (lang) {
$translate.use(lang);
};
$scope.currentLang = function () {
return $translate.use();
};
$scope.isCurrentLang = function (lang) {
return $translate.use() === lang;
};
$scope.languages = function(){
return $translate.IS_THERE_AN_API_FUNCTION_TO_GET_ALL_LANGUAGES();
}
}]);
我加载这些语言:
angular.module('myApp', ['pascalprecht.translate'])
.config(['$translateProvider', function ($translateProvider) {
$translateProvider.translations('de', de);
$translateProvider.translations('fr', fr);
$translateProvider.translations('en', en);
$translateProvider.preferredLanguage('en');
}]);
现在我想显示所有语言:
<ul ng-controller="LanguageCtrl">
<li ng-repeat="lang in languages" ng-class="{active: isCurrentLang(lang)}">
<a href="" ng-click="switchLang(lang)">lang</a>
</li>
</ul>
答案 0 :(得分:10)
不幸的是,还没有API可以获得所有注册语言。简而言之:这不可能 。
如果您愿意,可以在github上打开一个问题来提出该功能。但是,请记住,在使用异步加载程序时,语言不会在加载之前注册。
答案 1 :(得分:3)
更新回答:
/**
* @description
* This function simply returns the registered language keys being defined before in the config phase
* With this, an application can use the array to provide a language selection dropdown or similar
* without any additional effort
*
* @returns {object} returns the list of possibly registered language keys and mapping or null if not defined
*/
$translate.getAvailableLanguageKeys();
/*
* To register language keys, use the following function in your configuration:
*/
$translateProvider.registerAvailableLanguageKeys(['en', 'de']);