Compress translations

This commit is contained in:
Jakub Vrana 2012-09-02 06:44:32 -07:00
parent 09d81ffee9
commit 5d257861e1
3 changed files with 84 additions and 8 deletions

View file

@ -78,6 +78,43 @@ function switch_lang() {
echo "</div>\n</form>\n";
}
function lzw_decompress($binary) {
// convert binary string to codes
$dictionary_count = 256;
$bits = 8; // ceil(log($dictionary_count, 2))
$codes = array();
$rest = 0;
$rest_length = 0;
for ($i=0; $i < strlen($binary); $i++) {
$rest = ($rest << 8) + ord($binary[$i]);
$rest_length += 8;
if ($rest_length >= $bits) {
$rest_length -= $bits;
$codes[] = $rest >> $rest_length;
$rest &= (1 << $rest_length) - 1;
$dictionary_count++;
if ($dictionary_count >> $bits) {
$bits++;
}
}
}
// decompression
$dictionary = range("\0", "\xFF");
$return = "";
foreach ($codes as $i => $code) {
$element = $dictionary[$code];
if (!isset($element)) {
$element = $word . $word[0];
}
$return .= $element;
if ($i) {
$dictionary[] = $word . $element[0];
}
$word = $element;
}
return $return;
}
if (isset($_POST["lang"]) && $_SESSION["token"] == $_POST["token"]) { // $token and $error not yet available
cookie("adminer_lang", $_POST["lang"]);
$_SESSION["lang"] = $_POST["lang"]; // cookies may be disabled

View file

@ -6,6 +6,7 @@ Use VALUES() in INSERT+UPDATE export
Style logout button as link
Ctrl+click and Shift+click on button opens form to a blank window
Switch language by POST
Compress translations
selectQueryBuild() method (customization)
Serbian translation

View file

@ -61,12 +61,49 @@ function lang(\$translation, \$number) {
}
}
function lzw_compress($string) {
// compression
$dictionary = array_flip(range("\0", "\xFF"));
$word = "";
$codes = array();
for ($i=0; $i <= strlen($string); $i++) {
$x = $string[$i];
if (strlen($x) && isset($dictionary[$word . $x])) {
$word .= $x;
} elseif ($i) {
$codes[] = $dictionary[$word];
$dictionary[$word . $x] = count($dictionary);
$word = $x;
}
}
// convert codes to binary string
$dictionary_count = 256;
$bits = 8; // ceil(log($dictionary_count, 2))
$return = "";
$rest = 0;
$rest_length = 0;
foreach ($codes as $code) {
$rest = ($rest << $bits) + $code;
$rest_length += $bits;
$dictionary_count++;
if ($dictionary_count >> $bits) {
$bits++;
}
while ($rest_length > 7) {
$rest_length -= 8;
$return .= chr($rest >> $rest_length);
$rest &= (1 << $rest_length) - 1;
}
}
return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : "");
}
function put_file_lang($match) {
global $lang_ids, $project, $langs;
if ($_SESSION["lang"]) {
return "";
}
$return = "";
$all_translations = array();
foreach ($langs as $lang => $val) {
include dirname(__FILE__) . "/adminer/lang/$lang.inc.php"; // assign $translations
$translation_ids = array_flip($lang_ids); // default translation
@ -75,13 +112,14 @@ function put_file_lang($match) {
$translation_ids[$lang_ids[$key]] = $val;
}
}
$return .= "\tcase \"$lang\": \$translations = array(";
foreach ($translation_ids as $val) {
$return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", ";
}
$return = substr($return, 0, -2) . "); break;\n";
$all_translations[$lang] = $translation_ids;
}
return "switch (\$LANG) {\n$return}\n";
return '$translations = &$_SESSION["translations"];
if ($_GET["lang"] || !$translations) {
$all_translations = unserialize(lzw_decompress(\'' . add_apo_slashes(lzw_compress(serialize($all_translations))) . '\'));
$translations = $all_translations[$LANG];
}
';
}
function short_identifier($number, $chars) {
@ -281,8 +319,8 @@ foreach (array("adminer", "editor") as $project) {
}
}
$file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
$file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
$file = str_replace("\r", "", $file);
$file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
if ($_SESSION["lang"]) {
// single language version
$file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);