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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
<?php
/**
* SquirrelMail internal gettext functions
*
* Alternate to the system's built-in gettext. Relies on .po files (can't read
* .mo easily). Uses the session for caching (speed increase). Possible use in
* other PHP scripts? The only SM-specific thing is $sm_language, I think.
*
* @link http://www.php.net/gettext Original php gettext manual
* @copyright 1999-2012 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: gettext.php 14248 2012-01-02 00:18:17Z pdontthink $
* @since 1.1.2
* @package squirrelmail
* @subpackage i18n
*/
/** Almost everything requires global.php... */
require_once(SM_PATH . 'functions/global.php');
global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
$gettext_php_translateStrings, $gettext_php_loaded_language,
$gettext_php_short_circuit;
if (! isset($gettext_php_loaded)) {
$gettext_php_loaded = false;
sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
}
if (! isset($gettext_php_domain)) {
$gettext_php_domain = '';
sqsession_register($gettext_php_domain, 'gettext_php_domain');
}
if (! isset($gettext_php_dir)) {
$gettext_php_dir = '';
sqsession_register($gettext_php_dir, 'gettext_php_dir');
}
if (! isset($gettext_php_translateStrings)) {
$gettext_php_translateStrings = array();
sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
}
if (! isset($gettext_php_loaded_language)) {
$gettext_php_loaded_language = '';
sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
}
if (! isset($gettext_php_short_circuit)) {
$gettext_php_short_circuit = false;
sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
}
/**
* Converts .po file into array and stores it in session.
*
* Used internally by _($str) function
*
* @internal function is used internally by functions/gettext.php code
*/
function gettext_php_load_strings() {
global $squirrelmail_language, $gettext_php_translateStrings,
$gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
$gettext_php_loaded_language, $gettext_php_short_circuit;
/*
* $squirrelmail_language gives 'en' for English, 'de' for German,
* etc. I didn't wanna use getenv or similar, but you easily could
* change my code to do that.
*/
$gettext_php_translateStrings = array();
$gettext_php_short_circuit = false; /* initialization */
$filename = $gettext_php_dir;
if (substr($filename, -1) != '/')
$filename .= '/';
$filename .= $squirrelmail_language . '/LC_MESSAGES/' .
$gettext_php_domain . '.po';
$file = @fopen($filename, 'r');
if ($file == false) {
/* Uh-ho -- we can't load the file. Just fake it. :-)
This is also for English, which doesn't use translations */
$gettext_php_loaded = true;
$gettext_php_loaded_language = $squirrelmail_language;
/* Avoid fuzzy matching when we didn't load strings */
$gettext_php_short_circuit = true;
return;
}
$key = '';
$SkipRead = false;
while (! feof($file)) {
if (! $SkipRead) {
$line = trim(fgets($file, 4096));
} else {
$SkipRead = false;
}
if (preg_match('/^msgid "(.*)"$/', $line, $match)) {
if ($match[1] == '') {
/*
* Potential multi-line
* msgid ""
* "string string "
* "string string"
*/
$key = '';
$line = trim(fgets($file, 4096));
while (preg_match('/^[ ]*"(.*)"[ ]*$/', $line, $match)) {
$key .= $match[1];
$line = trim(fgets($file, 4096));
}
$SkipRead = true;
} else {
/* msgid "string string" */
$key = $match[1];
}
} elseif (preg_match('/^msgstr "(.*)"$/', $line, $match)) {
if ($match[1] == '') {
/*
* Potential multi-line
* msgstr ""
* "string string "
* "string string"
*/
$gettext_php_translateStrings[$key] = '';
$line = trim(fgets($file, 4096));
while (preg_match('/^[ ]*"(.*)"[ ]*$/', $line, $match)) {
$gettext_php_translateStrings[$key] .= $match[1];
$line = trim(fgets($file, 4096));
}
$SkipRead = true;
} else {
/* msgstr "string string" */
$gettext_php_translateStrings[$key] = $match[1];
}
$gettext_php_translateStrings[$key] =
stripslashes($gettext_php_translateStrings[$key]);
/* If there is no translation, just use the untranslated string */
if ($gettext_php_translateStrings[$key] == '') {
$gettext_php_translateStrings[$key] = $key;
}
$key = '';
}
}
fclose($file);
$gettext_php_loaded = true;
$gettext_php_loaded_language = $squirrelmail_language;
}
/**
* Alternative php gettext function (short form)
*
* @link http://www.php.net/function.gettext
*
* @param string $str English string
* @return string translated string
*/
function _($str) {
global $gettext_php_loaded, $gettext_php_translateStrings,
$squirrelmail_language, $gettext_php_loaded_language,
$gettext_php_short_circuit;
if (! $gettext_php_loaded ||
$gettext_php_loaded_language != $squirrelmail_language) {
gettext_php_load_strings();
}
/* Try finding the exact string */
if (isset($gettext_php_translateStrings[$str])) {
return $gettext_php_translateStrings[$str];
}
/* See if we should short-circuit */
if ($gettext_php_short_circuit) {
$gettext_php_translateStrings[$str] = $str;
return $str;
}
/* don't do fuzzy matching for strings with sprintf() formating */
if (! preg_match('/\%[\%bcdeufFosxX]/',$str)) {
/* Look for a string that is very close to the one we want
* Very computationally expensive */
$oldPercent = 0;
$oldStr = '';
$newPercent = 0;
foreach ($gettext_php_translateStrings as $k => $v) {
similar_text($str, $k, $newPercent);
if ($newPercent > $oldPercent) {
$oldStr = $v;
$oldPercent = $newPercent;
}
}
/* Require 80% match or better
* Adjust to suit your needs */
if ($oldPercent > 80) {
/* Remember this so we don't need to search again */
$gettext_php_translateStrings[$str] = $oldStr;
return $oldStr;
}
}
/* Remember this so we don't need to search again */
$gettext_php_translateStrings[$str] = $str;
return $str;
}
/**
* Alternative php bindtextdomain function
*
* Sets path to directory containing domain translations
*
* @link http://www.php.net/function.bindtextdomain
* @param string $name gettext domain name
* @param string $dir directory that contains all translations
* @return string path to translation directory
*/
function bindtextdomain($name, $dir) {
global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
if ($gettext_php_domain != $name) {
$gettext_php_domain = $name;
$gettext_php_loaded = false;
}
if ($gettext_php_dir != $dir) {
$gettext_php_dir = $dir;
$gettext_php_loaded = false;
}
return $dir;
}
/**
* Alternative php textdomain function
*
* Sets default domain name
*
* @link http://www.php.net/function.textdomain
* @param string $name gettext domain name
* @return string gettext domain name
*/
function textdomain($name = false) {
global $gettext_php_domain, $gettext_php_loaded;
if ($name != false && $gettext_php_domain != $name) {
$gettext_php_domain = $name;
$gettext_php_loaded = false;
}
return $gettext_php_domain;
}
|