[aur-dev] [PATCH 0/2] Format string cleanup
From: Lukas Fleischer <info@cryptocrack.de> I've wanted to get rid of the "%h" format specifiers for a long time and somehow lost track of it. Thanks to our translation team for poking me on this. First patch was generated by a simple sed(1) invocation. Not so sure if we should also rewrite some of the crazy stuff like "%s%s%s" and just use "%s" where suitable. It might make sense here. Note that this patch set is in my working branch ("wip") and I won't merge this into master before releasing 1.9.0 (doesn't seem worse a string freeze break). Lukas Fleischer (2): Use "%s" instead of "%h" in format strings web/lib/translator.inc.php: Use vsprintf() in __() web/html/index.php | 4 ++-- web/html/passreset.php | 2 +- web/html/pkgsubmit.php | 2 +- web/lib/acctfuncs.inc.php | 12 ++++++------ web/lib/translator.inc.php | 19 +++++-------------- 5 files changed, 15 insertions(+), 24 deletions(-) -- 1.7.6
Use the standard string type specifier instead of "%h" in format strings. Both specifiers are treated equally in __() so we shouldn't break anything here. This also allows us to replace the hacky substitution algorithm in __() by vsprintf(). Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/html/index.php | 4 ++-- web/html/passreset.php | 2 +- web/html/pkgsubmit.php | 2 +- web/lib/acctfuncs.inc.php | 12 ++++++------ 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/web/html/index.php b/web/html/index.php index ffc5f00..138541f 100644 --- a/web/html/index.php +++ b/web/html/index.php @@ -25,7 +25,7 @@ $dbh = db_connect(); <?php echo __( - 'Welcome to the AUR! Please read the %hAUR User Guidelines%h and %hAUR TU Guidelines%h for more information.', + 'Welcome to the AUR! Please read the %sAUR User Guidelines%s and %sAUR TU Guidelines%s for more information.', '<a href="http://wiki.archlinux.org/index.php/AUR_User_Guidelines">', '</a>', '<a href="http://wiki.archlinux.org/index.php/AUR_Trusted_User_Guidelines">', @@ -37,7 +37,7 @@ echo __( <?php echo __( - 'Contributed PKGBUILDs %hmust%h conform to the %hArch Packaging Standards%h otherwise they will be deleted!', + 'Contributed PKGBUILDs %smust%s conform to the %sArch Packaging Standards%s otherwise they will be deleted!', '<b>', '</b>', '<a href="http://wiki.archlinux.org/index.php/Arch_Packaging_Standards">', '</a>' diff --git a/web/html/passreset.php b/web/html/passreset.php index ed5d4d3..01f3204 100644 --- a/web/html/passreset.php +++ b/web/html/passreset.php @@ -122,7 +122,7 @@ html_header(__("Password Reset")); <?php } else { ?> - <p><?php echo __('If you have forgotten the e-mail address you used to register, please send a message to the %haur-general%h mailing list.', + <p><?php echo __('If you have forgotten the e-mail address you used to register, please send a message to the %saur-general%s mailing list.', '<a href="http://mailman.archlinux.org/mailman/listinfo/aur-general">', '</a>'); ?></p> <form action="" method="post"> diff --git a/web/html/pkgsubmit.php b/web/html/pkgsubmit.php index 64281c7..a5cc0c0 100644 --- a/web/html/pkgsubmit.php +++ b/web/html/pkgsubmit.php @@ -273,7 +273,7 @@ if ($uid): $error = __( "Could not create directory %s.", $incoming_pkgdir); } } else { - $error = __( "You are not allowed to overwrite the %h%s%h package.", "<b>", $pkg_name, "</b>"); + $error = __( "You are not allowed to overwrite the %s%s%s package.", "<b>", $pkg_name, "</b>"); } if (!$error) { diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 97fb69b..59fa730 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -233,7 +233,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", if ($result) { $row = mysql_fetch_array($result); if ($row[0]) { - $error = __("The username, %h%s%h, is already in use.", + $error = __("The username, %s%s%s, is already in use.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>"); } } @@ -251,7 +251,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", if ($result) { $row = mysql_fetch_array($result); if ($row[0]) { - $error = __("The address, %h%s%h, is already in use.", + $error = __("The address, %s%s%s, is already in use.", "<b>", htmlspecialchars($E,ENT_QUOTES), "</b>"); } } @@ -273,12 +273,12 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", "VALUES (1, 0, '" . implode("', '", $escaped) . "')"; $result = db_query($q, $dbh); if (!$result) { - print __("Error trying to create account, %h%s%h: %s.", + print __("Error trying to create account, %s%s%s: %s.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>", mysql_error($dbh)); } else { # account created/modified, tell them so. # - print __("The account, %h%s%h, has been successfully created.", + print __("The account, %s%s%s, has been successfully created.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>"); print "<p>\n"; print __("Click on the Home link above to login."); @@ -310,10 +310,10 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", $q.= " WHERE ID = ".intval($UID); $result = db_query($q, $dbh); if (!$result) { - print __("Error trying to modify account, %h%s%h: %s.", + print __("Error trying to modify account, %s%s%s: %s.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>", mysql_error($dbh)); } else { - print __("The account, %h%s%h, has been successfully modified.", + print __("The account, %s%s%s, has been successfully modified.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>"); } } -- 1.7.6
Remove hacky substitution code from __() and use vsprintf() instead which will deal with all sorts of format strings properly. Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/translator.inc.php | 19 +++++-------------- 1 files changed, 5 insertions(+), 14 deletions(-) diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php index 44c87bd..54e8cbb 100644 --- a/web/lib/translator.inc.php +++ b/web/lib/translator.inc.php @@ -5,12 +5,11 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR # usage: # use the __() function for returning translated strings of -# text. The string can contain escape codes %h for HTML -# and %s for regular text. +# text. The string can contain escape codes "%s". # # examples: # print __("%s has %s apples.", "Bill", "5"); -# print __("This is a %hmajor%h problem!", "<b>", "</b>"); +# print __("This is a %smajor%s problem!", "<b>", "</b>"); include_once('config.inc.php'); include_once('gettext.php'); @@ -26,23 +25,15 @@ function __() { $args = func_get_args(); # First argument is always string to be translated - $tag = $args[0]; + $tag = array_shift($args); # Translate using gettext_reader initialized before. $translated = $l10n->translate($tag); $translated = htmlspecialchars($translated, ENT_QUOTES); - $num_args = sizeof($args); - # Subsequent arguments are strings to be formatted - # - # TODO: make this more robust. - # '%%' should translate to a literal '%' - - if ( $num_args > 1 ) { - for ($i = 1; $i < $num_args; $i++) { - $translated = preg_replace("/\%[sh]/", $args[$i], $translated, 1); - } + if (count($args) > 0) { + $translated = vsprintf($translated, $args); } return $translated; -- 1.7.6
On Mon, Aug 15, 2011 at 10:37:22AM +0200, Lukas Fleischer wrote:
I've wanted to get rid of the "%h" format specifiers for a long time and somehow lost track of it. Thanks to our translation team for poking me on this.
First patch was generated by a simple sed(1) invocation. Not so sure if we should also rewrite some of the crazy stuff like "%s%s%s" and just use "%s" where suitable. It might make sense here.
Note that this patch set is in my working branch ("wip") and I won't merge this into master before releasing 1.9.0 (doesn't seem worse a string freeze break).
What I forgot to mention here: It might make sense to run sed(1) on the translations as well and push them to Transifex once we merge this.
On Mon, Aug 15, 2011 at 3:49 AM, Lukas Fleischer <archlinux@cryptocrack.de> wrote:
On Mon, Aug 15, 2011 at 10:37:22AM +0200, Lukas Fleischer wrote:
I've wanted to get rid of the "%h" format specifiers for a long time and somehow lost track of it. Thanks to our translation team for poking me on this.
First patch was generated by a simple sed(1) invocation. Not so sure if we should also rewrite some of the crazy stuff like "%s%s%s" and just use "%s" where suitable. It might make sense here.
Note that this patch set is in my working branch ("wip") and I won't merge this into master before releasing 1.9.0 (doesn't seem worse a string freeze break).
What I forgot to mention here: It might make sense to run sed(1) on the translations as well and push them to Transifex once we merge this.
Pro tip- man 1 msgfilter. -Dan
participants (2)
-
Dan McGee
-
Lukas Fleischer