如何本地化 cakePhp 中的字符串?我在在线文档方面没有取得任何成功。感谢您的帮助。
最佳答案
有几个步骤:
- 首先,设置要使用的区域设置
- 为该语言创建一个或多个
.po
文件 - 使用
__()
或__d()
辅助方法封装所有可本地化的字符串
以下是我的一个项目的摘录:
# app/app_controller.php
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 书提供 sufficient information on this ,我认为。
如果您选择仅使用一个 .po
文件,则将使用 __()
帮助器包装字符串。我选择了多个 .po
文件以避免出现一个庞大的文件,因此我使用了 __d()
帮助程序,以便我可以指定哪个域(域 == 的名称)不带 .po
扩展名的 .po
文件)。
更新
我应该补充一点,您需要使用翻译
行为来帮助您处理需要翻译的数据库内容。
关于php - 在 cakephp 中使用本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2661275/