Klaus Native
Klaus Native

Reputation: 3509

How to list all languages that are loaded by angular-translate

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>

Upvotes: 7

Views: 6529

Answers (2)

sports
sports

Reputation: 8147

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']);

Upvotes: 3

Pascal Precht
Pascal Precht

Reputation: 8893

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.

Upvotes: 10

Related Questions