Symfony 2 setting up translation files - symfony

I am failing to setup translation for my Symfony 2 project. I have manually created a folder inside app\Resources\translations\message.en.yml and it's content:
base:
title:
homePage: TeamERP IMS for BA
Then on the base twig template inside my bundle I am trying to call it:
<title>
{% block title %}
{{ base.title.homePage|trans }}
{% endblock %}
</title>
Then on the config.yml I has the following:
framework:
translator: { fallbacks: en }
I am getting this error:
Variable "base" does not exist in TeamERPBaseBundle::base.html.twig at line 7
What am I doing wrong?
Edit: after fixing the problem here:
{{ 'base.title.homePage'|trans }}
I stopped getting the error, thanks for that. now the page is not giveng the error but not loading the page with the warning in the logs fine:
[2015-05-01 12:42:57] translation.WARNING: Translation not found. {"id":"base.title.homePage","domain":"messages","locale":"en"} []
[2015-05-01 12:42:57] translation.WARNING: Translation not found. {"id":"Home","domain":"messages","locale":"en"} []
Edit2: There was some kind of problem with my version of symfony 2.6. I just did a composer update due to this, and it started working. normaly.

First of all it should be messages.en.yml as indicated by #xurshid29, but most important it should be
<title>
{% block title %}
{{ 'base.title.homePage'|trans }}
{% endblock %}
</title>
inside the template. The value passed to the trans filter must be a string but base.title.homePage|trans would be expanded to something similar to $base->getTitle()->getHomepage() because it's a Twig variable syntax. That's why you're getting the error message Variable "base" does not exist.

Rename message.en.yml to messages.en.yml, it should work.

The format of your yaml file is imho not correct and the translation in twig is wrong.
message.en.yml
base.title.homePage: TeamERP IMS for BA
your.twig.html
<title>
{% block title %}
{{ 'base.title.homePage' | trans }}
{% endblock %}
</title>
You can use the Translation component as you like, but better is to write down correct sentences in your main language (TeamERP IMS for BA) and translate them. Think of giving a translation file away to native speaker who should translate it:
message.de.yml
TeamERP IMS for BA: TeamERP IMS für BA
your.twig.html
<title>
{% block title %}
{% trans %}TeamERP IMS for BA{% endtrans %}
{% endblock %}
</title>
And of course check the domains {% trans_default_domain "message" %}

Related

Use a template to set a variable

