]> git.joonet.de Git - adminer.git/commitdiff
Plugins: Allow formatting translations using Adminer\lang_format()
authorJakub Vrana <jakub@vrana.cz>
Fri, 4 Apr 2025 15:16:18 +0000 (17:16 +0200)
committerJakub Vrana <jakub@vrana.cz>
Fri, 4 Apr 2025 15:18:34 +0000 (17:18 +0200)
CHANGELOG.md
adminer/include/lang.inc.php
compile.php

index fdd63adc6293c0c177e69139c8c536bf70481ef9..7c1639391893e3a532605c047d32e453346582e4 100644 (file)
@@ -5,6 +5,7 @@
 - MySQL: Display number of found rows in group queries (regression from 5.1.1)
 - non-MySQL: Parse '--' without trailing space as comment in SQL command (bug SF-842)
 - CSS: Add logo
+- Plugins: Allow formatting translations using Adminer\lang_format()
 - New plugin: Set up driver, server and database in Adminer Editor
 
 ## Adminer 5.1.1 (released 2025-04-02)
index 71b8171354b453a53fc9556ce0352000cc4ec274..0c537631f69ebd359fc4a7ddf2423a2a46b44fea 100644 (file)
@@ -1,7 +1,48 @@
 <?php
 namespace Adminer;
 
-// not used in a single language version
+/** Translate string
+* @param literal-string $idf
+* @param float|string $number
+*/
+function lang(string $idf, $number = null): string {
+       // this is matched by compile.php
+       $args = func_get_args();
+       $args[0] = Lang::$translations[$idf] ?: $idf;
+       return call_user_func_array('Adminer\lang_format', $args);
+}
+
+/** Format translation, usable also by plugins
+* @param string|list<string> $translation
+* @param float|string $number
+*/
+function lang_format($translation, $number = null): string {
+       if (is_array($translation)) {
+               // this is matched by compile.php
+               $pos = ($number == 1 ? 0
+                       : (LANG == 'cs' || LANG == 'sk' ? ($number && $number < 5 ? 1 : 2) // different forms for 1, 2-4, other
+                       : (LANG == 'fr' ? (!$number ? 0 : 1) // different forms for 0-1, other
+                       : (LANG == 'pl' ? ($number % 10 > 1 && $number % 10 < 5 && $number / 10 % 10 != 1 ? 1 : 2) // different forms for 1, 2-4 except 12-14, other
+                       : (LANG == 'sl' ? ($number % 100 == 1 ? 0 : ($number % 100 == 2 ? 1 : ($number % 100 == 3 || $number % 100 == 4 ? 2 : 3))) // different forms for 1, 2, 3-4, other
+                       : (LANG == 'lt' ? ($number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 > 1 && $number / 10 % 10 != 1 ? 1 : 2)) // different forms for 1, 12-19, other
+                       : (LANG == 'lv' ? ($number % 10 == 1 && $number % 100 != 11 ? 0 : ($number ? 1 : 2)) // different forms for 1 except 11, other, 0
+                       : (in_array(LANG, array('bs', 'ru', 'sr', 'uk')) ? ($number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 > 1 && $number % 10 < 5 && $number / 10 % 10 != 1 ? 1 : 2)) // different forms for 1 except 11, 2-4 except 12-14, other
+                       : 1)))))))) // different forms for 1, other
+               ; // http://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
+               $translation = $translation[$pos];
+       }
+       $translation = str_replace("'", '’', $translation); // translations can contain HTML or be used in optionlist (we couldn't escape them here) but they can also be used e.g. in title='' //! escape plaintext translations
+       $args = func_get_args();
+       array_shift($args);
+       $format = str_replace("%d", "%s", $translation);
+       if ($format != $translation) {
+               $args[0] = format_number($number);
+       }
+       return vsprintf($format, $args);
+}
+
+// this is matched by compile.php
+// not used in a single language version from here
 
 /** Get available languages
 * @return string[]
@@ -56,37 +97,6 @@ function langs(): array {
        );
 }
 
-/** Translate string
-* @param literal-string $idf
-* @param float|string $number
-*/
-function lang(string $idf, $number = null): string {
-       // this is matched by compile.php
-       $translation = (Lang::$translations[$idf] ?: $idf);
-       if (is_array($translation)) {
-               // this is matched by compile.php
-               $pos = ($number == 1 ? 0
-                       : (LANG == 'cs' || LANG == 'sk' ? ($number && $number < 5 ? 1 : 2) // different forms for 1, 2-4, other
-                       : (LANG == 'fr' ? (!$number ? 0 : 1) // different forms for 0-1, other
-                       : (LANG == 'pl' ? ($number % 10 > 1 && $number % 10 < 5 && $number / 10 % 10 != 1 ? 1 : 2) // different forms for 1, 2-4 except 12-14, other
-                       : (LANG == 'sl' ? ($number % 100 == 1 ? 0 : ($number % 100 == 2 ? 1 : ($number % 100 == 3 || $number % 100 == 4 ? 2 : 3))) // different forms for 1, 2, 3-4, other
-                       : (LANG == 'lt' ? ($number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 > 1 && $number / 10 % 10 != 1 ? 1 : 2)) // different forms for 1, 12-19, other
-                       : (LANG == 'lv' ? ($number % 10 == 1 && $number % 100 != 11 ? 0 : ($number ? 1 : 2)) // different forms for 1 except 11, other, 0
-                       : (in_array(LANG, array('bs', 'ru', 'sr', 'uk')) ? ($number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 > 1 && $number % 10 < 5 && $number / 10 % 10 != 1 ? 1 : 2)) // different forms for 1 except 11, 2-4 except 12-14, other
-                       : 1)))))))) // different forms for 1, other
-               ; // http://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
-               $translation = $translation[$pos];
-       }
-       $translation = str_replace("'", '’', $translation); // translations can contain HTML or be used in optionlist (we couldn't escape them here) but they can also be used e.g. in title='' //! escape plaintext translations
-       $args = func_get_args();
-       array_shift($args);
-       $format = str_replace("%d", "%s", $translation);
-       if ($format != $translation) {
-               $args[0] = format_number($number);
-       }
-       return vsprintf($format, $args);
-}
-
 function switch_lang(): void {
        echo "<form action='' method='post'>\n<div id='lang'>";
        echo lang('Language') . ": " . html_select("lang", langs(), LANG, "this.form.submit();");
index fc5c44da55137ae8d9fea52497c029be4339ccd5..e7a79d186ba09372b20c5f6fe83bd4d0acd6dd89 100755 (executable)
@@ -21,7 +21,7 @@ function remove_lang($match) {
        $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
        $s = (Adminer\Lang::$translations[$idf] ?: $idf);
        if ($match[3] == ",") { // lang() has parameters
-               return $match[1] . (is_array($s) ? "lang(array('" . implode("', '", array_map('add_apo_slashes', $s)) . "')," : "sprintf('" . add_apo_slashes($s) . "',");
+               return $match[1] . (is_array($s) ? "lang_format(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]");
 }
@@ -84,8 +84,8 @@ function put_file($match) {
                        }
                }
        }
