The LswGettextTranslationBundle adds gettext translation support to your Symfony4 application. It is aimed to be faster and more user-friendly than the built-in translation support of Symfony4.
Installation is broken down in the following steps:
Add LswGettextTranslationBundle in your composer.json:
{
"require": {
"leaseweb/gettext-translation-bundle": "*",
...
}
}
Now tell composer to download the bundle by running the command:
$ php composer.phar update leaseweb/gettext-translation-bundle
Composer will install the bundle to your project's vendor/leaseweb
directory.
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Lsw\GettextTranslationBundle\LswGettextTranslationBundle(),
);
}
As described here you can list the locales that you have installed on your (Linux) system with the command 'locale -a'. If you want to support Dutch, English and German you should execute the following commands:
sudo locale-gen nl_NL.UTF-8
sudo locale-gen en_US.UTF-8
sudo locale-gen de_DE.UTF-8
More language codes can be found here
Use the standard $request->setLocale('en');
to set the locale in your application.
Edit the following file to define 2 letter shortcuts for the locales (this is recommended):
Lsw/GettextTranslationBundle/Resources/config/config.yml
Usage is broken down in the following steps:
You can use the following functions:
_($text)
Shortcut for gettext_n($textSingular,$textPlural,$n)
Shortcut for ngettext__($format,$args,...)
Shortcut for sprintf(_($format),$args,...))
__n($formatSingular,$formatPlural,$n,$args,...)
Shortcut for sprintf(_n($formatSingular,$formatPlural,$n),$args,...))
Use the ./app/console gettext:bundle:extract
command to search a bundle for translation
strings and to store them into a gettext template (.pot) file.
Use the ./app/console gettext:bundle:initialize
command to copy the gettext template (.pot)
file into the language specific (.po) files.
Use the Poedit application to load a gettext language (.po) file. Choose the "Update from template" option and point Poedit to the generated gettext template (.pot) file. Review and confirm the changes.
Use the Poedit application to load a gettext language (.po) file. Translate all missing strings (shown in blue) and check and correct all fuzzy translated strings (shown in yellow).
Use the ./app/console gettext:combine
command combine all gettext language (.po) files into one
compiled gettext (.mo) file.