Gettext to work with context (pgettext)

不打扰是莪最后的温柔 提交于 2019-12-11 12:53:54

问题


I'd like to use pgettext to specify the context of some strings to translate, I've found that you need to add it yourself in PHP, which is what I did following this post's instructions. I've changed it a little bit to make it work (the dcgettext function call had some errors):

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

But this function doesn't seems to work, the text is not changing when I change the language.

What am I missing?


回答1:


I've found a solution that is working well for me:

function pgettext($context, $msgid) {
  $contextString = "{$context}\004{$msgid}"; 
  $translation = _($contextString); 

  if($translation == $contextString) return $msgid;
  else return $translation;
}


来源:https://stackoverflow.com/questions/31994975/gettext-to-work-with-context-pgettext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!