Internationalizing the a website, you run into a problem where you don’t know what strings you’ve parsed out for localization or not.
For these tasks, my favorite language is “.” I use that code to replace all strings with some XX’s. Now any strings (or images) I missed are immediately evident.
Keeping the zxx localization file up to date is easy to update…
// {{{ l10n_upgrader($pofile,$callback)
/**
* Allows you to upgrade a PO file automatically.
*
* Search for all "" (two double quotes) and translate these.
*
* @param string $pofile the messages.po file to upgrade
* @param string $callback the callback function that does the string lookup
* @return array
*/
function l10n_upgrader($pofile,$callback='')
{
if ($callback) {
$data = file_get_contents($pofile);
$matches = array();
$data = preg_replace_callback("!^msgid \"(.+?)\"\nmsgstr \"\"!m", $callback, $data); // if I add s it matches too aggressively
//unlink($pofile);
file_put_contents($pofile,$data);
}
// generate output
//msgfmt message.po
$exec = sprintf(
'cd %s;msgfmt %s',
escapeshellarg(dirname($pofile)),
escapeshellarg('messages.po')
);
pass($exec); //erroring out this is an exec function
}
// }}}
// {{{ l10n_ZXXer($matches)
/**
* Add a bunch of XXXX's to a string
*
* @param array $matches
* @return string
*/
function l10n_ZXXer($matches)
{
return sprintf(
"msgid \"%s\"\nmsgstr \"%s\"",
$matches[1],
str_pad('',strlen($matches[1]),'XXXX ')
);
}
// }}}
l10n_upgrader($gt_dird.'zxx_XX/LC_MESSAGES/messages.po', 'l10n_ZXXer');
You may have some trouble getting things working, so remember that gettext uses your system to set the language. If your system doesn’t have zxx installed, you can’t just flip your locale over and expect it to work. As a quick hack for develement, just symlink the directory in your dev machine over to . I’m currently using “zu_ZA” which will be okay until Tagged localizes for Zulu.
Also remember that your web server caches gettext strings, so be sure to shut down the webserver to free the file lock before updating your .pot file.
Improving your linguisticness
l10n_ZXXer() above is not that clever. It won’t handle the cases where you have nested sprintf() substitutions. But you can imagine improving it. Here are some ideas of different fake languages:
- Multiply
strlen() computation by 1.2 in order to handle spacing issues for localizing to German.
- Replace many of the letters with the letters in the cyrillic alphabet. This way you know unicode is correctly supported. so your pattern replace can be easier and strings don’t get smashed.
- Replace the code to run it through a filter so it’s linguistically recognizable. Maybe turn your site into 133t-speak. Personally, I used ebonics from my Plaxo days. Careful though, while it is amusing to see littered with argot, fo’ shizzle. Apparently, I was
committing a lot of cuss words into the code base. It’s times like that you’re grateful for the symlink trick above preventing your users from stumbling on these fake languages.
Truncated by Planet PHP, read more at the original (another 1347 bytes)