我见过Angular的一些i18n插件,但我不想重新发明这个轮子.i18next是一个很好的库,因此,我打算使用它.
我创建了一个指令i18n,它只调用i18n库:
define(['app', 'jquery', 'i18n'], function(app, $, i18n) {'use strict';
app.directive('i18n', function() {
return function($scope, elm, attrs) {
attrs.$observe('i18n', function(value) {
if ($.fn.i18n) {// for some reason, it isn't loaded quickly enough and first directives process fails
$(elm).i18n();
}
});
};
});
});
Run Code Online (Sandbox Code Playgroud)
在我的页面上,我可以动态更改语言:
<a ng-repeat="l in languages"> <img ng-src="images/{{l.id}}.png" title="{{l.label}}" ng-click="setLanguage(l.id)" /> </a>
Run Code Online (Sandbox Code Playgroud)
现在,我的主控制器在html标签上定义:
define(['app', 'i18n', 'jquery'], function(app, i18n, $) {'use strict';
return app.controller('BuilderController', ['$scope', '$location',
function BuilderController($scope, $location) {
/* Catch url changes */
$scope.$watch(function() {
return $location.path(); …Run Code Online (Sandbox Code Playgroud) javascript internationalization angularjs angularjs-directive