gettext, how to handle homonyms?

后端 未结 4 1109
悲&欢浪女
悲&欢浪女 2021-02-20 11:36

Using gettext

Single value

echo gettext( \"Hello, world!\\n\" );

Plurals

printf(ngett         


        
4条回答
  •  后悔当初
    2021-02-20 12:13

    What you are looking for is contexts for gettext which solves ambiguities like your example. You can find information about in the documentation. Still the needed method pgettext is not implemented in PHP so you might use the helper method stated in a user comment in the php documentation.

    if (!function_exists('pgettext')) {
    
      function pgettext($context, $msgid)
      {
         $contextString = "{$context}\004{$msgid}";
         $translation = dcgettext('messages', contextString,LC_MESSAGES);
         if ($translation == $contextString)  return $msgid;
         else  return $translation;
      }
    
    }
    

    In your case it would be

    echo pgettext('mail', 'Letter');
    echo pgettext('character', 'Letter');
    

提交回复
热议问题