2012-05-26 20:52
浏览 76


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":

  • 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?

图片转代码服务由CSDN问答提供 功能建议

它类似于 Drupal有什么


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

的顺序 参数不必遵循这种模式。 由于显而易见的原因,只有 string $ singular 才能成为第一个。


  • 翻译“你好”:

     <  li> 

    翻译“Hello”并格式化字符串(我正在使用 vsprintf func_get_args()< / code>

      t('Hello%s',$ user)
     <  li> 


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

    所以问题就出现了。 我假设这是一个复数翻译的请求,如果第二个参数是一个字符串并且第三个参数是整数。

    问题是我可以拦截 一个错误的单一翻译请求:

      $ user ='Mary'; 
    t('%s,你需要至少%d苹果给我带来你的狡猾',  $ user,420)

    您对如何更好地实现此类功能并避免这些冲突有任何建议吗? ?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    解决 无用
    打赏 举报
  • dongwei8440 2012-05-26 21:05

    What about if you would have only 2 arguments, first one is a string and 2nd one is in simple cases int, but in complex cases it would be associative array like

    array('plural_int' => 5, 'plural_str' => '%d apples', 'singular_int' => 10) 


    array('singular_int' => array(10,20,30))
    解决 无用
    打赏 举报

相关推荐 更多相似问题