-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygettext.php
executable file
·72 lines (63 loc) · 2.45 KB
/
mygettext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
//mygettext.php is my implementation of gettext, because I couldn't get php-gettext to work and
//GNU gettext isn't automatically included in the PHP build. Because iconv_get_encoding()
//doesn't query the OS to find out what kind of output encoding is needed, I had to
//include some ugly code to find it.
define('DEF_SOURCE_LANG', 'es');
define('DEF_INTERFACE_LANG', 'en');
define('DEF_CHARSET', 'ISO-8859-1');
$bDOS = false; //if true, OS contains MS-DOS, otherwise assume some form of UNIX
$sCharSet = DEF_CHARSET; //set the default character set for output.
$sInterfaceLang = DEF_INTERFACE_LANG; //set the default interface language
$aGetText = NULL; //array which will hold the gettext translations
//Because PHP doesn't bother looking up the interface language of the computer,
//I have created this ugly work-around to get it.
if (isset($_ENV['OS']) && !strncasecmp($_ENV['OS'], 'WINDOWS_', 8)) //if MS-Windows
{
$bDOS = True;
$sCharSet = 'CP850';
if (isset($GLOBALS['ProgramFiles']))
{
if (strpos($GLOBALS['ProgramFiles'], 'Program Files') !== false)
$sInterfaceLang = "en";
elseif (strpos($GLOBALS['ProgramFiles'], 'Archivos de Programa') !== false)
$sInterfaceLang = "es";
elseif (strpos($GLOBALS['ProgramFiles'], 'Arquivos de Programa') !== false)
$sInterfaceLang = "pt";
}
}
elseif (isset($_ENV['LANG'])) //if a Linux/UNIX/OS X system
{
//get user interface language
$sLang = strtolower(substr($_ENV['LANG'], 0, 2));
if ($sLang == DEF_SOURCE_LANG || file_exists(pathinfo(realpath($argv[0]),
PATHINFO_DIRNAME) . '/locale/' . $sLang))
{
$sInterfaceLang = $sLang;
}
//get user interface character set
//LANG usually has the form: "en_US.UTF-8", so get whatever follows the period
$iChSet = strpos($_ENV['LANG'], '.');
if($iChSet !== false && ++$iChSet < strlen($_ENV['LANG']))
{
$sCharSet = substr($_ENV['LANG'], $iChSet);
//if unrecognized character set, then set back to default character set
if (strpos($sCharSet, 'UTF') !== 0 && strpos($sCharSet, 'ISO') !== 0 &&
strpos($sCharSet, 'CP') !== 0 && strpos($sCharSet, 'UCS') !== 0 &&
stripos($sCharSet, 'MAC') !== 0)
{
$sCharSet = DEF_CHARSET;
}
}
}
//equivalent to the gettext() or _() functions
function __($str)
{
global $aGetText, $sInterfaceLang;
$sRet = $sInterfaceLang == DEF_SOURCE_LANG ? $str : $aGetText[$str];
if ($sRet == '') //will match if passed an empty string or a NULL or O
return $str;
else
return $sRet;
}
?>