Page 399 - AngularJS权威教程
P. 399

27.13  改变语言    379


                                  'app/scripts/translations.js': ['po/*.pot']                        19
                              },
                          },
                      }
                  })                                                                                 20
                  这个配置会使用所有的po/*.pot文件生成app/scripts/translations.js文件。
                                                                                                     21
                  也可以指定一个想要定义在translations内的特定模块,比如:
                  grunt.initConfig({
                      all: {                                                                         22
                              options: {
                                  module: 'myApp.translations'
                              },                                                                     23
                              files: {
                                  'app/scripts/translations.js': ['po/*.pot']
                              },
                          },                                                                         24
                      }
                  })
                                                                                                     25

                          建议设置一个Grunt任务来调用这两个nggettext_*函数,比如grunt.register
                         Task ('default', ['nggettext_extract', 'nggettext_compile']);。              26

                  现在,运行Grunt任务时,它会为我们生成translations.js文件。而我们只需要将这个文件包含
              在运行时的.html文件中,然后应用就已经做好翻译的准备了。                                                         27


              27.13  改变语言                                                                            28


                  至此我们已经设置好语言了,然后可以使用这些翻译来支持不同的语言。
                                                                                                     29
                  gettext模块还包含一个叫做gettextCatalog的服务,这个服务可以注入到应用中设置当前
              语言。为了设置默认语言,可以简单地调用它:
                                                                                                     30
                  angular.module('myApp')
                  .run(function(gettextCatalog) {
                      gettextCatalog.currentLanguage = 'es';                                         31
                  });
                  上面这段代码会把应用程序的内容当作西班牙语加载。                                                           32

                  注意,也可以在系统运行时做同样的事情,只需将gettextCatalog注入到Angular对象中,
              就像这样:                                                                                  33

                  .controller('HomeController', function($scope, gettextCatalog) {
                      $scope.user = { name: "Ari" };
                      $scope.count = 1;                                                              34
                      $scope.changeLanguage = function() {
                          gettextCatalog.currentLanguage = 'es';
                      };                                                                             35
                  })
                  英文状态,如图27-5所示。
                                                                                                     36
   394   395   396   397   398   399   400   401   402   403   404