I can't find it in docs or api of angular-translate. How can I retrieve all loaded languages by angular translate?
Assuming I have a LanguageCtrl like this:
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();
}
}]);
And I load these 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');
}]);
Now I would like to display all languages:
<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>
Unfortunately there's no API yet to get all registered languages. So in short: this is not possible yet.
If you want to, you can open an issue on github to propose that feature. However, keep in mind that when using asynchronous loaders, languages are not registered until they are loaded.
Updated answer:
/**
* @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']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With