Search code examples
cakephpinternationalizationtranslationcakephp-3.x

Cached translations in CakePHP 3.7.4


I have taken over a project on cakephp. The problem is that I can not change translation texts. Under src/Locale I have:

en_EN
  default.mo
  default.po
no_NO
  default.mo
  default.po
cake.pot
default.pot

In controller

public function view($id)
{
    $order = $this->Order->get($id);
    $this->set(compact('order'))
}

In view I have a form where is translated text by default

 <?= $this->Form->control('email_message', [
            'type' => 'textarea',
            'rows' => 15,
            'help' => sprintf('Email will be sent to %s', h($order->contact_email)),
            'default' => __('pickup_mailtext')
        ]) ?>

No I have in

en_EN
  default.po
msgid "pickup_mailtext"
msgstr "This is the old pickup mailtext"

if I change it to

msgid "pickup_mailtext"
msgstr "This is the NEW pickup mailtext"

Nothing changes. I have deleted everything in persistent directory. Also in Config/app.php default language is set to no_NO, but as I mentioned before this string is under en_EN

under

no_NO
  default.po

There is:

msgid "pickup_mailtext"
msgstr ""

I have also noticed, tad this string is in

en_EN
  default.mo

but if I try to modify it I get Internal server error.

So my question is: 1. How to get this translation working? Why this string is not changing? 2. If the default language is set to no_NO, then why the translations is in en_EN and why it is getting translated instead of being empty? 3. How to clear those .mo files?

Thanks


Solution

  • .mo files are the compiled binary versions of the respective .po files, you can't just modify them with a text editor, you need to recompile the .po files instead, using a program like msgfmt, or one with a GUI like Poedit.

    CakePHP will by default prefer .mo files over .po files (the former are usually faster to parse), ie if a .mo file exists it will be used instead of a possible .po file with the same name, so if you change only the .po file, nothing will happen as that file is not being used.

    If changing the default locale in config/app.php has no effect, then the locale might get changed somewhere else in your app. Check \Cake\I18n\I18n::getLocale() in your view template to figure what locale is actually being used at that point, and set a breakpoint in \Cake\I18n\I18n::setLocale() (vendor/cakephp/cakephp/src/I18n/I18n.php) or log a stacktrace to figure from where in the code the locale is being set.