Is there a way to get the current used language in a controller (without $translateProvider
)?
Couldn't find anything in the $translate
service.
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();
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();
};
}])
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;
}
}
{ "LANG_CODE": "en" }
and use thetranslate
filter in the view as usual, like:<video controls poster="img/poster-{{ 'LANG_CODE' | translate }}.png"> […] </video>
– Jari Keinänen