【问题标题】:using localization in cakephp在 cakephp 中使用本地化
【发布时间】:2010-04-18 05:52:39
【问题描述】:

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

【问题讨论】:

  • "havent had any success with the online documentation"?就像“我在粗略的扫描中找不到正确的部分”或“我尝试了一些我不会费心告诉你但它们不起作用的事情”?无论哪种方式,不好的问题。 L10n 在 Cake 手册中有详细记录。

标签: php cakephp localization


【解决方案1】:

有几个步骤:

  1. 首先,设置要使用的语言环境
  2. 为该语言创建一个或多个 .po 文件
  3. 使用 __()__d() 辅助方法包装所有支持 l10n 的字符串

这是我的一个项目的摘录:

# 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
}

那个 sn-p 将设置语言。您需要做的就是在/app/locale/eng/LC_MESSAGES/ 目录中提供适当的.po 文件。 CakePHP 书提供了sufficient information on this,我想。

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

更新

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

【讨论】:

  • 您根本不需要实例化L10n 类。这会在第一次调用 __() 函数时自动发生。
  • 有趣。我不知道,但由于我在这里使用它来get() 语言,在评估任何__() 之前我不需要一个实例吗?
  • 不,这一切都是在您调用__() 时完成的。您可以尝试烘焙一个新的 Cake 应用程序,然后在某处添加一个 __('Test'),为该字符串提供一个翻译 .po 文件并切换您的浏览器首选语言设置。它会切换语言,而您无需做任何其他事情。
  • 无法更改默认“域”,__() 始终使用默认域。
【解决方案2】:

CakePHP 3.x 本地化

为动态语言更改创建操作

路线

$routes->connect('/locale', ['controller' => ' Users', 'action' => 'languageChange']);

控制器动作

用户控制器

use Cake\Http\Session;
use Cake\I18n\I18n;

//Before filter in users controller
public function beforeFilter(\Cake\Event\Event $event)
    {
        parent::beforeFilter($event);
    }


      /**
     * Change language
     */
    public function languageChange()
    {
               if ($this->request->is('post')) {
            $session = $this->getRequest()->getSession();
            if (!empty($this->request->getData('locale'))) {
                $session->write('Config.language', $this->request->getData('locale'));
                $this->redirect($this->referer());
            } else {
                $session->write('Config.language', I18n::getLocale());
                $this->redirect($this->referer());
            }
        }
    }

创建视图文件意味着language_change.ctp文件

<?php
echo $this->Form->create("Localizations", array('url' => '/locale'));
echo $this->Form->radio("locale", [
    ['value' => 'en_US', 'text' => 'English'],
    ['value' => 'de_DE', 'text' => 'German'],
    ['value' => 'fr_FR', 'text' => 'French'],
]);
echo $this->Form->button('Change Language');
echo $this->Form->end();
?>
<h2><?php echo __('Hello'); ?></h2>
<h2><?php echo __('How are you?'); ?></h2>
<h2><?php echo __('Wel Come'); ?></h2>
<h2><?php echo __('Good Job'); ?></h2>
  • 这个执行流程是
  • 第一次通话动作语言更改前-http://example.local/locale
  • 然后更改语言选择单选按钮更改语言

AppController

use Cake\I18n\I18n;
use Cake\Http\Session;
use Cake\Core\Configure;


 public function beforeFilter(Event $event)
    {
        $session = $this->getRequest()->getSession();

        if ($session->check('Config.language')) {
            I18n::setLocale($session->read('Config.language'));
        } else {
            $session->write('Config.language', I18n::getLocale());
        }
    }

在文件中创建此位置 src/Locale/en_US/default.po

msgid "Hello"
msgstr "Hello"

msgid "How are you?"
msgstr "How are you?"

msgid "Wel Come"
msgstr "Wel Come"

msgid "Good Job"
msgstr "Good Job"
  • src/Locale/de_DE/default.po

    msgid "Hello"
    msgstr "Hallo"
    
    msgid "How are you?"
    msgstr "Hoe gaat het met je?"
    
    msgid "Wel Come"
    msgstr "Wel kom"
    
    msgid "Good Job"
    msgstr "Goed gedaan"
    
  • 在 boostrap.php 中配置

    '会话'=> [ '默认值' => 'php', '语言' => 'en_US' ]

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2019-01-24
    相关资源
    最近更新 更多