I have implemented angular-translate for internationalization in my AngularJS project.
Each view in the application has its own controller where I define the value to display as the page title.
Sample Code
HTML
<h1>{{ pageTitle }}</h1>
JavaScript
.controller('FirstPageCtrl', ['$scope', '$filter', function ($scope, $filter) {
$scope.pageTitle = $filter('translate')('HELLO_WORLD');
}])
.controller('SecondPageCtrl', ['$scope', '$filter', function ($scope, $filter) {
$scope.pageTitle = 'Second page title';
}])
I am using the angular-translate-loader-url extension to load translation files.
Issue
Upon initial loading of the page, the translation key is displayed instead of the actual translated text. For instance, instead of displaying Hello, World!
, it shows HELLO_WORLD
.
However, upon revisiting the page, the translation works fine and displays the correct text.
I suspect this problem arises because the translation file may not be fully loaded when the controller assigns the value to $scope.pageTitle
.
Note
Using
<h1>{{ pageTitle | translate }}</h1>
along with $scope.pageTitle = 'HELLO_WORLD';
solves this issue by ensuring translations work properly from the first load. However, there are cases where I might not want to use translations (such as passing a raw string to the second controller).
Query
Is this a common challenge or limitation? How can this be addressed?