【问题标题】:Steps for making Yii::t() work with extensions in Yii使 Yii::t() 与 Yii 中的扩展一起工作的步骤
【发布时间】:2014-02-26 20:54:17
【问题描述】:

我创建了一个名为 GalleryManager 的扩展,存储在应用程序的扩展目录中。 我有这个结构:

gallerymanager
|--  assets
|--  messages
|    |-- en
|    |    |-- main.php
|    |-- es
|    |    |-- main.php
|    GalleryManager.php

我的 GalleryManager 类(在 GalleryManager.php 中定义)如下:

class GalleryManager extends CApplicationComponent{...

这是documentation 所说的关于定义翻译类别的内容:

扩展类(例如小部件、模块)的消息可以是 专门管理和使用。特别是,如果一条消息属于 类名为 Xyz 的扩展名,则消息类别可以是 以“Xyz.categoryName”的格式指定。以及对应的 消息文件假定为 'BasePath/messages/LanguageID/categoryName.php',其中'BasePath' 指包含扩展类文件的目录。什么时候 使用 Yii::t() 翻译扩展消息,类别名称 应设置为“Xyz.categoryName”。

所以,我在视图中尝试这样的一行:

<?php echo Yii::t('GalleryManager.main', 'Add');?>

而且它不起作用。 为了使翻译适用于 Yii 扩展,我还需要做什么?

【问题讨论】:

  • 你的消息不应该在protected/messages/吗?
  • @AliMasudianPour 不,根据文档,默认情况下messages 目录应存储在包含类文件的目录中(在我的情况下,这就是我在 dir 结构中显示的内容)

标签: php yii


【解决方案1】:

您所做的一切都是正确的,并且符合文档。

您确定在主配置中正确声明了扩展吗?你应该在那里:

'components' => array(
            /* load extension */
            'GalleryManager' => array(
                'class' => 'app.extensions.GalleryManager',
            ),

我有时不会自己加载这样的扩展程序(因为这仍然可以正常工作),但要让消息正常工作,我相信您也必须在配置中声明它。

【讨论】:

  • 是的,我在config/main.phpcomponents 部分声明它,但是像这样:'components' =&gt; array( /* load extension */ 'gallerymanager' =&gt; array( 'class' =&gt; 'app.extensions.gallerymanager.GalleryManager', ), 是不是错了?
  • 看起来不错,你可以试试 看看它是否有效? (请注意,我不使用 GalleryManager 而是使用 gallerymanager)。我确实相信它可能区分大小写,并且您的文件夹称为 gallerymanager 而不是 GalleryManager。
  • 尝试手动定义 CPhpMessageSource 的处理程序。查看更多详细信息 yiiframework.com/doc/api/1.1/…yiiframework.com/forum/index.php/topic/…。在您的配置中,您应该有 'messages'=>array( 'class'=>'CPhpMessageSource', 'extensionPaths' => array( 'GalleryManager' => 'app.extensions.gallerymanager.messages', ) ), )跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多