3

Pro Drupal Development, Second Editionを読んでいます。次のことが必要であると述べています。

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

しかし、それはまた、! プレースホルダーは、文字列に対して変換が行われないことを意味します。それでは、次のことだけではありません。

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

ありがとう。

4

2 に答える 2

12

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:

  1. t('Number 1')
  2. t('Number 2')
  3. 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. '1 whatever'
  2. '2 whatever'
  3. '3 whatever'

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

于 2009-08-23T16:56:37.427 に答える
0

の最初の引数t()はリテラル文字列です。関数呼び出し ast("Your favorite color is $color")は関数にリテラル文字列を渡さず、翻訳する文字列を抽出するスクリプトは翻訳する文字列を抽出できません。実際には、抽出スクリプトは"Your favorite color is $color"(変数が文字列で置き換えられていないことに注意して) 抽出しますが、それは実行時に に渡される文字列ではありませんt()

于 2009-12-16T21:14:25.043 に答える