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

View file

@ -7,8 +7,9 @@ if ($_SERVER["argc"] > 1) {
$messages_all = array(); $messages_all = array();
foreach (glob("*.php") as $filename) { foreach (glob("*.php") as $filename) {
$file = file_get_contents($filename); $file = file_get_contents($filename);
preg_match_all("~lang\\(('(?:[^\\\\']*|\\\\.)+')[),]~s", $file, $matches); if (preg_match_all("~lang\\(('(?:[^\\\\']*|\\\\.)+')([),])~", $file, $matches)) {
$messages_all += array_flip($matches[1]); $messages_all += array_combine($matches[1], $matches[2]);
}
} }
$file = file_get_contents("lang.inc.php"); $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"; $s .= "$match[1]// $match[2],\n";
} }
} }
if ($translations[1][$key][0] != 'en') { foreach($messages as $idf => $val) {
foreach($messages as $key => $val) { if ($val == ",") {
$s .= "\t\t\t$key => '',\n"; $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])); $file = substr_replace($file, $s, $translation[1], strlen($translation[0]));

View file

@ -1,9 +1,10 @@
<?php <?php
function page_header($title) { function page_header($title) {
global $LANG;
header("Content-Type: text/html; charset=utf-8"); 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"> <!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> <head>
<title><?php echo lang('phpMinAdmin') . " - $title"; ?></title> <title><?php echo lang('phpMinAdmin') . " - $title"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

View file

@ -1,24 +1,11 @@
<?php <?php
function get_lang() { function lang($idf = null, $number = null) {
if (strlen($_SESSION["lang"])) { global $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) {
static $translations = array( static $translations = array(
'en' => array( 'en' => array(
'Query executed OK, %d row(s) affected.' => array('Query executed OK, %d row affected.', 'Query executed OK, %d rows affected.'), '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'), '%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( 'cs' => array(
'Login' => 'Přihlásit se', 'Login' => 'Přihlásit se',
@ -110,22 +97,29 @@ function lang($idf = null) {
'Unable to upload a file.' => 'Nepodařilo se nahrát soubor.', 'Unable to upload a file.' => 'Nepodařilo se nahrát soubor.',
'File upload' => 'Nahrání souboru', 'File upload' => 'Nahrání souboru',
'File uploads are disabled.' => 'Nahrávání souborů není povoleno.', '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)) { if (!isset($idf)) {
return array_keys($translations); 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(); $args = func_get_args();
if (is_array($translation)) { if (is_array($translation) && $translation) {
switch ($lang) { switch ($LANG) {
case 'cs': $pos = ($args[1] == 1 ? 0 : (!$args[1] || $args[1] >= 5 ? 2 : 1)); break; case 'cs': $pos = ($number == 1 ? 0 : (!$number || $number >= 5 ? 2 : 1)); break;
default: $pos = ($args[1] == 1 ? 0 : 1); default: $pos = ($number == 1 ? 0 : 1);
} }
$translation = $translation[$pos]; $translation = $translation[$pos];
} }
$args[0] = (strlen($translation) ? $translation : $idf); if ($translation) {
$args[0] = $translation;
}
return call_user_func_array('sprintf', $args); return call_user_func_array('sprintf', $args);
} }
@ -141,3 +135,16 @@ function switch_lang() {
if (isset($_GET["lang"])) { if (isset($_GET["lang"])) {
$_SESSION["lang"] = $_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";
}
}
}