php - 在 cakephp 中使用本地化

标签 php cakephp localization

如何本地化 cakePhp 中的字符串?我在在线文档方面没有取得任何成功。感谢您的帮助。

最佳答案

有几个步骤:

  1. 首先,设置要使用的区域设置
  2. 为该语言创建一个或多个 .po 文件
  3. 使用 __()__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/

相关文章:

php - MySQL 查询响应时间过长

java相当于php的hmac-SHA1

email - 使用 gmail 帐户发送的电子邮件出现在 cakePHP 中收件人的垃圾邮件文件夹中?

ios - 从中文切换回英文会添加换行符

PHP PDO $row 内部 php 代码未运行

cakephp - CakePHP 2.0 中的错误处理。转义 View 变量

php - 如何修复CENTOS 7.6上的 ‘No Space Left on Device’错误

android - 如何创建多平台本地化解决方案?

ios - 如何使用所有语言填充 iOS 选择器?

php - 可捕获的 fatal error :第14行的C:\xampp\htdocs\PokemonFight\pokemon.php中无法将类mysqli_result的对象转换为字符串