dongzhi9574 2015-11-06 15:54
浏览 54

在PHP中翻译模型异常 - Zf2 - Zend i18n - Poedit

How can I translate my custom exceptions, inside my module/model? what's the best way?

1) Directly into MyModelTableGetaway.php. example:

 public function getAlbum($id)
     $id  = (int) $id;
     $rowset = $this->tableGateway->select(array('id' => $id));
     $row = $rowset->current();
     if (!$row) {
         throw new \Exception(__("Could not find row $id"));
     return $row;

And I'll tell to poedit to consider all inside __()

2) Create a folder MyModel/languages/language_than_i_want and into this folder I can put a file .php with array of exceptions

Whats is better and fast? There are any other methods?

Thanks a lot

  • 写回答

1条回答 默认 最新

  • drxp993551 2015-11-11 07:50

    I'd go with making Zend\I18n\Translator\Translator a dependency of your class and using that. You can set that in your model/service factory (or whatever way you provision that object), e.g.:

    $translator = new Translator();
    $translator->addTranslationFile('array', 'path/to/your/translation/array.php, '', 'de);

    And the use it in your class:

     if (!$row) {
         throw new \Exception($translator->translate("Could not find row $id"));

    You could also override the exception template and use translator helper in it. That might not work all the time, though.

    Other than that you'd need something to feed the translator service. I'd recommend translation array, since it'd be easier to work with than gettext.

    Relevant docs or blogposts:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
    • ¥100 webapi的部署(标签-服务器)
    • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
    • ¥15 C语言除0问题的检测方法
    • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
    • ¥15 macos13下 ios交叉编译的问题
    • ¥15 bgz压缩文件怎么打开
    • ¥15 封装dll(引入了pcl的点云设计库)
    • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发