Adding i18n support to PHP using gettext?

I have always heard of gettext - I know that it is some kind of unix command to search for a translation based on the provided string argument and then creates a .pot file, but someone can explain to me in unprofessional terms how to do this take care in the web frame?

I could take a look at how some of the established frameworks did this, but a layman’s explanation would help, because it would just help clear the picture a bit more before I really delve into things to provide my own solution.

+5
source share
1 answer

gettext , , .

- "" .

-

echo _("Hello, world!");

( % s, printf)

french
msgid "Hello, world!"
msgstr "Salut, monde!"
msgid "My name is %s"
msgstr "Mon nom est %s"

italian
msgid "Hello, world!"
msgstr "Ciao, mondo!"
msgid "My name is %s"
msgstr "Il mio nome è %s"

,

  • gettext (gettext(), ngettext(), _())
  • xgettext (* nix) php .po.
  • poedit, .po.
  • msgfmt (* nix) .mo .po
  • .mo ,

/de_DE/LC_MESSAGES/myPHPApp.mo

/en_EN/LC_MESSAGES/myPHPApp.mo

/it_IT/LC_MESSAGES/myPHPApp.mo

, php script, ,

php

<?php
// Set language to German
setlocale(LC_ALL, 'de_DE');

// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");

// Choose domain
textdomain("myPHPApp");

// Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now

// Print a test message
echo gettext("Welcome to My PHP Application");

// Or use the alias _() for gettext()
echo _("Have a nice day");
?>

php

+12

All Articles