Variables in PHP gettext

11,669

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');
Share:
11,669
Terence Eden
Author by

Terence Eden

Long haired geek.

Updated on June 11, 2022

Comments

  • Terence Eden
    Terence Eden almost 2 years

    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?

  • Terence Eden
    Terence Eden over 11 years
    Well that was easy! Thanks. Have edited to show sprintf. printf just shows the length.
  • Luca Rainone
    Luca Rainone over 11 years
    Sprintf? Why? Printf print the string and sprintf return it. Maybe you write echo sprintf.
  • TommyAutoMagically
    TommyAutoMagically almost 11 years
    @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.)
  • Thomas Rbt
    Thomas Rbt almost 7 years
    Can do do something like this with <?= __('key') ?>?