Symfony 6 : how to import bundle translations? - php

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'

Related

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.

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

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)

Symfony Translator is not generating cache message catalogue for default locale?

I am trying to run a Symfony 2.6.4 application on the Google AppEngine which is translated to 5 languages.
Problem is that cache:clear command generates only message catalogues for all languages except default locale (sk) and then it tries to generate missing message catalogue on the fly. Problem is that Google AppEngine is read-only. Can anybody explain why it isn't generated on cache:clear in warmup stage?
app.yml
framework:
default_locale: "sk"
translator:
fallback: "sk"
jms_i18n_routing:
default_locale: sk
locales: [cs, en, de, sk, ru]
strategy: prefix_except_default
Exception:
An exception has been thrown during the rendering of a template ("Cannot rename "vfs://root/temp/.../app/cache/prod/translations/catalogue.sk.php54e9b262334c93.83518975" to "/.../app/cache/prod/translations/catalogue.sk.php".") in base.html.twig at line 10.
Please, can anybody help me? I am trying to solve this for 3 days.

JmsI18NBundle - The directory where translations are must be set

When i run the command
php app/console translation:extract en --enable-extractor=jms_i18n_routing
i get the following error:
[JMS\TranslationBundle\Exception\InvalidArgumentException]
The directory where translations are must be set.
this is the bundle configuration:
jms_i18n_routing:
default_locale: it
locales: [it, en]
strategy: prefix
cookie:
enabled: false
What is wrong with that?
You can fix this by using the --dir option to define the directory where your translations are when running your translation:extractcommand.
From the documentation,
For dumping, the bundle provides you with a console command which you
can use to update your translation files, or also just to preview all
changes that have been made.
Updating Files:
php app/console translation:extract de --dir=./src/ --output-dir=./app/Resources/translations
i had to use the following command
php app/console translation:extract en --enable-extractor=jms_i18n_routing --bundle="AcmeFooBundle" --domain="routes"

Symfony2 locale detection and translation

Here is what I'm stuck on.
I'm working on Symfony2 and trying to make the translations working. But somehow, I can't make it work as it should. Here's what I got.
In the config.yml
translator: { fallback: de }
session:
default_locale: en
In messages.en.yml I have this structure:
Please log in: Bitte melden Sie sich an
I also have similar files for messages.en_US.yml, messages.ru.yml, etc.
In the code I use $this->get('session')->setLocale('ru_RU'); to change the locale.
But the translation works only when the locale is set to "en_US" or "de_DE". In any other case it returns the key, not the value. I try the translation with this code
return new Response($this->get('translator')->trans('Please log in').' '.$this->get('session')->getLocale()); and it returns the locale I have set.
So what can cause this problem?
you should clear the cache by using the symfony comand
app/console cache:clear --no-debug
then eventually restore write permission on cache/ and log/ folders as described here
I found the answer myself. Just needed to clean up the cache for Symfony. Used console to do so. Changed directory to myProject/app and used this commands to clean the cache and logs:
sudo rm -R cache/
sudo rm -R logs/

Categories