Bazinga js转换器返回翻译键而不是翻译

时间:2018-09-27 12:13:25

标签: javascript symfony webpack translation webpack-encore

简介

我正在将Symfony v4.1Bazinga js translator捆绑包一起使用。

我已经按照官方文档中的槽设置进行了操作,但无济于事。

我搜索了互联网,但没有找到解决我问题的方法。

问题

无法用JavaScript获取翻译-我从Bazinga js translator包中获得的全部是translation keys。然而Yarn可以毫无错误地编译资产!

我想念什么?

CODE

  

base.html.twig

<!DOCTYPE html>
<html lang="{{ app.request.locale|split('_')[0] }}">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{% block title %}BASE{% endblock %}</title>
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        {% block stylesheets %}
            <link rel="stylesheet" type="text/css" href="{{ asset('build/global.css') }}" />
        {% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('build/static/favicon_zen_1.ico') }}" />
        <link rel="shortcut icon" type="image/x-icon" href="{{ asset('build/static/favicon_zen_1.ico') }}" />
    </head>
    <body>
        {% block body %}
            <div class="box-primary" style="margin-top:12px;">
                <div id="main-holder">
                    <div id="main-content">
                        {% block main_content %}
                            CONTENT
                        {% endblock %}
                    </div>
                </div>
            </div>
        {% endblock %}
        {% block javascripts %}
            <script src="{{ asset('bundles/bazingajstranslation/js/translator.min.js') }}"></script>
            <script src="{{ url('bazinga_jstranslation_js') }}"></script>

            <script type="text/javascript" src="{{ asset('build/app.js') }}"></script>
        {% endblock %}
    </body>
</html>
  

test.html.twig

{% extends 'base.html.twig' %}

{% trans_default_domain 'upload' %}

{% block stylesheets %}
    {{ parent() }}

    <link rel="stylesheet" type="text/css" href="{{ asset('build/global.css') }}" />
{% endblock %}

{% block title %}JavaScript translation test{% endblock %}

{% block main_content %}
    <div id="language-changer" style="margin-bottom:1rem;">
        <a href="{{ path(app.request.get('_route'), app.request.get('_route_params') | merge({'_locale': 'en'})) }}">EN</a> /
        <a href="{{ path(app.request.get('_route'), app.request.get('_route_params') | merge({'_locale': 'ru'})) }}">RU</a>
    </div>

    <div id="test">
        TEST
    </div>
{% endblock %}

{% block javascripts %}
    {{ parent() }}

    <script src="{{ asset('build/test.js') }}"></script>
{% endblock %}
  

test.js:

'use strict';

import Translator from 'bazinga-translator';

(function(window)
{
    let item;

    item = Translator.trans('upload.status.uploadFailed', {}, 'upload');

    console.log(item);

})(window);
  

webpack.config.js

// webpack.config.js
const Encore = require('@symfony/webpack-encore');
const path = require('path');

const CopyWebpackPlugin = require('copy-webpack-plugin');

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    // only needed for CDN's or sub-directory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
     */
    .addEntry('app', './assets/js/app.js')
    .addEntry('blueimp', './assets/js/blueimp.js')
    .addEntry('test', './assets/js/test.js')

    .addStyleEntry('global', './assets/css/global.scss')

    .addPlugin(new CopyWebpackPlugin([
        // copies to {output}/static
        { from: './assets/static', to: 'static' }
    ]))

    // allow sass/scss files to be processed
    .enableSassLoader(function(sassOptions) {},
        {
            resolveUrlLoader: false
        }
    )

    .autoProvideVariables({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery'
    })

    /*
     * FEATURE CONFIG
     *
     * Enable & configure other features below. For a full
     * list of features, see:
     * https://symfony.com/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())
;

/*
module.exports = Encore.getWebpackConfig();
module.exports.externals = [{
    'bazinga-translator': 'Translator'
}];
*/

const config = Encore.getWebpackConfig();

config.externals = {
    'bazinga-translator': 'Translator'
};

config.resolve.alias = {
    'jquery-ui/ui/widget': path.resolve(__dirname, 'node_modules/jquery.ui.widget/jquery.ui.widget.js')
};

// export the final configuration
module.exports = config;
  

bazinga_js_translation.yaml:

bazinga_js_translation:    
    locale_fallback: en

谢谢

谢谢您的评论和回答。

已用资源

  1. Bazinga Js Translator Bundle documentation
  2. Translator is not defined when using Webpack Encore in github issues
  3. Issue using willdurand/BazingaJsTranslationBundle

更新1

感谢@stof for spotting the cause of a problem。我正在使用两种单独的方式同时包含translator

其中一种解决方案(ES5 oneadded to SO,但我更喜欢完整的ES2015解决方案...

谢谢您的回答和想法。

1 个答案:

答案 0 :(得分:0)

如果您希望/需要使用old school JavaScript并包含在每个页面中,则此解决方案有效。

必须提供翻译域。有了翻译就可以了。

<script src="{{ url('bazinga_jstranslation_js', { 'domain': 'TRANSLATION_DOMAIN_NAME', 'locales': 'en,fr,de' }) }}"></script>