CakePHP 2.1 .po files / translation not working
Asked Answered
E

2

5

I use <?php echo __("this is my string"); ?> inside my views to display text in multiple languages. I created a .po-file directory for german:

=> app/Locale/ger/LC_MESSAGES/default.po

msgid "this is my string"

msgstr "dies ist meine zeichenkette"

When I add Configure::write('Config.language','ger'); to my AppController.php beforeFilter function the text still remains "this is my string". Even if I create a .po for english with a different msgstr the text still remains the same.

Do I have to change a specific setting to activate the translation?


I have a follow up problem: I added Configure::write('Config.language','eng'); to core.php to set my default language but now I can't change the value, even if I try to override it in AppController.php beforeFilter with:

Configure::write('Config.language', 'deu'); //'de' also doesn't work
CakeSession::write('Config.language', 'deu'); //'de' also doesn't work

If I change the value in core.php it works perfectly fine but once set in core.php I cant change it.

solved: ah, I forgot to add parent::beforeFilter(); inside the beforeFilter function of all the controllers.


Additional information:

  • If you want to define a default language (and you usually will), add Configure::write('Config.language', 'eng'); to the core.php
Earthlight answered 6/4, 2012 at 21:51 Comment(0)
P
9

You can look at the file lib/Cake/I18n/L10n.php, to know how to call each language.

settings for German:

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

=> app/Locale/deu/LC_MESSAGES/default.po

Phidias answered 6/4, 2012 at 22:17 Comment(1)
Thanks for the hint with the L10n.php file, I will use that for future references.Earthlight
T
6

its "deu" not "ger"

CakePHP uses the official (T) codes from http://www.loc.gov/standards/iso639-2/php/code_list.php, for CakePHP 1.3 it is documented under Internationalizing Your Application:

The three-character locale codes conform to the ISO 639-2 standard, although if you create regional locales (en_US, en_GB, etc.) cake will use them if appropriate.

ISO 639-2 |  ISO 639-1 | English name | French name | German name |
Code      |  Code      | of Language  | of Language | of Language |
-------------------------------------------------------------------
ger (B)   |            |              |             |             |
deu (T)   |  de        | German       | allemand    | Deutsch     |
Tinworks answered 6/4, 2012 at 22:16 Comment(3)
Ah, now it works. Wasn't obvious to me that I have to use the "T" value from the file, too bad that this is not mentioned in the documentation as I think a lot of other CakePHP users might not know that.Earthlight
Thanks for your answer! I can only mark one as the correct answer, I took del_dan's one because of the reference to the L10n file.Earthlight
no problem. as for the documentation: you can participate by pushing to the github rep: github.com/cakephp/docs - so you can put the missing information in.Tinworks

© 2022 - 2024 — McMap. All rights reserved.