undf/angular-utrans

Symfony2 和 AngularJS 客户端翻译模块

dev-master 2013-09-20 15:38 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:01:56 UTC


README

AngularTranslator Bundle ( Symfony2 ) 提供客户端翻译功能。该翻译器可以通过 Angular 滤镜或服务访问。

安装

在您的 composer.json 文件中包含来自 Packagist 的 Bundle

require: { "undf/angular-utrans": "dev-master" }

包含 JavaScript

  @UndfAngularTransBundle/Resources/public/js/services/uTrans.js

传递一个包含所有暴露消息键的 JSON,这些键将从特定目录中为翻译器提供数据。

只需包含 twig 函数,在第一个参数中指定,第二个参数中指定目录域名。

  {{ utrans_expose_translations('NameOfTheCatalogueYouWantToExpose', 'locale') }}

utrans_expose_translations 使用 "translator" 服务读取 "NameOfTheCatalogueYouWantToExpose" 目录,创建一个 JSON 对象,该对象将所有翻译暴露给客户端。因此,您不必担心创建此配置,twig 函数会为您完成。twig 辅助工具将为您完成以下操作

<script type="text/javascript">
                    angular.module("uTrans").value("translations", {
                            "key-to-translate" : "Translated String"
           });
</script>
```

This bundle uses an Angular Module called "uTrans"
Don't forget to include this module as a dependency of your AngularJS application.

Example:
```html
<html ng-app="mainModule">
...
</html>
// You must add the uTrans module in your app's dependencies
angular.module('mainModule', [ 'ng', 'uTrans' , '...' ]);

用法

从 *.html.twig 文件

{% raw %}

  {{ 'key_to_translate' | trans  }}  

{# Note that the curly braces are a string that will be interpreted by angularJS #}
  
{% endraw %}

如果需要传递参数,请使用占位符 %myVariable% 传递

  {{ 'key_to_translate' | trans: { '%myVariable%' : javascriptVar }  }}

您还可以使用 uTrans 服务,并允许 AngularJS 在需要的地方为您注入服务

angular.module('aModule', [ 'uTrans' ]).
    factory('aService', function( uTrans ){ 
    //  uTrans is the service injected by Angular I will use in my 'aService';

        var foo = 'bar';
        var message = uTrans.trans('myKey', { '%param%' : foo })
        alert( message );

});