简体   繁体   English

将 Symfony 翻译传递给 Symfony Webpack Encore

[英]Passing Symfony Translation to Symfony Webpack Encore

With Symfony, I use translation , Twig and Webpack encore components.在 Symfony 中,我使用了translation 、 Twig 和Webpack encore组件。

I can translate in frontend Twig with :我可以在前端 Twig 中翻译:

'my_key'|trans

I use command yarn encore dev for generate my app.js , but PHP translation component it's not accessible in Javascript.我使用命令yarn encore dev来生成我的app.js ,但PHP 翻译组件在 Javascript 中无法访问。

I have a lot of things to translate in javascript.我有很多东西需要用 javascript 翻译。

Unfortunately since JS is not handled by PHP and by extension also not by Symfony, you will not have access to Symfony's Translation component inside your js files.不幸的是,由于 JS 不是由 PHP 处理的,扩展名也不是由 Symfony 处理的,因此您将无法访问 js 文件中的 Symfony 翻译组件。

A workaround that could work when you don't have too many translations you need to pass is create a JS data object inside your twig template as part of your Symfony application and then access it from your js files.当您不需要传递太多翻译时,一种可行的解决方法是在您的 twig 模板中创建一个 JS 数据对象作为 Symfony 应用程序的一部分,然后从您的 js 文件中访问它。 So roughly like this:所以大致是这样的:

# inside your twig template, e.g. index.html.twig
{% block javascripts %}
    <script type="text/javascript">
    const TRANSLATION_MAP = {
        'my_key': "{{ 'some_key '|trans }}",
        'my_other_key': "{{ 'other_key '|trans }}"
    };
    </script>

    {{ parent() }} # This loads all your js files which can then access the translation map defined above
{% endblock %}

The downside to this solution is, that you have to decide which keys to put in your translation map without really knowing whether they are used, so this might become a bit inefficient and hard to follow.此解决方案的缺点是,您必须决定将哪些键放入翻译映射中,而无需真正知道它们是否被使用,因此这可能会变得有点低效且难以遵循。 Also you have to be careful that your translated content is valid json.此外,您必须注意翻译的内容是有效的 json。 You can apply (custom) escaping/filtering to ensure that, but still makes it a bit fragile.您可以应用(自定义)转义/过滤来确保这一点,但仍然使它有点脆弱。

All in all, this might not be the best solution but can be a decent workaround for smaller projects until you find it becomes more of a nuisance and you have to find something more sophisticated.总而言之,这可能不是最好的解决方案,但对于较小的项目来说可能是一个不错的解决方法,直到您发现它变得更加麻烦并且您必须找到更复杂的东西。

You have to use BazingaJsTranslationBundle which allows you to access translations you have exposed through javascript:您必须使用BazingaJsTranslationBundle ,它允许您访问通过 javascript 公开的翻译:

Translator.trans('key', {}, 'DOMAIN_NAME');

Translator.transChoice('key', 1, {}, 'DOMAIN_NAME');
  1. You could transform translations yaml to json with webpack or a task manager (gulp | grunt).您可以使用 webpack 或任务管理器 (gulp | grunt) 将翻译 yaml 转换为 json。
  2. Put built json translation files to assets.将构建的 json 翻译文件放入资产。
  3. Require them inside js script.在 js 脚本中需要它们。
  4. Emit locale value to frontend to choose proper translation json object inside js script.将语言环境值发送到前端以在 js 脚本中选择正确的翻译 json 对象。

Note: in webpack case you should run 2 steps consequently: first step will build translations to assets to be required in js scripts.注意:在 webpack 情况下,您应该因此运行 2 个步骤:第一步将构建到 js 脚本中所需的资产的翻译。 second step will compile js scripts afterwards.第二步将在之后编译js脚本。

Here is a part of webpack encore config which transforms translations *.yaml files to json with 'js-yaml' and puts them into assets directory:这是 webpack encore 配置的一部分,它使用“js-yaml”将翻译 *.yaml 文件转换为 json 并将它们放入资产目录:

.addPlugin(new CopyWebpackPlugin(
    {
        patterns: [
            {
                from: './translations/*.yaml',
                to: './[name].json',
                transform(content) {
                    return Buffer.from(
                        JSON.stringify(
                            yaml.safeLoad(content.toString('utf8'), {schema: yaml.JSON_SCHEMA})
                        ),
                        'utf8'
                    )
                }
            }
        ],
    }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM