angular translate入门示例

最近在做angularJS,记录一下angular translate的基本使用。


页面:




    
    Index


    


js:
var app = angular.module('myApp', ['pascalprecht.translate']).config(['$translateProvider',function($translateProvider){
    var lang = window.localStorage.lang || 'cn';
    console.info(lang);
    $translateProvider.preferredLanguage(lang);
    $translateProvider.useStaticFilesLoader({
        prefix: '../i18n/',
        suffix: '.json'
    });
}]);

app.controller('myController', ['$scope', '$translate', function (scope, $translate) {
    scope.switching = function(lang){
        $translate.use(lang);
        window.localStorage.lang = lang;
        window.location.reload();
    };
    scope.test = '11111111111';

    $translate('100001').then(function (key) {
        console.info('trans --------' + key);
        scope.test = key;
    });

    //$translate(['HEADLINE', 'PARAGRAPH', 'NAMESPACE.PARAGRAPH']).then(function (translations) {
    //    $scope.headline = translations.HEADLINE;
    //    $scope.paragraph = translations.PARAGRAPH;
    //    $scope.namespaced_paragraph = translations['NAMESPACE.PARAGRAPH'];
    //});

}]);


cn.json:

{"100001":"登录","100002":"注册"}




你可能感兴趣的:(angularjs)