文件目录
先引入所需的js文件
<script> /** * Created by Administrator on 2015/10/20. */ var app = angular.module('myApp', ['pascalprecht.translate']); // 加载本地静态化资源 app.config(['$translateProvider', function ($translateProvider) { $translateProvider.useStaticFilesLoader({ files: [{ prefix: 'locale-', suffix: '.json' }, { prefix: '/locale/locale-', suffix: '.json' }] }); $translateProvider.preferredLanguage('en'); }]); //config translate to load static file app.config(function ($translateProvider) { $translateProvider.useStaticFilesLoader({ files: [{ prefix: './locale/locale-', suffix: '.json' }] }); $translateProvider.registerAvailableLanguageKeys(['en', 'zh'], { 'en_US': 'en', 'en_UK': 'en', 'zh_CN': 'zh' }); //set preferred lang //$translateProvider.preferredLanguage('en'); //auto determine preferred lang $translateProvider.determinePreferredLanguage(); //when can not determine lang, choose en lang. $translateProvider.fallbackLanguage('en'); }); // 多语言选择 app.controller('Ctrl', ['$translate', '$scope', function ($translate, $scope) { $scope.changeLanguage = function (langKey) { $translate.use(langKey); }; }]); </script>创建locale-en.json
{ "HEADLINE": "What an awesome module!", "PARAGRAPH": "Srsly!", "PASSED_AS_TEXT": "Hey there! I'm passed as text value!", "PASSED_AS_ATTRIBUTE": "I'm passed as attribute value, cool ha?", "PASSED_AS_INTERPOLATION": "Beginners! I'm interpolated!", "VARIABLE_REPLACEMENT": "Hi {{name}}", "BUTTON_LANG_DE": "german", "BUTTON_LANG_EN": "english" }创建locale-de.json
{ "HEADLINE": "Was für ein gro?artiges Modul!", "PARAGRAPH": "Ernsthaft!", "PASSED_AS_TEXT": "Hey! Ich wurde als text übergeben!", "PASSED_AS_ATTRIBUTE": "Ich wurde als Attribut übergeben, cool oder?", "PASSED_AS_INTERPOLATION": "Anf?nger! Ich bin interpoliert!", "VARIABLE_REPLACEMENT": "Hi {{name}}", "BUTTON_LANG_DE": "deutsch", "BUTTON_LANG_EN": "englisch" }页面引用代码
<div ng-controller="Ctrl"> <p>{{ 'HEADLINE' | translate }}</p> <p>{{ 'PARAGRAPH' | translate }}</p> <p translate>PASSED_AS_TEXT</p> <p translate="PASSED_AS_ATTRIBUTE"></p> <p translate>{{ 'PASSED_AS_INTERPOLATION' }}</p> <p translate="{{ 'PASSED_AS_INTERPOLATION' }}"></p> <p translate="VARIABLE_REPLACEMENT" translate-values="{ name: 'PascalPrecht' }"></p> <p translate>MISSING_TRANSLATION</p> <button ng-click="changeLanguage('de')" translate="BUTTON_LANG_DE"></button> <button ng-click="changeLanguage('en')" translate="BUTTON_LANG_EN"></button> </div>
楼主:希望对需要做国际化的朋友有用!!!!!!!!!