Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在cakephp中使用本地化_Php_Cakephp_Localization - Fatal编程技术网

在cakephp中使用本地化

在cakephp中使用本地化,php,cakephp,localization,Php,Cakephp,Localization,如何在cakePhp中本地化字符串?我在网上文档方面没有取得任何成功。谢谢您的帮助。这里有几个步骤: 首先,设置要使用的区域设置 为该语言创建一个或多个.po文件 使用\uuuu()或\uuu d()帮助程序方法包装所有l10n-able字符串 以下是我的一个项目的摘录: # app/app_controller.php uses ( 'L10n' ); class AppController extends Controller { public function beforeFilte

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

这里有几个步骤:

  • 首先,设置要使用的区域设置
  • 为该语言创建一个或多个
    .po
    文件
  • 使用
    \uuuu()
    \uuu d()
    帮助程序方法包装所有l10n-able字符串
  • 以下是我的一个项目的摘录:

    # 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\u MESSAGES/
    目录中提供适当的
    .po
    文件。我认为,CakePHP书提供了

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

    更新


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

    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());
                }
            }
        }
    
    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());
            }
        }
    
    创建视图文件mean 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>
    
    在文件中创建此位置 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"
    
    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"
    
    • src/Locale/de_de/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"
      
      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”, “语言”=>“英语” ]


      • “在线文档还没有成功”
        ?比如“我在粗略的扫描中找不到正确的部分”或者“我尝试了一些我不想告诉你的事情,但它们不起作用”?不管怎样,这都是个糟糕的问题。L10n在Cake手册中有很好的文档记录。您根本不需要实例化
        L10n
        类。这会在第一次调用
        \uuu()
        函数时自动发生。有趣。我不知道这一点,但因为我在这里使用它来
        获取()
        语言,在计算任何
        之前,我不需要一个实例吗?不,这都是在调用
        \uu()
        时完成的。您可以尝试烘焙一个新的蛋糕应用程序,只需在某个地方添加一个
        。(“Test”)
        ,为字符串提供一个translation.po文件,并切换浏览器首选的语言设置。它将切换语言,而无需您做任何其他操作。无法更改默认的“域”,\uuuu()始终使用默认域。