cakephp-本地化不起作用

cakephp-本地化不起作用,cakephp,localization,cakephp-2.0,Cakephp,Localization,Cakephp 2.0,我有cakephp2,默认语言是fre,但由于一些奇怪的原因,在我生日的表格中,我的月份仍然是英语。这是我的密码 echo $this->Form->input('User.birthday', array ( 'label' => array('text'=>__("form_birthday", "true"), 'class' => ''), '

我有cakephp2,默认语言是fre,但由于一些奇怪的原因,在我生日的表格中,我的月份仍然是英语。这是我的密码

 echo $this->Form->input('User.birthday', 
        array (
            'label' => array('text'=>__("form_birthday", "true"), 
                      'class' => ''),

            'selected' => 'empty',
            'dateFormat' => 'MDY',
            'minYear' => date('Y') - 90,
            'maxYear' => date('Y') - 18,
            'separator'=> " ",
            'empty' => __('select', true)
        )
    );
任何人都知道我如何将月份名称翻译成法语。My default.po已经有了月份的法语翻译。

CakePHP是PHP 如中所述:

本地化应用程序的另一个方面不包括在翻译函数的使用中,那就是日期/货币格式。不要忘记CakePHP是PHP:,因此要设置这些东西的格式,您需要使用

因此,要本地化日期,您需要在适当的位置调用setlocale,例如如果是多语言应用程序,则调用AppController::beforeFilter;如果是单语言应用程序,则调用app/bootstrap.php

或者 如果您不需要依赖setlocale(这是一个更好的主意),而是更喜欢一点控制,那么您可以使用“monthNames”=>true来使用正常翻译,而不是strftime的输出。你会找到这个的。您可以从中获得一个法语po文件,该文件除了为您翻译月份名称外,还可以翻译来自核心的所有其他标准文本

如果一切都失败了 您永远不应该在这里登陆-但如果没有任何功能,这将意味着您的应用程序中存在错误,您可以将monthNames定义为一个普通的month number->name数组,它将优先于自动派生的月份名称。

CakePHP是PHP 如中所述:

本地化应用程序的另一个方面不包括在翻译函数的使用中,那就是日期/货币格式。不要忘记CakePHP是PHP:,因此要设置这些东西的格式,您需要使用

因此,要本地化日期,您需要在适当的位置调用setlocale,例如如果是多语言应用程序,则调用AppController::beforeFilter;如果是单语言应用程序,则调用app/bootstrap.php

或者 如果您不需要依赖setlocale(这是一个更好的主意),而是更喜欢一点控制,那么您可以使用“monthNames”=>true来使用正常翻译,而不是strftime的输出。你会找到这个的。您可以从中获得一个法语po文件,该文件除了为您翻译月份名称外,还可以翻译来自核心的所有其他标准文本

如果一切都失败了 您永远不应该在这里登陆-但如果没有任何功能,这将意味着您的应用程序中存在错误,您可以将monthNames定义为一个普通的月号->名称数组,这将优先于自动派生的月名。

有两个步骤:

首先,设置要使用的区域设置。 为该语言创建一个或多个.po文件。 使用_uu或_ud helper方法包装所有l10n-able字符串。 以下是我的一个项目的摘录:

uses ( 'L10n' );

class AppController extends Controller {
 public function beforeFilter() {
  /**
 * Derive the desired locale by reading the subdomain from
 * the HTTP_HOST server variable. Locale subdomains can use
 * either the 2 or 3 character ISO code. Information on locale
 * ISO codes is at http://www.loc.gov/standards/iso639-2/php/code_list.php.
 */
$this->L10n = new L10n();

/** Auto-detect the request language settings */
$this->L10n->get();

/**
 * Set the default "domain" for translations. The domain is the
 * same as the po file name in a given locale directory. e.g.
 * __d( 'homepage', 'message_id' ) would look for the
 * message_id key in homepage.po. Using the __() convenience
 * function will always look in default.po.
 */
$this->set( 'domain', 'default' );
}

# The rest of your AppController code
}  
该片段将设置语言。您只需在/app/locale/eng/LC_MESSAGES/目录中提供适当的.po文件。我认为CakePHP这本书提供了足够的信息

如果您选择只使用一个.po文件,那么您将使用_u_uu帮助程序包装字符串。我选择了多个.po文件以避免一个大文件,因此我使用了uu d helper,以便可以指定不带.po扩展名的.po文件的域名

更新

我应该补充一点,您需要使用翻译行为来帮助您处理需要翻译的数据库内容。

有几个步骤:

首先,设置要使用的区域设置。 为该语言创建一个或多个.po文件。 使用_uu或_ud helper方法包装所有l10n-able字符串。 以下是我的一个项目的摘录:

uses ( 'L10n' );

class AppController extends Controller {
 public function beforeFilter() {
  /**
 * Derive the desired locale by reading the subdomain from
 * the HTTP_HOST server variable. Locale subdomains can use
 * either the 2 or 3 character ISO code. Information on locale
 * ISO codes is at http://www.loc.gov/standards/iso639-2/php/code_list.php.
 */
$this->L10n = new L10n();

/** Auto-detect the request language settings */
$this->L10n->get();

/**
 * Set the default "domain" for translations. The domain is the
 * same as the po file name in a given locale directory. e.g.
 * __d( 'homepage', 'message_id' ) would look for the
 * message_id key in homepage.po. Using the __() convenience
 * function will always look in default.po.
 */
$this->set( 'domain', 'default' );
}

# The rest of your AppController code
}  
该片段将设置语言。您只需在/app/locale/eng/LC_MESSAGES/目录中提供适当的.po文件。我认为CakePHP这本书提供了足够的信息

如果您选择只使用一个.po文件,那么您将使用_u_uu帮助程序包装字符串。我选择了多个.po文件以避免一个大文件,因此我使用了uu d helper,以便可以指定不带.po扩展名的.po文件的域名

更新


我应该补充一点,您需要使用Translate行为来帮助您处理需要翻译的数据库内容。

我在引导文件中放了下面一行,效果非常好

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

我把下面的一行放在引导文件中,效果很好

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

在2.x中,_uu方法立即返回翻译,第二个参数为no true。如果您最近从1.x升级到2.x,_uuu方法会立即返回翻译结果,第二个参数为no true,则文档和迁移指南中都会提到这一点。它在文档中,也在迁移指南中提到,以防您最近从1.x升级:它不起作用。我不需要添加一个翻译
nce我没有需要翻译的db内容。我只需要把我表格中的月份名称改成法语。这是我的代码:App::使用'L10n','I18n';类AppController扩展控制器{public$helpers=array'Html',Form',public function beforeFilter{$this->L10n=new L10n;$this->L10n->getfre;debug$this->L10n;$this->set'domain',default';debugConfigure::read'Config.language';$this->set'lang,$this->getLanguage;}如果你创建了一个新的L10n实例-你所更改的任何属性都不会有任何效果,因为你没有更改\uuuu等人正在从中读取的实例。L10n->get and$This->set'domain',default',与翻译完全无关-它只是在视图中将$domain定义为默认值。尽管这个答案过于简单复杂的是,这实际上是我所看到的唯一一个答案,有些东西在实际使用L10n。它不起作用。我不需要添加翻译行为,因为我没有需要翻译的db内容。我只需要在我的表单中输入我的月份名称,使其与法语对应。下面是我的代码:App::uses'L10n','I18n';class AppController扩展了控件ller{public$helpers=array'Html',Form',public function beforeFilter{$this->L10n=new L10n;$this->L10n->getfre;debug$this->L10n;$this->set'domain',default';debugConfigure::read'Config.language';$this->set'lang,$this->getLanguage;debug$this->getLanguage;}如果你创建了一个新的L10n实例-你所更改的任何属性都不会有任何效果,因为你没有更改\uuuu等人正在从中读取的实例。L10n->get and$This->set'domain',default',与翻译完全无关-它只是在视图中将$domain定义为默认值。尽管这个答案过于简单复杂的是,这实际上是我所看到的唯一一个关于L10n实际使用的答案。我在bootstrap.php中设置了localelc_ALL,'fr_CA.utf8',但我没有运气。我的setlocale现在是setlocaleLC_ALL,'fr_CA',在一个独立的php文件中使用时,它会显示法国日期。现在我将locale改为fra。现在仍然很幸运。这是我的Appco在我的beforeFilter中只包含Configure::write'Config.language',fra;之后,controller.php仍然存在问题。我还在子类中调用了parent::beforeFilter。所有其他代码都证明了我迫切需要一个解决方案。所有其他表单标签和其他文本都是从Locale/fre/LC_MESSAGES/default.po转换而来的,但是我的moN个名称没有被转换成法语。我的bootstrap.php中有setlocaleLC_ALL,'fr_CA.utf8',但我没有运气。我的setlocale现在是setlocaleLC_ALL,'fr_CA',当在独立的php文件中使用时,它会显示法语日期。现在我将locale更改为fra。现在仍然很幸运。这是我的Appcontroller.php在havi之后仍然存在问题ng Configure::write'Config.language',fra;,在我的beforeFilter中。我也在我的子类中调用了parent::beforeFilter。所有这些代码都证明了我迫切需要一个解决方案。其他所有形式的标签和其他文本都是从Locale/fre/LC_MESSAGES/default.po翻译过来的,但我的月名并没有转换成法语.