[Solved] Translate Configure messages

Hi everyone,

There are a few aspects of i18n that I haven’t been able to understand.

I was trying to figure out if it was possible to translate the strings defined in settings.php and loaded via Configure.

Consider this scenario:
I have a a custom settings defined as it follow

// in settings.php
return [ 
   'Settings' => [
       'genders' => [
	   'm' =>__d('settings', 'Male'),
	   'f' => __d('settings', 'Female'),
	'salutations' => [
		'm' => 'Mr.',
		'f' => 'Mrs.',

I use this settings to set the Gender field options when creating / editing a user.
Just for example:

echo $this->Form->control('email', ['required' => false]);
echo $this->Form->control('gender', 
        'required' => false,
        'options' => Configure::read('Settings.genders')
echo $this->Form->control('first_name', ['required' => false]);
echo $this->Form->control('last_name', ['required' => false]);

When i extract the translate strings from /config i can see a settings.pot file is created in resources/locales/settings.pot.
After that i proceed to create a translate file: resources/locales/de/settings.po and clean the cake core cache with the command:

bin/cake cache clear _cake_core_

What did i expect?
I expect that by visiting the page en/users/add the gender field has Male and Female as options, while visiting the page de/users/add the options should be Männlich and Weiblich

What did i get?
When accessing de/users/add the options for gender are Male and Female

Bonus problem
It seems that the same thing also happens for the validation messages defined in the Tables models.

Every other traslate string works fine (i mean the ones in the controllers or the views)

Anyone is facing the same problem?

Check the timing of when you are loading those settings vs when you are setting the locale.

1 Like

Hi Zuluru, and thanks for your answer.

Regarding timing, I upload my settings to bootstrap.php at the very end of file:

// bootstrap.php
 * Read settings file
try {
    Configure::load('settings', 'default');
    Configure::load('settings-admin', 'default');
} catch (\Exception $e) {
    exit($e->getMessage() . "\n");

The locale is set by AdMad/I18n plugin in middleware:

->add(new \ADmad\I18n\Middleware\I18nMiddleware([
                // If `true` will attempt to get matching languges in "languages" list based
                // on browser locale and redirect to that when going to site root.
                'detectLanguage' => true,
                // Default language for app. If language detection is disabled or no
                // matching language is found redirect to this language
                'defaultLanguage' => Configure::read('I18n.defaultLang'),
                // Languages available in app. The keys should match the language prefix used
                // in URLs. Based on the language the locale will be also set.
                'languages' => Configure::read('I18n.locales'),

I’m aware that middlware are called after bootstrap. So i tried to load the settings after the middleware but still got no result. Any tips on where should be safe to load settings via Configure::load?

Thanks in advance!

You would need to load them not just after adding the middleware, but after the middleware executes, which is later in the process. beforeFilter would seem to be a possibility? Or create your own middleware that runs after this one?

1 Like

Thank you!

I will give it a shot and come back here with the response!

I went with the beforeFilter solution which works fine.

Some of the middleware i’m using require some data from settings, and in order to pass it to the middleware i created 2 separated settings:

  1. config_settings - which is loaded in bootstrap.php before middleware
  2. custom_settings - which contain translated strings loaded in AppController->beforeFilter()