sprintf() in lang()

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@84 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2007-07-09 15:28:37 +00:00
parent e99f61f0d7
commit 89436789ff
4 changed files with 57 additions and 33 deletions

View file

@ -1,11 +1,25 @@
<?php
function add_apo_slashes($s) {
return addcslashes($s, "\\'");
}
function remove_lang($match) {
$s = lang(strtr($match[2], array("\\'" => "'", "\\\\" => "\\")));
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, "<?php") <= substr_count($return, "?>") && !$match[5]) {
$return .= "<?php\n";
@ -32,11 +46,10 @@ $filename = "phpMinAdmin.php";
$file = file_get_contents("index.php");
if ($_SESSION["lang"]) {
$filename = "phpMinAdmin-$_SESSION[lang].php";
$file = str_replace("include \"./lang.inc.php\";\n", "", $file);
}
$file = preg_replace_callback('~(<\\?php)?\\s*(include|require)(_once)? "([^"]*)";(\\s*\\?>)?~', '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("<?php switch_lang(); ?>\n", "", $file);
$file = str_replace("<?php echo get_lang(); ?>", $_SESSION["lang"], $file);
}

View file

@ -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]));

View file

@ -1,9 +1,10 @@
<?php
function page_header($title) {
global $LANG;
header("Content-Type: text/html; charset=utf-8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?php echo get_lang(); ?>">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?php echo $LANG; ?>">
<head>
<title><?php echo lang('phpMinAdmin') . " - $title"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

View file

@ -1,24 +1,11 @@
<?php
function get_lang() {
if (strlen($_SESSION["lang"])) {
return $_SESSION["lang"];
}
$langs = lang();
$return = preg_replace('~[,;].*~', '', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
if (!in_array($return, $langs)) { //! try next languages
$return = preg_replace('~-.*~', '', $return);
if (!in_array($return, $langs)) {
$return = "en";
}
}
return $return;
}
function lang($idf = null) {
function lang($idf = null, $number = null) {
global $LANG;
static $translations = array(
'en' => 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";
}
}
}