沃梦达 / 编程问答 / php问题 / 正文

gettext 如何处理动态内容?

How does gettext handle dynamic content?(gettext 如何处理动态内容?)

本文介绍了gettext 如何处理动态内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在php中(或者可能是一般的gettext),gettext在看到动态内容的变量时会做什么?

In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content?

我有两个案例.

1) 假设我有 <?=$user1?>戳约翰<?=$user2?>.也许在某些语言中,单词的顺序是不同的.gettext 是如何处理的?(不,我不是在构建 facebook,这只是一个例子)

1) Let's say I have <?=$user1?> poked John <?=$user2?>. Maybe in some language the order of the words is different. How does gettext handle that? (no, I'm not building facebook, that was just an example)

2) 假设我在数据库中存储了一些类别.它们很少,但它们存储在数据库中.如果我这样做会发生什么 <?php echo gettext($data['name']);?> ?我希望翻译人员也翻译这些类别名称,但是必须在数据库本身中完成吗?

2) Let's say I store some categories in a database. They rarely, but they are store in a database. What would happen if I do <?php echo gettext($data['name']); ?> ? I would like the translators to translate those category names too, but does it have to be done in the database itself?

谢谢

推荐答案

你最好的选择是使用 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() 函数:

Which you would input into the translate() function like this:

<?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']); ?>

这篇关于gettext 如何处理动态内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:gettext 如何处理动态内容?

基础教程推荐