Last active
December 16, 2015 09:49
-
-
Save norman784/5415468 to your computer and use it in GitHub Desktop.
Functions that I use to localize my apps
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 | |
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