Get current language with angular-translate

AngularjsAngular Translate

Angularjs Problem Overview


Is there a way to get the current used language in a controller (without $translateProvider)?

Couldn't find anything in the $translate service.

Angularjs Solutions


Solution 1 - Angularjs

$translate.use() is a getter and setter.

See this demo found in links of docs:

http://jsfiddle.net/PascalPrecht/eUGWJ/7/

Solution 2 - Angularjs

$translate.use() is the way to go. Also, when an asynchronous loader is executed, you might wanna use $translate.proposedLanguage() which returns the language key of the language that is currently loaded but not finished loaded yet.

Solution 3 - Angularjs

When using angular-translate-loader-static-files I have noticed that $translate.proposedLanguage() returned undefined when using the default language while $translate.use() always returned the proposed language.

Therefore I fixed it by using:

var currentLang = $translate.proposedLanguage() || $translate.use();

Solution 4 - Angularjs

The $translate service has a method called preferredLanguage() that return what you want. The return of this function is the string of the language, like 'en'.

Here i wrote you an example:

angular.module('traslateApp').controller('myController', ['$scope', '$translate', function($scope,$translate){
   $scope.changeLanguage = function (langKey) {
      $translate.use(langKey);
   };
   $scope.getCurrentLanguage = function () {
       $translate.preferredLanguage();
   };
}])

Solution 5 - Angularjs

$translate.use() seems not to work on initial load of the app, to get last selected language from storage: $translate.storage().get( $translate.storageKey() ) or just $translate.proposedLanguage();

Solution 6 - Angularjs

translate.currentLang is used to check the current selected language in i18n

Solution 7 - Angularjs

I think this is the better way to determine the language -

$window.navigator.language || $window.navigator.userLanguage

Solution 8 - Angularjs

Maybe is not related but could be useful. In angular2+ the way to access to the current language is

...
import { TranslateService } from '@ngx-translate/core';

export class MyComponent implements OnInit {
  constructor(private translate: TranslateService) {}

  ngOnInit() {
   translate.use('it');
   const currentLang = this.translate.currentLang;
  }
 }


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjviottiView Question on Stackoverflow
Solution 1 - AngularjscharlietflView Answer on Stackoverflow
Solution 2 - AngularjsPascal PrechtView Answer on Stackoverflow
Solution 3 - AngularjsJoan-Diego RodriguezView Answer on Stackoverflow
Solution 4 - AngularjsIran ReyesView Answer on Stackoverflow
Solution 5 - AngularjsxacView Answer on Stackoverflow
Solution 6 - AngularjsJackView Answer on Stackoverflow
Solution 7 - AngularjsAleksandr GolovatyiView Answer on Stackoverflow
Solution 8 - AngularjsEmanuele FricanoView Answer on Stackoverflow