duanliujie8639 2012-05-26 20:52
浏览 76
已采纳

用于gettext转换的多功能功能

It's similar to what Drupal has.

My current function definition is like this:

string t(string $singular, [string $plural, int $number, mixed $arg1, ...])

The order of the arguments doesn't have to follow this pattern. Only string $singular needs to be the first for obvious reasons.

Some examples to demonstrate how it works:

  • Translate "Hello":

    t('Hello')
    
  • Translate "Hello" and format the string (I'm using vsprintf with func_get_args())

    t('Hello %s', $user)
    
  • Plural translation:

    t('%d apple', '%d apples', 5, 5)
    

    So here the problems start. I'm assuming that this is a request for a plural translation if 2nd argument is a string and 3rd argument is integer.

    The issue is that I could intercept a singular translation request by mistake:

    $user = 'Mary';
    t('%s, you need at least %d apples for me to take you srsly', $user, 420)
    

Do you have any suggestions on how could I better implement such functionality and avoid clashes like these?

  • 写回答

2条回答 默认 最新

  • dsklzerpx64815631 2012-05-26 21:11
    关注

    I would use standard _() and ngettext() instead, because then you could get all localizible strings gathered via xgettext shell command. You could even use your own function names like t() and nt() and still extract them with xgettext (see --keyword param in https://developer.mozilla.org/en/gettext) but you can't (and shouldn't) avoid two functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示