Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 631
fischermatte Avatar asked May 14 '14 16:05

fischermatte


2 Answers

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.

like image 163
Pascal Precht Avatar answered Nov 02 '22 19:11

Pascal Precht


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']);
like image 21
sports Avatar answered Nov 02 '22 18:11

sports