2

我有 cakephp 2,默认语言为“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)
        )
    );

任何人都知道我如何将月份名称翻译成法语。我的 default.po 已经有月份的法语翻译。

4

3 回答 3

3

CakePHP is PHP

As mentioned in the docs:

There’s one other aspect of localizing your application which is not covered by the use of the translate functions, and that is date/money formats. Don’t forget that CakePHP is PHP :), therefore to set the formats for these things you need to use setlocale.

As such, to localize dates you need to call setlocale somewhere appropriate - such as your AppController::beforeFilter if it's a multilingual app - or app/bootstrap.php if it's single language.

Alternatively

If instead of relying on setlocale (which is the better idea) you prefer a bit more control you can use the 'monthNames' => true to use "normal" translations rather than the output of strfrtime. You'll find this in the source. You can get a French po file from the localized repository - which in addition to translating month names for you, also translates all the other standard texts that come out of the core.

If all else fails

You should never land here - but if nothing is working (this will mean that there are mistakes in your application) you can just defined monthNames to a normal array of month number -> name, and this will take precedence over automatically derived month names.

于 2013-01-23T22:05:29.647 回答
1

我将以下行放在引导文件中,效果很好。

Configure::write('Config.language', 'fra');
于 2014-07-11T14:30:39.670 回答
0

有几个步骤:

首先,设置要使用的语言环境。
为该语言创建一个或多个 .po 文件。
使用 __() 或 __d() 辅助方法包装所有支持 l10n 的字符串。
这是我的一个项目的摘录:

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 书提供了足够的信息。http://book.cakephp.org/view/161/Internationalization-Localization

如果您选择仅使用一个 .po 文件,您将使用 __() 帮助程序包装您的字符串。我选择了多个 .po 文件以避免一个庞大的文件,因此我使用了 __d() 帮助程序,以便我可以指定哪个域(域 == .po 文件的名称,没有 .po 扩展名)。

更新

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

于 2013-01-24T13:52:46.387 回答