Domanda

I defined a configuration parameters in bootstrap, but they are not translated after in use.

$duedates = array(
    '0'=> __('none'),
    '1'=> __('8 days'),
    '2'=> __('10 days'),
    '3'=> __('15 days'),
    '4'=> __('30 days'),
    '5'=> __('45 days'),
    '6'=> __('60 days'),
    '7'=> __('90 days'),
    '8'=> __('120 days'),
) ;

Configure::write('DUEDATES', $duedates);

They aren't trasnlated after using array

Configure::read('DUEDATES');

Where is the problem?

È stato utile?

Soluzione

You can not define translatable strings in your bootstrap, as the language/locale is not yet set at that point. The bootstrap is processed before anything else, including the Config/core.php, which is where your language/locale settings should be. You should either:

  1. Define the array at a later point, like in the beforeRender method of your AppController.
  2. Move the language configuration to your bootstrap prior to defining the translatable strings.

Altri suggerimenti

Bootstrapping CakePHP

If you have any additional configuration needs, use CakePHP’s bootstrap file, found in app/Config/bootstrap.php. This file is executed just after CakePHP’s core bootstrapping.

This file is ideal for a number of common bootstrapping tasks:

  • Defining convenience functions.
  • Registering global constants.
  • Defining additional model, view, and controller paths.
  • Creating cache configurations.
  • Configuring inflections.
  • Loading configuration files.

Be careful to maintain the MVC software design pattern when you add things to the bootstrap file: it might be tempting to place formatting functions there in order to use them in your controllers.

Resist the urge. You’ll be glad you did later on down the line.

http://book.cakephp.org/2.0/en/development/configuration.html

It seems wrong to put localized strings into configuration. Their place seems to be in Views part of MVC.

CakePHP’s Configure class can be used to store and retrieve application or runtime specific values. Be careful, this class allows you to store anything in it, then use it in any other part of your code: a sure temptation to break the MVC pattern CakePHP was designed for. The main goal of Configure class is to keep centralized variables that can be shared between many objects. Remember to try to live by “convention over configuration” and you won’t end up breaking the MVC structure we’ve set in place.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top