Internationalization

Hi,

I’m trying to use internationalization in my CakePHP(3.6) app. I have followed the official documentation.

I have created two po files :

  1. src/Locale/en/default.po
    msgid “accueil_main_title”
    msgstr “Welcomel”

  2. src/Locale/fr/default.po
    msgid “accueil_main_title”
    msgstr “Bienvenue”

After in a view I have written the following code :

< h1 class=“title-page text-center”><?= __('accueil_main_title')?>

I have modified the app.php file as follow :
‘App’ => [
‘namespace’ => ‘App’,
‘encoding’ => env(‘APP_ENCODING’, ‘UTF-8’),
‘defaultLocale’ => env(‘APP_DEFAULT_LOCALE’, ‘fr’),
‘defaultTimezone’ => env(‘APP_DEFAULT_TIMEZONE’, ‘UTC’),
‘base’ => false,
‘dir’ => ‘src’,
‘webroot’ => ‘webroot’,
‘wwwRoot’ => WWW_ROOT,
//‘baseUrl’ => env(‘SCRIPT_NAME’),
‘fullBaseUrl’ => false,
‘imageBaseUrl’ => ‘img/’,
‘cssBaseUrl’ => ‘css/’,
‘jsBaseUrl’ => ‘js/’,
‘paths’ => [
‘plugins’ => [ROOT . DS . ‘plugins’ . DS],
‘templates’ => [APP . ‘Template’ . DS],
‘locales’ => [APP . ‘Locale’ . DS],
],
],

And now when I run my app, I get on my view the text “accueil_main_title”. I expect to get “Bienvenue” or “Welcome”. What did I do wrong ?

Regards,

Snoopy

I would reccomend you to extract the translation strings by the cake console as described in the cookbook, and try with them.

I try with the command line bin/cake i18n extract. POT files are generated correctly in the folder /src/Locale/fr, but I still get 'accueil_main_title" on my view.

You should have a default.pot at /src/Locale, and default.po and default.mo in /src/Locale/fr. After this You should open your /src/Locale/fr/default.po file with Poedit, refresh it from your pot file, do a translation. Delete your cake cache, and after that it should work.

1 Like

Ho great !!! It’s working now !! :grinning:

Thanks you very much

Great :grinning:

I would reccomend you to do not use placeholder texts. Just pick up a base language and use it. So what I would do is use __(‘Welcome’) instead of __(‘accueil_main_title’).

By that you have a default language what does not require translation and that will be the fallback for all non translated texts. In this case English will go to your pot file.

I use placeholder because I want to translate a full paragraph and not only one word. For instance in my view I wrote something like this :

__(‘Accueil_paragraph_1’)

and then in the po file I have this:

msgid “Accueil_paragraph_1”
msgstr “Lorem ipsum dolor fse sit amet, consectetur adipiscing elit. Duis eu imperdiet lorem, non tincidunt nibh. Phasellus quis massa eget erat tempus interdum id ut sem. Nam tristique purus massa, et scelerisque urna rutru…”

Is it a bad approach ?

I would not do that. Why do not you put there the whole paragraph? From your placeholder nobody will know (exept you) what to put there as a translation. That is why we use a default language.

I try as you said and it is still working :slight_smile: !!

Thanks for your advice.