angular.module('plunker', [
'mymodule',
'pascalprecht.translate',
'ui.router'
])
.config(function($translateProvider) {
$translateProvider.useLoader('$translatePartialLoader', {
urlTemplate: './locale-{lang}.json'
});
// fallback language
$translateProvider.preferredLanguage('en');
})
.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: 'module_page.html',
controller: 'DemoController',
controllerAs: 'vm',
resolve: {
ensureUserAndLanguage: function($log, $q, $translate, user){
var deferred = $q.defer();
user.getCurrent()
.then(function(result){
$log.debug('Route resolve. Setting lang to ' + result.language);
// set the language based on the user's preferences
$translate.use(result.language)
.then(function(){
$log.debug('$translate.use. Lang is: ' + $translate.use());
deferred.resolve();
});
});
return deferred.promise;
}
}
});
})
.factory('user', function($q, $timeout){
var service = {
getCurrent: function(){
// normally an HTTP call to the server would take place here
var deferred = $q.defer();
$timeout(function(){
deferred.resolve({
username: 'juristr',
language: 'de'
});
}, 200);
return deferred.promise;
}
};
return service;
});
angular.module('mymodule', ['pascalprecht.translate'])
.config(function($translatePartialLoaderProvider) {
$translatePartialLoaderProvider.addPart('mymodule');
})
.controller('DemoController', function($log, $translate){
var vm = this;
$log.debug('DemoController. Lang is: ' + $translate.use());
vm.currentLanguage = $translate.use();
vm.data = {};
activate()
/////////////////////////
function activate(){
vm.data = {
name: {
de: 'Deutscher Wert',
en: 'English value'
}
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>
<script data-require="angular-translate@*" data-semver="2.6.0" src="https://cdn.rawgit.com/angular-translate/bower-angular-translate/2.6.0/angular-translate.js"></script>
<script data-require="ui-router@*" data-semver="0.2.13" src="//rawgit.com/angular-ui/ui-router/0.2.13/release/angular-ui-router.js"></script>
<script data-require="angular-translate-loader-partial@*" data-semver="2.0.1" src="https://rawgit.com/PascalPrecht/bower-angular-translate-loader-partial/master/angular-translate-loader-partial.js"></script>
<script src="app.js"></script>
</head>
<body>
<ui-view></ui-view>
</body>
</html>
/* Put your css in here */
<h1>Module page</h1>
<p>Current language: {{ vm.currentLanguage }}</p>
<p>
Data: {{ vm.data.name[vm.currentLanguage] }}
</p>
{
"message": "Hi there, how are you?"
}
{
"message": "Hallo, wie gehts?"
}