且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

gettext如何处理动态内容?

更新时间:2023-02-02 19:38:49

您***的选择是使用sprintf()函数.然后,您将使用printf表示法来处理字符串中的动态内容.这是我不久前在这里找到的一个功能,可以轻松为您处理:

Your best option is to use sprintf() function. Then you would use printf notation to handle dynamic content in your strings. Here is a function I found on here a while ago to handle this easily for you:

function translate()
{
    $args = func_get_args();
    $num = func_num_args();
    $args[0] = gettext($args[0]);

    if($num <= 1)
      return $args[0];

    return call_user_func_array('sprintf', $args);

}

现在以示例1为例,您想将字符串更改为:

Now for example 1, you would want to change the string to:

%s poked %s

您将像这样输入translate()函数:

<?php echo translate('%s poked %s', $user1, $user2); ?>

您将使用poEdit解析所有translate()函数.然后将字符串"%s poked %s"转换为所需的任何语言,而无需修改%s字符串占位符.分别由translate()函数使用user1和user2输出时将替换它们.您可以在PHP手册中的sprintf()上阅读更多信息,以了解更多高级用法.

You would parse out all translate() functions with poEdit. and then translate the string "%s poked %s" into whatever language you wanted, without modifying the %s string placeholders. Those would get replace upon output by the translate() function with user1 and user2 respectively. You can read more on sprintf() in the PHP Manual for more advanced usages.

第2个问题.您将需要创建一个静态文件,poEdit可以解析该文件,其中包含类别名称.例如misctranslations.php:

For issue #2. You would need to create a static file which poEdit could parse containing the category names. For example misctranslations.php:

<?php

_('Cars');
_('Trains');
_('Airplanes');

然后进行poEdit解析misctranslations.php.然后,您可以使用<?php echo gettext($data['name']); ?>

Then have poEdit parse misctranslations.php. You would then be able to output the category name translation using <?php echo gettext($data['name']); ?>