Cakephp, i18n.po files, how to use them correctly

I finally managed to create a multilingual cakephp site. Although this has not ended, I can change DEFAULT_LANGUAGE for the first time at boot, and I can see that the language is changing.

My problem right now is that I cannot figure out how to use po files correctly. According to the tutorials that I used, I need to create a folder / application / locale, and inside this folder create a folder for each language in the following format: / locale / eng / LC_MESSAGES.

I did this and I was also able to extract the default.pot file using cake i18n extract. And it seems that all occurrences of the __ () function were found successfully.

In my application, I use 2 languages: eng and gre. I can understand why you need a separate folder for each language. However, in my case, nothing happens when I edit the po files inside each folder .... well, almost nothing. If I edit / app / locale / gre /LC_MESSAGES/default.po, I have no language changes. If I edit / app / locale / eng /LC_MESSAGES/default.po, then the language will change to a new value (in the translation field) and will not switch to another language.

What am I doing wrong. I hope I have made everything as clear as possible.

+4
source share
2 answers

There are different ways to do this. The easiest way is to encode the application in the main language and simply wrap all translatable strings in __() . Later, you can add .po files for each translation you may need.

The problem with this approach is that if you want to change the text in the original language, you will also need to change the msgid entry for this line in every .po file that you may have. This can become rather cumbersome if you need to support different languages.

Please ignore the above information. A properly configured i18n workflow will use xgettext or similar utilities to automatically extract __() wrapped lines from source code and create, update, and merge .po files. Nothing cumbersome about this.


An alternative is to use the β€œdescriptor” text in the source files and to place the actual text in the .po files, even for the main language. I.e:

 __('PRODUCT CAPTION'); /eng/.po msgid "PRODUCT CAPTION" msgstr "Buy our awesome products!" /ger/.po msgid "PRODUCT CAPTION" msgstr "Kaufen Sie unsere Produkte!" 

What works best depends on the project and on you, you have to figure it out ...

+4
source

Use

 Configure::write('Config.language', 'fr'); 

to set the user language to French.

+1
source

Source: https://habr.com/ru/post/1304982/


All Articles