How to use Gettext (.mo/.po) files with Symfony2? - php

I want to use Gettext for the translation of my website on Symfony2
In ../Resources/translations/ i have my translations files like that :
translations/en/LC_MESSAGES/en.mo
translations/en/LC_MESSAGES/en.po
translations/fr/LC_MESSAGES/fr.mo
translations/fr/LC_MESSAGES/fr.po
...
I already configured the default local variable with the help of Symfony2 cookbook to be french(fr) http://symfony.com/doc/current/book/translation.html#the-locale-and-the-url
When i go to app_dev.php/fr/hello/test, my Hello World is in english. There is something else i need to configure?
Already tried this configuration : Configure Translation component in Symfony 2 to use get text

Using Gettext (.mo/.po) files with Symfony is very simple and does not require any additional bundle.
The following is an example about how to use *.po files for translations.
First add the default_locale parameter in parameters.yml:
parameters:
# ...
default_locale: en_US
# ...
Then enable the translator in config.yml:
framework:
# ...
translator:
enabled: true
fallbacks: ['%default_locale%']
paths:
- '%kernel.root_dir%/Resources/translations'
# ...
And add the service to the services.yml file:
translation.loader.po:
class: Symfony\Component\Translation\Loader\PoFileLoader
tags:
- { name: translation.loader, alias: po }
Place your mo/po translation files under app/Resources/translations using the following structure:
app/Resources/translations/messages.en.po
app/Resources/translations/messages.it.po
...
Now from your controllers you can call:
$this->get('translator')->trans('my.message.id');

After few days of research, I finally found an answer. It's not the one that i was searching for, but it's a very good alternative.
I found this Awesome Bundle : https://github.com/LeaseWeb/LswGettextTranslationBundle
Very easy to put in place, i recommend you to use "routes" to change locales like that :
prefix: /{_locale}/
requirements:
_locale: |fr|en|es|pt #All your locales "shortcuts"
Be sur to configure the Bundle to use locales shortcuts in Lsw/GettextTranslationBundle/Resources/config/config.yml like that :
parameters:
gettext.locale_shortcuts:
en: en_US
fr: fr_FR
pt: pt_PT
es: es_ES
For all configurations, use the step by step bundle configurations (easy to use)

Related

Symfony 6 : how to import bundle translations?

I'm using Symfony 6 and don't understand how I should enable translation for dependency messages.
For example: I just installed SymfonyCasts/verify-email-bundle which provides translations in its directory (src/Resources/translations)
To enable them, I have:
installed the translation bundle with: composer require symfony/translation
set the default_locale to fr in my config/packages/translation.yaml
cleared the cache with bin/console cache:clear
also tried to manually clear translation cache as stated in other related posts : rm -rf var/log/translations
Then, all messages that should be handled by the provided translations are still in English.
I have also tried to force translation by calling myself the $translator->trans() method on the string returned by the bundle. The profiler then says the translation is missing and fallbacks to en as configured.
I have tried to copy the bundle VerifyEmailBundle.fr.xlf file into my own /translations directory but got the same error. bin/console debug:translation fr shows me the needed translations but all are marked as unused.
I encounter the same issue with multiple bundles and don't see anything in the offical documentation about this.
What am I missing?
To set default language you must set #config/packages/translation.yaml like this:
framework:
default_locale: fr
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- fr
and you can add translations at #translations/messages.fr.yml
Hello: 'Bonjour'
This friendly message is coming from: 'Ce message amical vient de'
Welcome to my new App: 'Bienvenue sur ma nouvelle application'
user:
profile:
admin: 'administrateur'

Symfony2 error: The service "templating" has a dependency on a non-existent service "templating.globals"

I have just installed a bundle using the following command to activate Mustache templates in my project but now I get the following error;
The service "templating" has a dependency on a non-existent service "templating.globals".
The command I used was:
composer.phar require bobthecow/mustache-bundle
Of course I know I am missing the templating.globals service but How do I include this and why is it not included automatically?
The templating.globals service is only created by the symfony/framework-bundle if you have php in the list on templating engines.
As you can see in the Resources/config/templating_php.xml the service is created.
This file is only loaded in the extension if php is in the list of templating engines.
TL;DR Add php to the templating engines in your app/config/config.yml.
# app/config/config.yml
framework:
# ...
templating:
engines: ['twig', 'php']
If you are running Symfony 4.x then You've to add the following to the config/packages/framework.yaml:
framework:
# ... other stuff ...
templating:
engines: ['twig', 'php']

jms translation bundle: The format "yml~" does not exist.

In symfony2, with JMSTranslationBundle, when running
php app/console translation:extract fr --config=app
I get the issue
[JMS\TranslationBundle\Exception\InvalidArgumentException]
The format "yml~" does not exist.
my config being
jms_translation:
configs:
app:
dirs: [%kernel.root_dir%, %kernel.root_dir%/../src]
output_dir: %kernel.root_dir%/Resources/translations
ignored_domains: [routes]
excluded_names: ["*TestCase.php", "*Test.php"]
excluded_dirs: [cache, data, logs]
# extractors: [jms_i18n_routing]
Any idea as how to solve this ?
The solution is to remove all *.yml~ files from your project.

Missing form.type_extension.csrf.enabled parameter with SonataAdminBundle

I'm following the SonataAdminBundle documentation, so I've installed the Bundles and added them to AppKernel put now I'm stuck on the configuration.
I've used the basic config but I keep getting this error:
The service "sonata.admin.builder.orm_datagrid" has a dependency on a non-existent parameter "form.type_extension.csrf.enabled".
I've no idea what is this parameter nor how to set it properly.
in your app/config/yml you should have a line like :
framework:
#csrf_protection: ~
be sure it is not commented :
framework:
csrf_protection: ~

Activate StringLoader Twig Extension in Symfony

I'm trying to active the Twig StringLoader Extension in a Symfony 2.3 project but just can't get the yaml-syntax right.
This post refers to the answer by Heyflynn on a post dealing with the exact same problem but providing a solution that just does not work (for me).
Writing the following lines in my app/config/config.yml just gives me the exception below:
# app/config/config.yml
acme.twig.extension.loader:
class: Twig_Extension_StringLoader
tags:
- { name: 'twig.extension' }
Gives me this:
FileLoaderLoadException: Cannot import resource ".../app/config/config.yml" from ".../app/config/config_dev.yml". (There is no extension able to load the configuration for "acme.twig.extension.loader" (in .../app/config/config.yml). Looked for namespace "acme.twig.extension.loader", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "apy_data_grid", "project", "acme_demo", "web_profiler", "sensio_distribution")
(I was already wondering about the acme appearence in acme.twig.extension.loader and replaced it with the name of the project bundle but that gives me the same exception again. Just cutting it off doesnt work either.)
Please help!
The Twig is not part of acme bundle, Twig is vendor bundle itself and therefore the Error is correct. There is not such namespace as acme.twig.extension.loader
The fixed code would be:
# services.yml
services:
twig.extension.stringloader:
class: Twig_Extension_StringLoader
tags:
- { name: twig.extension }
This can be added to /app/config/config.yml to use to in each bundle or add it into your bundle folder into /Resources/config/services.yml to use in only in certain bundle.
Then in twig templates use them as:
{{ include(template_from_string(page.template)) }}
The above works for me in Symfony v2.5

Categories