Skip to content

Instantly share code, notes, and snippets.

@idleberg
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idleberg/2de69f834d326b731814 to your computer and use it in GitHub Desktop.
Save idleberg/2de69f834d326b731814 to your computer and use it in GitHub Desktop.
<?php
session_start();
if (isset($_GET["locale"])) {
$locale = $_GET["locale"];
} else if (isset($_SESSION["locale"])) {
$locale = $_SESSION["locale"];
} else {
$locale = "en_US";
}
putenv("LANG=" . $locale);
setlocale(LC_ALL, $locale);
// name matches example.mo
$domain = "example";
// name matches locale folder
bindtextdomain($domain, "Locale");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
// name matches example2.mo
$domain2 = "example2";
// name matches locale folder
bindtextdomain($domain2, "Locale");
bind_textdomain_codeset($domain2, 'UTF-8');
// Examples:
// _() is an alias of gettext()
echo _("Let's make the web multilingual");
var1 = "one";
var2 = "two";
echo sprintf(_("Let's use the variables %1$s and %2$s", $var1, $var2);
// dgettext() is similar to _(), but it also accepts a domain name if a string from a different textdomain should be used
echo dgettext($domain2, "Let's make the web multilingual");
// ngettext() is used when the plural form is based 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 when the count is 15
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment