Permalink
Browse files

Compress translations

  • Loading branch information...
1 parent 8db73ad commit 7dea0d370dbcf9ca261854c75b7e0f9b26225c16 @vrana committed Sep 2, 2012
Showing with 86 additions and 9 deletions.
  1. +39 −1 adminer/include/lang.inc.php
  2. +1 −0 changes.txt
  3. +46 −8 compile.php
@@ -78,7 +78,45 @@ function switch_lang() {
echo "</div>\n</form>\n";
}
-if (isset($_GET["lang"])) {
+
+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($_GET["lang"])) { //! CSRF
$_COOKIE["adminer_lang"] = $_GET["lang"];
$_SESSION["lang"] = $_GET["lang"]; // cookies may be disabled
}
View
@@ -4,6 +4,7 @@ Edit strings with \n in textarea
Time out long running database list and select count
Use VALUES() in INSERT+UPDATE export
Style logout button as link
+Compress translations
selectQueryBuild() method (customization)
Serbian translation
View
@@ -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 @@ function compile_file($match) {
}
}
$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);

0 comments on commit 7dea0d3

Please sign in to comment.