Boris
Boris

Reputation: 166

Translations in JS files with Symfony2

I'm currently developing an applications that will need to use translations from my JavaScripts.

The bundle BazingaJsTranslationBundle seems to be good, but after I tried it I don't think it fits my needs. It generates all the translations for all my application's bundles. It can be heavy to load.

Do you know other bundles or tricks for that ?

Thank you for your help.

Upvotes: 1

Views: 3473

Answers (2)

poh
poh

Reputation: 171

I had similar problem (large translation files) with BazingaJsTranslationBundle and I simplify this by:

#config.yml
bazinga_expose_translation:
    default_domains: [ jsonly ]
    locale_fallback: "%locale%"

create simple html twig to store your js variables and bazing expose them from this files

{# jsOnleVariables.html.twig #}
{% set var1 = 'Welcome'|trans({},'jsonly') %}
{% set var2 = 'Bye'|trans({},'jsonly') %}

dump variables

php app/console bazinga:expose-translation:dump web/js

and in your layout include only wanted variables

{# layout.html.twig #}
<script src="{{ asset('bundles/bazingaexposetranslation/js/translator.js') }}" type="text/javascript"></script>

{% if app.request.locale == 'pl' %}
<script src="{{ asset('js/i18n/jsonly/pl.js') }}" type="text/javascript"></script>
{% else %}
<script src="{{ asset('js/i18n/jsonly/en.js') }}" type="text/javascript"></script>
{% endif %}

Upvotes: 2

csjpeter
csjpeter

Reputation: 1946

Depending on your exact needs and circumstances, a simple object in json format can be a good enough dictionary. For example like this:

var dict = {
    TextIdWelcome : "Welcome",
    TextIdGoodBy : "Good by"
}

A usage example:

var elem = document.getElementById("WelcomeTag");
elem.innerHtml = dict["TextIdWelcome"];

You can generate such json object on your server side for the actual language selected on client side and you can retrieve it by your favourite method (jquery, XMLHttpRequest, etc), and just assign it to this dict variable.

If you need sentences with runtime dependent values in it, a simple basic trick might fit your needs. Let use some markup like %0, %1, etc in the translated text.

var dict = {
    TextIdWelcome : "Welcome %0",
    TextIdGoodBy : "Good by %0"
}

function tr(textId, runTimeValue1, runTimeValue2){
    var text = dict[textId];
    if(runTimeValue1 !== undefined)
        text = text.replace("%0", runTimeValue1);
    if(runTimeValue2 !== undefined)
        text = text.replace("%1", runTimeValue2);
    return text;
}

So a usage example:

var userName = "Jhon";
var elem = document.getElementById("WelcomeTag");
elem.innerHtml = tr("TextIdWelcome", userName);

Please note that this solution lacks several refined tricks (escaping markups, variable number of runtime values, efficient replace algorithm, etc), but in simple everyday cases this could be good enough. This trick is also very simple (so you can easily enhance it to your needs) and you can control 100% how exactly it should handle the dictionary. You can of course control when and what dictionary to load into the dict variable.

Upvotes: 0

Related Questions