Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of PHP file translated using gettext
<?php
session_start();
if (isset($_GET["locale"])) {
$locale = $_GET["locale"];
}
else if (isset($_SESSION["locale"])) {
$locale = $_SESSION["locale"];
}
else {
$locale = "en_UK";
}
putenv("LANG=" . $locale);
setlocale(LC_ALL, $locale);
$domain = "example";
bindtextdomain($domain, "Locale");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
$domain2 = "example2";
bindtextdomain($domain2, "Locale");
bind_textdomain_codeset($domain2, 'UTF-8');
$user = "Curious gettext tester";
// _() is an alias of gettext()
echo _("Let’s make the web multilingual.");
echo _("We connect developers and translators around the globe
on Lingohub for a fantastic localization experience.");
echo sprintf(_('Welcome back, %1$s! Your last visit was on %2$s', $user, date('l'));
// dgettext() is similar to _(), but it also accepts a domain name if a string from
// a domain other the one set by textdomain() needs to be displayed
echo dgettext("example2", "");
// ngettext() is used when the plural form of the message is dependent on the count
echo ngettext("%d page read.", "%d pages read.", 1); //outputs a form used for singular
echo ngettext("%d page read.", "%d pages read.", 15); //outputs a form used when the count is 15
?>

There's a missing bracket on line 35.

sp00n commented Jun 6, 2014

AFAIK it should be "en_GB" instead of "en_UK".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment