86

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

Couldn't find anything in the $translate service.

Shahar
  • 2,269
  • 1
  • 27
  • 34
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • 1
    If for some god forsaken reason you need to use it straight in the view somewhat globally, probably the method of least effort is to define the language code in your translation provider (ie. in the actual translation file) e.g. `{ "LANG_CODE": "en" }` and use the `translate` filter in the view as usual, like: `` – Jari Keinänen Dec 07 '16 at 12:32
  • Why don't use $window.navigator – Aleksandr Golovatyi May 14 '18 at 10:08

9 Answers9

157

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

See this demo found in links of docs:

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

Chango
  • 6,754
  • 1
  • 28
  • 37
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Should I use this service only for that single feature to get current browser language? even if I'm not planning to do translations – ses Feb 10 '15 at 17:46
  • 5
    This did not give me the current language but the default one. $translate.proposedLanguage() gave me the current used language in a controller as per the question – Joan-Diego Rodriguez Mar 06 '15 at 15:42
41

$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.

Pascal Precht
  • 8,803
  • 7
  • 41
  • 53
  • 2
    I was trying to determine the language before the translations were loaded but `$translate.use()` wasn't set. `$translate.proposedLanguage()` gave me the correct lang. – ncabral Nov 13 '14 at 06:51
  • 3
    Is it possible to use this ``$translate.proposedLanguage()`` in app.config ? i only have $translateProvider and i need to find the result of this function IN config, not later in controller. Do you have an idea ? Thanks a lot – maxime1992 Jul 29 '15 at 11:59
  • 2
    + for `proposedLanguage()` – Herr Derb Jul 01 '16 at 12:19
38

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();
Joan-Diego Rodriguez
  • 2,439
  • 1
  • 27
  • 29
8

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();
   };
}])
digit
  • 4,479
  • 3
  • 24
  • 43
Iran Reyes
  • 523
  • 4
  • 9
  • 2
    This would give you the "preferred" language, not necessarily the "current" language selected. If you switch between languages, my understanding is that this does not do likewise. – arcseldon May 25 '14 at 19:57
  • 2
    var currentLanguage = $translate.use(); does switch on change, as per answer by charlietfl – arcseldon May 25 '14 at 20:02
8

$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();

pomeh
  • 4,742
  • 4
  • 23
  • 44
xac
  • 310
  • 3
  • 9
1

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

Miron
  • 1,007
  • 2
  • 17
  • 31
Jack
  • 31
  • 5
0

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

$window.navigator.language || $window.navigator.userLanguage
Aleksandr Golovatyi
  • 1,033
  • 1
  • 11
  • 18
0

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;
  }
 }
-1
import { TranslateService } from '@ngx-translate/core';

export class MyComponent implements OnInit {
  constructor(private translate: TranslateService)
 {
  translate.setDefaultLang('en');
}
Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
Viknesh
  • 1
  • 1