Skip to content

Instantly share code, notes, and snippets.

@norman784
Last active December 16, 2015 09:49
Show Gist options
  • Save norman784/5415468 to your computer and use it in GitHub Desktop.
Save norman784/5415468 to your computer and use it in GitHub Desktop.
Functions that I use to localize my apps
<?php
function i18n($file = null) {
global $transtation;
$transtation = array();
if (!isset($_SESSION['lang'])) $_SESSION['lang'] = 'en';
if (!$file) $file = 'default';
$file = dirname(__FILE__) . "/locale/{$_SESSION['lang']}/{$file}.po";
if (!file_exists($file)) return;
$po = file($file);
foreach ($po as $line):
if (substr($line, 0, 5) == 'msgid') {
$current = trim(substr(trim(substr($line, 5)), 1, -1));
}
if (substr($line, 0, 6) == 'msgstr') {
$transtation[$current] = trim(substr(trim(substr($line, 6)), 1, -1));
}
endforeach;
}
function __($msgid) {
global $transtation;
if (!isset($transtation[$msgid]) || $transtation[$msgid] == '') {
return $msgid;
} else {
return $transtation[$msgid];
}
}
// Just some random function to get the current key of the key/value from the PO file
function __KEY($key_or_value) {
global $transtation;
foreach ($transtation as $key=>$value):
if ($key == $key_or_value || $value == $key_or_value) return $key;
endforeach;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment