I am using Twig for my project as an Template engine.
The thing i want to achieve is Translate name of the months. For instance 30 December to 30 Декабрь
Project wasn't built upon symfony or any other framework. It is just project built upon PHP7 with MVC pattern.
Maybe you can use the Twig Extensions library, which provides several useful extensions for Twig: https://twig-extensions.readthedocs.io/en/latest/.
It is developed by the same people who built the Twig template engine.
You can use the i18n Extension (https://twig-extensions.readthedocs.io/en/latest/i18n.html), then, in your code, you'll use the trans block to mark parts in the template as translatable:
{% trans "Hello World!" %}
{% trans string_var %}
{% trans %}
Hello World!
{% endtrans %}
{% set name = object.name_property %}
{% trans %}
Hello {{ name }}!
{% endtrans %}
If you just want a quick solution for translation of month names and you don't want to overload your project with other dependecies, you can create a simple Twig filter, as described here: https://twig.symfony.com/doc/2.x/advanced.html#filters.
$filter = new Twig_Filter('trans', function ($string) {
// $string is the month name...
// return the translated string
return ... ;
});
$twig = new Twig_Environment($loader);
$twig->addFilter($filter);
Then use it in your template:
{{ 'December'|trans }}
Related
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?
I'm building a multisite eZ Platforme installation and I need to specify a main layout for my templates.
Right now I have a template article.html.twig :
{% extends "main_layout.html.twig" %}
{% block content %}
<h1>{{ ez_render_field(content, 'body') }}</h1>
{% endblock %}
what I want to do is something like this :
{% if(siteaccess = "site1"){
extends "site1_main_layout.html.twig"
}
else if(siteaccess = "site2"){
extends "site1_main_layout.html.twig"
}
%}
Please help me!
You can just configure the layout in the config:
ezpublish:
system:
site1:
pagelayout: "tpl1.html.twig"
site2:
pagelayout: "tpl2.html.twig"
After that, you can just use the following in your full view:
{% extends pagelayout %}
{% block content %}
...
{% endblock %}
pagelayout is a variable prepopulated by eZ Platform from the above config based on current siteaccess. It requires eZ Platform 1.2 at the least, I believe.
It should also be noted that pagelayout variable is available only in full view templates. Other templates wishing to use the configured pagelayout must use the following:
{% extends ezpublish.configResolver.parameter('pagelayout') %}
Correct me if I misunderstood your goal, but do you recon it could be sorted by checking the domains? (I am assuming they would be different so could serve as a separator):
{% if app.request.baseUrl == 'site1' %}
...
{% else %}
...
{% endif %}
If I am not wrong, I also belive you can create a default Twig Controller Loader to decide this beforehand instead of leaving the logic to your views :)
Have a look here https://doc.ezplatform.com/en/latest/guide/design_engine/
you can use the Design engine where you can set up different themes with fallback.
Define one base theme an add per siteaccess one extra theme where you can override any template.
I have two bundles I created by my own: one to generate admin sections (AdminBundle), and another one to create bills (BillingBundle). BillingBundle has basically 2 entities: Client and Bill (1:m).
If I install both bundles to generate the new/edit forms and lists for Client and Bill at the admin, I include a part at config.yml like this:
my_admin_bundle:
entities:
project: #whatever entity. Project at AppBundle for example
new: true
list: true
client:
new: true
list: true
bill:
new: true
list: true
On the other hand, on AdminBundle I have a template called form.html.twig which renders the form to create new elements. Basically it is just like this:
{{ form(form) }}
The doubt: since the form to create new Bills need a .js file (accounting.js), how to include it only for new Bill form but not for new Client or Project forms ?
As you can imagine, now I have this in form.html.twig:
{{ form(form) }}
<script src="/bundles/ziiwebbilling/js/accounting.js" type="text/javascript"></script>
but I don't need accounting.js at form.html.twig for new Projects, or new Clients forms.
NOTE: sorry if I extend too much with my explanation, but I don't know how to explain it in another way. Even the title of the question is awful, please edit it if you find something better.
EDIT: as I've just wrote at the first comment for jkucharovic's answer, what I'm really looking for to solve my problem, is a non-intrusive way, that is: I wouldn't like to add the line about accounting.js inside an AdminBundle template. Or maybe am I looking for "too much"?
Use custom Twig extenstion with test function like this one:
public function getTests()
{
return [
new \Twig_SimpleTest('bill', function ($var) {
return $var instanceof Bill;
})
];
}
And then in template, you can simply test if form is for Bill entity:
{{ form(form) }}
{% if form.vars.value is bill %}
<script src="/bundles/ziiwebbilling/js/accounting.js"></script>
{% endif %}
Edit: If you don't want to modify template inside bundle for certain reason, you can make loading application-wide by defining own template and overriding default form block:
{%- block form -%}
{% if form.vars.value is bill %}
<script src="/bundles/ziiwebbilling/js/accounting.js"></script>
{% endif %}
{{ form_start(form) }}
{{- form_widget(form) -}}
{{ form_end(form) }}
{%- endblock form -%}
I need to load a page, that will be "inserted" in a template - as I read it, Volt's Template Inheritance should do the trick and it does... kinda. Hardcoded values, as shown in the examples, work fine - the following example works:
<!-- Template -->
<div id="site_content">
{% block test %}
{% endblock %}
</div>
and the page, that inherits the template:
{% extends "../../templates/de/index.volt" %}
{% block test %}
{{ content() }} {# this is a registered volt function that outputs the generated content #}
{% endblock %}
However, the same page might need to inherit a different template and that must be decided on runtime, so the name of the template must be generated dynamically. Two options occurred to me:
Set the template name to a variable and use it when extending - the problem here is that I don't see a way to use it afterwards. That guy seems to have had the same problem, but there is neither an answer of how to do it, nor a confirmation that it isn't possible at all.
Register another function to generate the complete string (e.g. {% extends "../../templates/de/index.volt" %}) and then compile it, e.g.
$compiler->addFunction('get_template',
function ($resolvedArgs, $exprArgs) use ($volt) {
return $volt->getCompiler()
->compileString('{% extends "../../templates/de/index.volt" %}');
});
and then use that function in the page, e.g.
{{ get_template() }}
{% block test %}
{{ content() }}
{% endblock %}
However, using that approach does not parse the page content (e.g. the content returned by the registered content() function is not shown). I'm also open to other solutions (using Twig instead of Volt is only a last resort, for performance issues), advices of what I'm doing wrong or pointers of useful articles on the topic. Thanks in advance!
Try using partials as documented in the Phalcon doc: Using Partials
When I was doing plain PHP, I was simply doing this:
printf(_("Hello %s !"), $name);
Now with Twig, I must use the trans tag. So I've copy/paste the documentation example, and here's my full template:
{% extends 'MyAppBundle::layout.html.twig' %}
{% block content %}
<h1>
{% trans %}
Hello {{ name }}!
{% endtrans %}
</h1>
{% endblock %}
Why Symfony return the following exeption ?
A message must be a simple text in "MyAppBundle::home.html.twig"
500 Internal Server Error - Twig_Error_Syntax
One missing bit with the previous answer is the "with" portion that is needed to do the replacement of the variable part of the message.
{% trans with {'%name%':name} %}Hello %name%!{% endtrans %}
The precise syntax for translations is a little different in Symfony2 than it is in standalone Twig. You'll want to check out the Symfony2 documentation for translations in twig templates, found here. The correct syntax would look something like this:
{% trans %}Hello %name%!{% endtrans %}
I have a similar issue: to pass my translation path to trans filter, I need to concatenate a string and a variable, then transform into lowercase.
Here {% trans %} and {% endtrans %} are not used, but trans filter instead:
<span>{{ ('statuses.' ~ status | lower) | trans }}</span>
Assuming in the translation there is:
- status:
- failed: The task has failed
and in the template you pass the variable name with value FAILED.