From: Bruno Haible Date: Sun, 1 Jun 2025 23:23:13 +0000 (+0200) Subject: xgettext: PHP: Improve heuristic for format strings. X-Git-Tag: v0.26~129 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dacdbfec87a01589480fe120db9395b528f2c922;p=thirdparty%2Fgettext.git xgettext: PHP: Improve heuristic for format strings. * gettext-tools/src/format-php.c (struct spec): Add field 'likely_intentional_directives'. (format_parse): Set it to the number of directives that don't contain a space. (format_is_unlikely_intentional): New function. (formatstring_php): Use it. * gettext-tools/tests/format-php-3: New file. * gettext-tools/tests/Makefile.am (TESTS): Add it. --- diff --git a/gettext-tools/src/format-php.c b/gettext-tools/src/format-php.c index b43fd5a99..11b55a0d7 100644 --- a/gettext-tools/src/format-php.c +++ b/gettext-tools/src/format-php.c @@ -72,6 +72,11 @@ struct numbered_arg struct spec { unsigned int directives; + /* We consider a directive as "likely intentional" if it does not contain a + space. This prevents xgettext from flagging strings like "100% complete" + as 'php-format' if they don't occur in a context that requires a format + string. */ + unsigned int likely_intentional_directives; unsigned int numbered_arg_count; struct numbered_arg *numbered; }; @@ -92,6 +97,7 @@ format_parse (const char *format, bool translated, char *fdi, { const char *const format_start = format; unsigned int directives; + unsigned int likely_intentional_directives; unsigned int numbered_arg_count; struct numbered_arg *numbered; unsigned int numbered_allocated; @@ -99,6 +105,7 @@ format_parse (const char *format, bool translated, char *fdi, struct spec *result; directives = 0; + likely_intentional_directives = 0; numbered_arg_count = 0; numbered = NULL; numbered_allocated = 0; @@ -108,6 +115,8 @@ format_parse (const char *format, bool translated, char *fdi, if (*format++ == '%') { /* A directive. */ + bool likely_intentional = true; + FDI_SET (format - 1, FMTDIR_START); directives++; @@ -148,7 +157,11 @@ format_parse (const char *format, bool translated, char *fdi, for (;;) { if (*format == '0' || *format == '-' || *format == ' ') - format++; + { + if (*format == ' ') + likely_intentional = false; + format++; + } else if (*format == '\'') { format++; @@ -231,6 +244,8 @@ format_parse (const char *format, bool translated, char *fdi, numbered_arg_count++; } + if (likely_intentional) + likely_intentional_directives++; FDI_SET (format, FMTDIR_END); format++; @@ -285,6 +300,7 @@ format_parse (const char *format, bool translated, char *fdi, result = XMALLOC (struct spec); result->directives = directives; + result->likely_intentional_directives = likely_intentional_directives; result->numbered_arg_count = numbered_arg_count; result->numbered = numbered; return result; @@ -313,6 +329,14 @@ format_get_number_of_directives (void *descr) return spec->directives; } +static bool +format_is_unlikely_intentional (void *descr) +{ + struct spec *spec = (struct spec *) descr; + + return spec->likely_intentional_directives == 0; +} + static bool format_check (void *msgid_descr, void *msgstr_descr, bool equality, formatstring_error_logger_t error_logger, void *error_logger_data, @@ -397,7 +421,7 @@ struct formatstring_parser formatstring_php = format_parse, format_free, format_get_number_of_directives, - NULL, + format_is_unlikely_intentional, format_check }; diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 355e47e62..edf08b489 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -214,7 +214,7 @@ TESTS = gettext-1 gettext-2 \ format-lisp-1 format-lisp-2 \ format-lua-1 format-lua-2 \ format-modula2-1 format-modula2-2 \ - format-php-1 format-php-2 \ + format-php-1 format-php-2 format-php-3 \ format-python-1 format-python-2 \ format-python-brace-1 format-python-brace-2 \ format-pascal-1 format-pascal-2 \ diff --git a/gettext-tools/tests/format-php-3 b/gettext-tools/tests/format-php-3 new file mode 100755 index 000000000..969665e17 --- /dev/null +++ b/gettext-tools/tests/format-php-3 @@ -0,0 +1,49 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test heuristic recognition of PHP format strings like "100% complete". + +cat <<\EOF > f-ph-3.php + +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d f-ph-3.tmp f-ph-3.php || Exit 1 +LC_ALL=C tr -d '\r' < f-ph-3.tmp.po > f-ph-3.po || Exit 1 + +cat <<\EOF > f-ph-3.ok +msgid "Test 1a is 100% complete" +msgstr "" + +#, php-format +msgid "Test 1b is 100% complete" +msgstr "" + +msgid "Test 2a from 0% complete to 100% complete" +msgstr "" + +#, php-format +msgid "Test 2b from 0% complete to 100% complete" +msgstr "" + +#, php-format +msgid "Test 3a of %s is 100% complete" +msgstr "" + +#, php-format +msgid "Test 3b of %s is 100% complete" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} f-ph-3.ok f-ph-3.po +result=$? + +exit $result