Created
October 3, 2011 11:50
-
-
Save maximevalette/1258947 to your computer and use it in GitHub Desktop.
Utilisation pratique de gettext avec PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
$locale = "fr_FR"; | |
putenv("LC_ALL=".$locale); | |
setlocale(LC_ALL, $locale); | |
bindtextdomain("messages", "./locale"); | |
textdomain("messages"); | |
$m = new Memcache; | |
$m->addServer('localhost',9000); | |
$m->set('Hello World','Bonjour Monde'); | |
if ($argv[1] == 'gettext') { | |
function __($string) { | |
if ($argv[2] == 'cached') { | |
if (!is_string($_SESSION[$string])) $_SESSION[$string] = _($string); | |
return $_SESSION[$string]; | |
} else { | |
return _($string); | |
} | |
} | |
} else { | |
function __($string) { | |
global $m; | |
if (!is_string($_SESSION[$string])) $_SESSION[$string] = $m->get($string); | |
return $_SESSION[$string]; | |
} | |
} | |
ob_start(); | |
for ($i=1;$i<=1000000;$i++) { | |
if ($argv[1] == 'gettext' && $argv[2] == 'direct') { | |
echo _('Hello World') . "\n"; | |
} else { | |
echo __('Hello World') . "\n"; | |
} | |
} | |
ob_end_clean(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$locale = "fr_FR"; | |
putenv("LC_ALL=".$locale); | |
setlocale(LC_ALL, $locale); | |
bindtextdomain("messages", "./locale"); | |
textdomain("messages"); | |
/// TRANSLATORS: Please leave %s as it is, because it is needed by the program. | |
/// Thank you for contributing to this project. | |
printf(_("My name is %s.\n"), $my_name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* mget() et mset() sont les fonctions pour récupérer et mettre en | |
mémoire sur le serveur memcache, afin de ne parser le fichier .po | |
qu'une seule fois par page. */ | |
function __($message) { | |
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1') { | |
// Si on est en local, on parse en direct le fichier .po | |
if (($lines=mget('lines'.$_SESSION['lang']))===null) { | |
$content = file_get_contents('./locale/'.$_SESSION['lang'].'/LC_MESSAGES/messages.po'); | |
preg_match_all('/msgid "(.+)"\nmsgstr "(.+)"/i',$content,$r); | |
foreach ($r[0] as $k => $v) { | |
$lines[str_replace('\"', '"', $r[1][$k])] = str_replace('\"', '"', $r[2][$k]); | |
} | |
mset($lines,'lines'.$_SESSION['lang']); | |
} | |
if (!empty($lines[$message])) return $lines[$message]; | |
} else { | |
// Sinon on utilise gettext pour de meilleures performances | |
return _($message); | |
} | |
return $message; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Création du fichier vide pour plaire à xgettext | |
echo '' > messages.po | |
# On parse tous les fichiers .php avec xgettext | |
find . -type f -iname "*.php" | xgettext -f - --add-comments=/ -L PHP --keyword=__ --from-code=UTF-8 | |
# On merge l'ancien fichier avec le nouveau | |
msgmerge --no-wrap -N old.po messages.po > new.po | |
# On garde les anciennes variables de l'ancien fichier, elles n'étaient pas là pour rien | |
sed 's/#~ //g' new.po > cleaned.po | |
# On remet le nouveau fichier à la place de l'ancien | |
mv cleaned.po old.po | |
# On supprime les fichiers intermédiaires | |
rm new.po messages.po |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment