Drupal 6:为什么在t()中使用!标记?(Drupal 6: Why use !tokens in t()?)

编程入门 行业动态 更新时间:2024-10-05 19:20:57
Drupal 6:为什么在t()中使用!标记?(Drupal 6: Why use !tokens in t()?)

我正在阅读Pro Drupal Development,第二版 。 它说,以下是必要的:

t("Your favorite color is !color", array('!color' => "$color"));

但它也说了! 占位符表示不会对字符串进行转换。 那么为什么不只是:

t("Your favorite color is $color");

谢谢。

I'm reading Pro Drupal Development, Second Edition. It says that the following is necessary:

t("Your favorite color is !color", array('!color' => "$color"));

But it also says that the ! placeholder means that no transformation will be done on the string. So why not just:

t("Your favorite color is $color");

Thanks.

最满意答案

t()用于查找封闭字符串的翻译。 如果直接在该字符串中包含可变内容(在您的示例中为$ color),则翻译查找将失败,因为遇到并未翻译任何新内容。 占位符允许翻译员只翻译字符串的固定部分,并仍允许注入可变内容。

修饰符'!','%','@'只是让您更好地控制插入的方式,'!' 这意味着该字符串将按原样插入。

最明显的例子是数字:

如果你有

t("Number $count");

你可以用不同的数字来称呼它,比如1,2,3,每次t()都会为不同的字符串寻找不同的翻译

t('Number 1') t('Number 2') t('Number 3')

而与

t('Number !count', array('!count' => $count);

它只会寻找一个翻译 ,注入数字'原样'!

另一个好处是,翻译者可以通过提供例如'!count whatever'作为翻译字符串,将占位符置于适合目标语言使用的不同位置。 通过上面的例子,这将导致:

'不管' '2无论' '3不管'

使用'%'将用<em>标记围住占位符以突出显示,'@'会通过check_plain()运行它来转义标记。

t() is used to look up a translation of the enclosed string. If you have variable content directly in that string ($color in your example), the translation lookup will fail for any new content encountered and not yet translated. The placeholders allow translators to translate only the fixed part of the string and still allow injection of variable content.

The modifiers '!','%','@' just give you some more control over how the insertion takes place, with '!' meaning that the string will be inserted as is.

The most obvious example would be with numbers:

If you have

t("Number $count");

and you call it several times with different numbers, say 1,2,3, each time t() would look for a different translation for a different string:

t('Number 1') t('Number 2') t('Number 3')

whereas with

t('Number !count', array('!count' => $count);

it would only look for one translation, injecting the number 'as is' into that!

An added benefit is that the translator can place the placeholder in a different position that fits the usage of the target language by providing, e.g. '!count whatever' as the translation string. With the above example, this would result in:

'1 whatever' '2 whatever' '3 whatever'

Using '%' would surround the placeholder with <em> tags for highlighting, '@' would run it through check_plain() to escape markup.

更多推荐

本文发布于:2023-07-09 10:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   Drupal   tokens

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!