I'm using craftcms and the template language they use is Twig.
The _layout/base.html:
<!DOCTYPE html>
<html>
<head>
{% include '_layout/_seo_default.html %}
</head>
_layout/_seo_default.html:
{% if seo is not defined %}
{% set seo = {title: "Default values for things", description:"Default Description"} %}
{% endif %}
<title>{{ seo.title }}</title>
<meta name="description" content="{{ seo.description }}">
I have a blog/_entry template which shows an entry from the CMS based on the url. The blog/_entry.html:
{% extends '_layout/base.html' %}
{% block main %}
{# i want this include to set a variable used in _seo_default.html #}
{% include '_seo/_from_article_type_entry.html' with {entry: entry} %}
<article>
html irrelevant to question
</article>
{% endblock %}
The _seo/_from_article_type_entry.html
{% set seo = { title: entry.title, description: entry.short_description } %}
The idea was that i would be able to map the fields to the correct keys in one template / at one place. So i don't have to reuse it for the news/blog/story templates the client wants. But the 'seo' variable set in _seo/_from_article_type_entry.html does not get set (either not at all, or not in time that the _layout/_seo_default.html picks it up, and default values are always used.
If i replace the {% include '_seo/_from_article_type_entry.html' with {entry: entry} %} line in blog/_entry.html with the contents of _seo/_from_article_type_entry.html it does work, so it just doesn't seem to get set in the include. But I can't figure out what i'm missing. Or maybe i'm trying to do something that Twig is not supposed to do.
In either case, any help would be very welcome :)
Included templates have their own variable scope, templates outside the included one can't access these variables as seen here

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Symfony2: Custom Error Page Extend base.html.twig

I am trying to customize the error pages in Symfony.
This is my error.html.twig file located in app/Resources/TwigBundle/views/Exception/:
{% extends '::base.html.twig' %}
{% block body %}
<h1>{{ status_code }}: {{ status_text }}</h1>
{% endblock %}
Unfortunately I get the following error message:
Fatal error: Uncaught exception
'Symfony\Component\Routing\Exception\ResourceNotFoundException' in
[...]
vendor\symfony\symfony\src\Symfony\Component\HttpKernel\EventListener\RouterListener.php
on line 144
When I remove {% extends '::base.html.twig' %} everything works fine. Any ideas how to have my base template included in the error page?
Edit 1:
The strange thing is that it seems to work when a 403 is thrown, e.g., when I access /user but don't have the necessary privilege.
Edit 2:
I pasted the whole content of my base.html.twig into the error.html.twig file and noticed that the error was caused due to the menu rendered by the KnpMenuBundle bundle:
{{ knp_menu_render('ACMEMemberBundle:Builder:mainMenu', { 'style': 'pills', 'currentClass': 'active' }) }}
When I remove this line, everything works fine. But this is not the way I would like to go. Is there no possibility to keep the navigation?
file should be located in app/Resources/views/Exception/
instead of
app/Resources/TwigBundle/views/Exception/
Did you put the page in the following place?
/app/ResourceS/TwigBundle/views/Exception/error404.html.twig
{% extends '::base.html.twig' %}
{% block content %}
{%trans%}errors.404{%endtrans%}
{% endblock %}
// app/Resouces/views/base.html.twig
×
{% include('path/to/include') %}
○
{% include('::path/to/include') %}
I finally have done it putting the whole code (base+current) in the same 'error.html.twig' file.
It works for me and avoided a huge headache.
plz remove :: in first line https://symfony.com/doc/current/templating.html
{% extends 'base.html.twig' %}
{% block body %}
<h1>{{ status_code }}: {{ status_text }}</h1>
{% endblock %}

Use Twig Custom Set Variables From an Include

I'm trying to include a twig file with a bunch of custom set variables and then use the variables in the multiple other template files. Similar to how including a PHP file works.
I don't seem to have access to the variables set inside the include in my index file.
Is there any way to do this?
Sample Code *Edited
Included File:
{# variables.html #}
{% set width = "100" %}
{% set height = "250" %}
Template File:
{# index.html #}
{% include 'variables.html' %}
{{ width }}
{{ height }}
Expected Outcome:
100 250
Actual Outcome:
// Nothing gets output
I was just trying to do the same thing you were and came up with the following:
Created snippets.twig to maintain all these mini variables. In your case, you might call it variables.twig. In this file, I used a macro without any arguments. I was creating formatted entry date markup that I can use across all my templates and it looked like this:
{% macro entry_date() %}
<time datetime="{{post.post_date|date('m-d-Y')}}">{{post.post_date|date('F j, Y')}}</time>
{% endmacro %}
note that the parenthesis after the name declaration were imperative
In my main layout file, layout.twig, I referenced this macro via an import statement so it would be accessible in all child templates:
{% import "snippets.twig" as snippets %}
<!doctype html>
...
In my template files, I now have snippets accessible and can query it like any other variable:
{{ snippets.entry_date }}
UPDATE
This doesn't seem to correctly run code. If you're just storing static content, you should be good. You can also pass args to the macro so I imagine you could make some magic happen there but I haven't tried it.
As far as I know it is only possible with {% extends %} tag. Instead of including template with variables you should extend it.
Example:
variables.tpl:
{% set some_variable='123' %}
... more variables ...
{% block content %}
{% endblock %}
template.tpl
{% extends 'variables.tpl' %}
{% block content %}
{{ some_variable }}
... more code which uses variables assigned in variables.tpl ...
{% endblock %}
You can use a template extension : https://symfony.com/doc/current/templating/twig_extension.html
post|entry_date

Translate messages inside twig in symfony2

I'm trying to access to the translations via twig.
For example, I have the name of my application inside my Resources/translations/messages.de.yml and Resources/translations/messages.en.yml
My controller does only a render of the twig file.
And inside my twig-file I want to access to the application.name property which is defined inside the messages-file (yml)
How can I access to this property to get the application name (let's say it contains some language-specific information)
I tried these methods, and failed:
{{ application.name }}
Looks more like for variables which have been sent through the controller, I've got an error, that the variable 'application' was not found
{% trans% } application.name {% endtrans %}
displays application.name
{% trans% } 'application.name' {% endtrans %}
displays 'application.name'
With inline notation you should use filter:
{{ 'application.name'|trans }}
With trans tag I think problem in whitespaces around application.name
{% trans% }app.name{% endtrans %}
In your messages.en.yml
<trans-unit id="app.name" resname="app.name">
<source>My app</source>
<target>My app</target>
</trans-unit>
In your messages.de.yml
<trans-unit id="app.name" resname="app.name">
<source>My app</source>
<target>Meine App</target>
</trans-unit>

Resources