From 89436789ff51f5953dad9e84c0c9385b7d7a96ae Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Mon, 9 Jul 2007 15:28:37 +0000 Subject: [PATCH] sprintf() in lang() git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@84 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- _compile.php | 21 ++++++++++++++++---- _lang.php | 13 ++++++++----- design.inc.php | 3 ++- lang.inc.php | 53 ++++++++++++++++++++++++++++---------------------- 4 files changed, 57 insertions(+), 33 deletions(-) diff --git a/_compile.php b/_compile.php index 5be824ee..627e78b8 100644 --- a/_compile.php +++ b/_compile.php @@ -1,11 +1,25 @@ "'", "\\\\" => "\\"))); - return ($match[1] && $match[3] ? $s : "$match[1]'" . addcslashes($s, "\\'") . "'$match[3]"); + $s = lang(strtr($match[2], array("\\'" => "'", "\\\\" => "\\")), false); + if ($match[3] == ",") { + return "$match[1]" . (is_array($s) ? "lang(array('" . implode("', '", array_map('add_apo_slashes', $s)) . "')," : "sprintf('" . add_apo_slashes($s) . "',"); + } + return ($match[1] && $match[4] ? $s : "$match[1]'" . add_apo_slashes($s) . "'$match[4]"); } function put_file($match) { $return = file_get_contents($match[4]); + if ($match[4] == "./lang.inc.php") { + if (!$_SESSION["lang"]) { + $return = str_replace("\tif (\$number === false && \$translation) {\n\t\treturn \$translation; // used in _compile.php\n\t}\n", "", $return); + } elseif (preg_match("~case '$_SESSION[lang]': (.*) break;~", $return, $match) || preg_match("~default: (.*)~", $return, $match)) { + return "$match[1]\nfunction lang(\$ar, \$number) {\n\t$match[1]\n\treturn \$ar[\$pos];\n}\n$match[5]"; + } + } $return = preg_replace("~\\?>\n?\$~", '', $return); if (substr_count($return, "") && !$match[5]) { $return .= ")?~', 'put_file', $file); if ($_SESSION["lang"]) { - $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']*|\\\\.)+)'\\)(;\\s*\\?>)?~s", 'remove_lang', $file); + $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']*|\\\\.)+)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file); $file = str_replace("\n", "", $file); $file = str_replace("", $_SESSION["lang"], $file); } diff --git a/_lang.php b/_lang.php index f82324cb..45a4c1ab 100644 --- a/_lang.php +++ b/_lang.php @@ -7,8 +7,9 @@ if ($_SERVER["argc"] > 1) { $messages_all = array(); foreach (glob("*.php") as $filename) { $file = file_get_contents($filename); - preg_match_all("~lang\\(('(?:[^\\\\']*|\\\\.)+')[),]~s", $file, $matches); - $messages_all += array_flip($matches[1]); + if (preg_match_all("~lang\\(('(?:[^\\\\']*|\\\\.)+')([),])~", $file, $matches)) { + $messages_all += array_combine($matches[1], $matches[2]); + } } $file = file_get_contents("lang.inc.php"); @@ -25,9 +26,11 @@ foreach (array_reverse($translations[2], true) as $key => $translation) { $s .= "$match[1]// $match[2],\n"; } } - if ($translations[1][$key][0] != 'en') { - foreach($messages as $key => $val) { - $s .= "\t\t\t$key => '',\n"; + foreach($messages as $idf => $val) { + if ($val == ",") { + $s .= "\t\t\t$idf => array(),\n"; + } elseif ($translations[1][$key][0] != 'en') { + $s .= "\t\t\t$idf => '',\n"; } } $file = substr_replace($file, $s, $translation[1], strlen($translation[0])); diff --git a/design.inc.php b/design.inc.php index 91cf1d7c..99d6c784 100644 --- a/design.inc.php +++ b/design.inc.php @@ -1,9 +1,10 @@ - + <?php echo lang('phpMinAdmin') . " - $title"; ?> diff --git a/lang.inc.php b/lang.inc.php index 644e6d00..6e1d014a 100644 --- a/lang.inc.php +++ b/lang.inc.php @@ -1,24 +1,11 @@ array( 'Query executed OK, %d row(s) affected.' => array('Query executed OK, %d row affected.', 'Query executed OK, %d rows affected.'), '%d byte(s)' => array('%d byte', '%d bytes'), + 'Routine has been called, %d row(s) affected.' => array('Routine has been called, %d row affected.', 'Routine has been called, %d rows affected.'), ), 'cs' => array( 'Login' => 'Přihlásit se', @@ -110,22 +97,29 @@ function lang($idf = null) { 'Unable to upload a file.' => 'Nepodařilo se nahrát soubor.', 'File upload' => 'Nahrání souboru', 'File uploads are disabled.' => 'Nahrávání souborů není povoleno.', + 'Routine has been called, %d row(s) affected.' => array('Procedura byla zavolána, byl změněn %d záznam.', 'Procedura byla zavolána, byly změněny %d záznamy.', 'Procedura byla zavolána, bylo změněno %d záznamů.'), + 'Call' => 'Zavolat', + 'Error during calling' => 'Chyba při volání', ), ); if (!isset($idf)) { return array_keys($translations); } - $lang = get_lang(); - $translation = $translations[$lang][$idf]; + $translation = $translations[$LANG][$idf]; + if ($number === false && $translation) { + return $translation; // used in _compile.php + } $args = func_get_args(); - if (is_array($translation)) { - switch ($lang) { - case 'cs': $pos = ($args[1] == 1 ? 0 : (!$args[1] || $args[1] >= 5 ? 2 : 1)); break; - default: $pos = ($args[1] == 1 ? 0 : 1); + if (is_array($translation) && $translation) { + switch ($LANG) { + case 'cs': $pos = ($number == 1 ? 0 : (!$number || $number >= 5 ? 2 : 1)); break; + default: $pos = ($number == 1 ? 0 : 1); } $translation = $translation[$pos]; } - $args[0] = (strlen($translation) ? $translation : $idf); + if ($translation) { + $args[0] = $translation; + } return call_user_func_array('sprintf', $args); } @@ -141,3 +135,16 @@ function switch_lang() { if (isset($_GET["lang"])) { $_SESSION["lang"] = $_GET["lang"]; } + +if (strlen($_SESSION["lang"])) { + $LANG = $_SESSION["lang"]; +} else { + $langs = lang(); + $LANG = preg_replace('~[,;].*~', '', $_SERVER["HTTP_ACCEPT_LANGUAGE"]); + if (!in_array($LANG, $langs)) { //! try next languages + $LANG = preg_replace('~-.*~', '', $LANG); + if (!in_array($LANG, $langs)) { + $LANG = "en"; + } + } +}