-       if (basename($match[2]) != "lang.inc.php" || !$_SESSION["lang"]) {
-               if (basename($match[2]) == "lang.inc.php") {
+       if (basename($match[2]) == "lang.inc.php") {
+               if (!$_SESSION["lang"]) {
                        $return = str_replace('function lang(string $idf, $number = null): string {', 'function lang($idf, $number = null) {
        if (is_string($idf)) { // compiled version uses numbers, string comes from a plugin
                // English translation is closest to the original identifiers //! pluralized translations are not found
@@ -97,27 +97,17 @@ function put_file($match) {
                        if (!$count) {
                                echo "lang() not found\n";
                        }
+               } else {
+                       $return = preg_replace('~// not used in a single language version from here\n.*~s', '', $return);
+                       $return = preg_replace_callback('~(\$pos = (.+\n).+;)~sU', function ($match) {
+                               return "\$pos = $match[2]\t\t\t: " . (preg_match("~'$_SESSION[lang]'.* \\? (.+)\n~U", $match[1], $match2) ? $match2[1] : "1") . "\n\t\t);";
+                       }, $return);
+                       $return = str_replace('Lang::$translations[$idf] ?: $idf', '$idf', $return); // lang() is used only by old plugins
+                       $return .= "define('Adminer\\LANG', '$_SESSION[lang]');\n";
                }
-               $tokens = token_get_all($return); // to find out the last token
-               return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
-       } elseif (preg_match('~\s*(\$pos = (.+\n).+;)~sU', $return, $match2)) {
-               // single language lang() is used for plural
-               return "define('Adminer\\LANG', '$_SESSION[lang]');
-
-function lang(\$translation, \$number = null) {
-       if (is_array(\$translation)) {
-               \$pos = $match2[2]\t\t\t: " . (preg_match("~'$_SESSION[lang]'.* \\? (.+)\n~U", $match2[1], $match3) ? $match3[1] : "1") . '
-               );
-               $translation = $translation[$pos];
-       }
-       $translation = str_replace("%d", "%s", $translation);
-       $number = format_number($number);
-       return sprintf($translation, $number);
-}
-';
-       } else {
-               echo "lang() \$pos not found\n";
        }
+       $tokens = token_get_all($return); // to find out the last token
+       return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
 }
 
 function lzw_compress($string) {