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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