16

Possible Duplicate:
How does gettext handle dynamic content?

I'm using PHP's gettext. I want to translate a sentence which has a variable in it. Is this possible?

For example, in English:

Are you sure you want to block Alice?

(Where "Alice" is the user's name.)

But, in German, the subject does not appear at the end of the sentence.

Sind Sie sicher, dass Sie Alice blockieren?

In messages.po, I have

msgid "BLOCK"
msgstr "Are you sure you want to block"

But I see no way to pass one or more variables. Is this possible?

Community
  • 1
  • 1
Terence Eden
  • 14,034
  • 3
  • 48
  • 89
  • 2
    Placeholdrs like `%s` and `sprintf` or alike are commonly used for that. Also you should not use ABBReviations for the gettext source string. Use plain English for the untranslated messages. Messages seldomly change in practice, and the gettext catalogs can be easily adapted if it ever happens. – mario Oct 12 '12 at 14:12

1 Answers1

37

poedit recognizes the vars.

msgid "Are you sure you want to block %s?"
msgstr "Sind Sie sicher, dass Sie %s blockieren?"

and in PHP

sprintf(_('Are you sure you want to block %s?'),'Alice');
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
  • Well that was easy! Thanks. Have edited to show sprintf. printf just shows the length. – Terence Eden Oct 12 '12 at 15:09
  • 2
    Sprintf? Why? Printf print the string and sprintf return it. Maybe you write `echo sprintf`. – Luca Rainone Oct 12 '12 at 17:05
  • 2
    @chumkiu - `sprintf()` is probably used here for reasons that are irrelevant to gettext and localization. (The app itself probably needed to store/manipulate the resulting string, not output it directly to stdout.) – rinogo Aug 07 '13 at 18:22
  • Can do do something like this with `= __('key') ?>`? – Thomas Rbt May 10 '17 at 08:05