Symfony2 Translation using trans_default_domain - php

I'd like to translate a part of my twig template which is not in my trans_default_domain
For exemple:
{% trans_default_domain 'FOSUserBundle' %}
{{ 'test.trans'|trans({}, 'ProjectMainBundle') }}
So test.trans is translated in ProjectMainBundle but I always have test.trans in my text.
Edit:
test.trans is in src/Project/MainBundle/Resources/translations/messages.en
It works everywhere but it doesn't work when I am trying to get my trans with a trans_default_domain

You are storing the translation in a file called messages.en.yml which means according to the naming conventions for translations these translations have the domain messages and not ProjectMainBundle.
Therefore the translator doesn't find a translation if you're trying to use the domain ProjectMainBundle and returns the string itself.
Each message file must be named according to the following path:
domain.locale.loader
Your translations should be stored in #AcmeYourBundle/Resources/translations/<domain>.<locale>.yml ( or php, xliff, ... ).
Remember to clear your cache after renaming.

Related

Automatically add translate tags to Twig for Symfony translate component

I'm working on the i18n of my website and all our templates are just plain old English. I'm look for a tool (Google didn't help) that can make adding translation tags semi-automatic.
So for example from an input template:
Hello {{ name }}
Log out
It would make:
{% trans with {'%name%': name} from 'app' %}sayhello %name%{% endtrans %}
{% trans %}logout_text{% endtrans %}
And YML:
sayhello %name%: Hello %name%
logout_text: Log out
Since someone needs to add the placeholder names for the translation, I imagine this would be some interactive program parsing Twig templates in the project.
Are you aware of anything like that?

Symfony, how to check if strings are translated?

I would like to return a specific string each time a string is used in {% trans %}.
Exemple :
{% trans from 'base' %}home.title{% endtrans %}
Returns : "Home"
I would like it to return "XXX" and to automatically do it.
I used to do this in some pure PHP projects but I just don't know how to do it in Symfony.
Its unclear how you decide XXX. Whatever you did with pure PHP is doable with twig by means for twig filters. you can do something like home.title|trans_base and implement trans_base filter. See document under Filters section here

How to translate labels on a bundle

I made my own file with the translations of the bundle fields in my own language. It's working on my FOSUserBundle I created another one for comments bundle but not in FOSUserComment. It's on Translation folder.
Exemple code:
<button data-url="{{ url("fos_comment_remove_thread_comment", {"id": comment.thread.id, "commentId": comment.id, "value": constant('FOS\\CommentBundle\\Model\\CommentInterface::STATE_DELETED')}) }}" class="fos_comment_comment_remove">
{% trans from 'FOSCommentBundle' %}fos_comment_comment_delete{% endtrans %}
</button>
Can I replace {% trans from 'FOSCommentBundle' %}fos_comment_comment_delete{% endtrans %} to work with my translation file?
The translation works fine with defined languages but i copy a file translation and implement my language cause didn't exist on the bundle.
I am not sure what you did there. Can you tell where do you store the files and what names you gave to the translation files?
you can also use {{ 'fos_comment_delete'|trans }}
Update your question so I can update my answer.

Twig "trans with" not working

I'm using the twig trans tag in my templates, and I want to pass variables in it like so:
{% trans with {
'%link_start%': '<a href="http://www.google.nl/">',
'%link_end%': '</a>'
} %}
This %link_start%disclaimer%link_end% applies to all of our messages.
{% endtrans %}
But this gives me the following exception which points to the twig template at the {% trans with line:
PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "punctuation" of value "{" ("end of statement block" expected).' in [twig-template:line]
Even when I copy & paste the examples from the Symfony documentation I get the same exception. So I'm at a loss, what am I doing wrong here?
FYI: I'm using Twig 1.33 with the i18n extension enabled (and I'm not using the Symfony framework)
Twig doesn't support trans with out of the box. It is part of the Symfony translation extension. That explains why even the official Symfony documentation doesn't work - you are not using Symfony.
See this issue: https://github.com/twigphp/Twig-extensions/issues/74. There is a pull request to support trans with, but it hasn't been merged.
You might want to use the Symfony Translation Component in your application. You can use Symfony Components in your application, even without using the full Symfony (Framework) stack.
I haven't tried it, but you can try to use jhogervorst/Twi18n instead.
As a workaround you can use the filter tag with replace.
{% filter replace({'%foo%': 'blue', '%bar%': 'red'}) %}
{% trans %}
I like %foo% and %bar% messages.
{% endtrans %}
{% endfilter %}

Symfony2 Twig Inject additional blocks

Is there a way with Symfony2 and Twig to always make files available which just contain blocks.
For example, if I wanted to make a block named 'cookie' always available in any template in my system, without having to always include or extend it in the template I'm using.
Any ideas?
Clarification
Say I have a generic block which can do something, like:
{% block myBlock %}
ABC Examples
{% endblock %}
I have a class which knows it wants to be rendered with this block. My template itself doesn't necessarily know this though.
{{ block(myObj.blockName) }}
I would then like to have it so my controller/services/etc. could register the file which contains that block, without my template actually needing to know about it directly (so I could have multiple files like that, each working with some common interface).
Similar to registering custom Twig functions with a TwigExtension. My template doesn't need to explicitly know it's there, it just has to be available at run-time.
Does that make sense?
To clarify a bit further, I'm essentially looking to do something just like how there are default Twig blocks for rendering Forms in Symfony2. I don't have to include the default form file every time, just when I want to change something.
I went digging around in the Symfony source code to try and find my answer. It looks like there isn't some fancy, neat way to embed it from a configuration file or controller directly, which is a bit disappointing, but not a huge deal.
So, to solve my situation, I'll be using the "use" keyword to include my block file in my base template, so it will then be available to everything else.
{# widget_blocks.html.twig #}
{# Widgets #}
{% block my_widget %}
ABC Cookies
{% endblock %}
{# base.html.twig #}
{% use widget_blocks.html.twig %}
{{ block(my_widget.block) }}
Not exactly what I wanted, but sufficiently close.
{% render 'MyMainBundle:Default:credits' with {'arg1': $myObj } %}
Thats what I say. What's the difference between the line above or
{{ block(myObj.blockName) }}
You can register a custom filter but as far as I know it returns only string value. http://twig.sensiolabs.org/doc/advanced.html#id2

Categories