[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/7] java: make the syntax error format string translatable
From: |
Akim Demaille |
Subject: |
[PATCH 4/7] java: make the syntax error format string translatable |
Date: |
Sun, 9 Feb 2020 14:02:24 +0100 |
The error format should be translated, but contrary to the case of
C/C++, we cannot just depend on macros to adapt on the
presence/absence of '_'. Let's consider that the message format is to
be translated iff there are some internationalized tokens.
* src/output.c (prepare_symbol_names): Define b4_has_translations.
* data/skeletons/java.m4 (b4_trans): New.
* data/skeletons/lalr1.java: Use it to emit translatable or not the
format string.
---
data/skeletons/java.m4 | 7 +++++++
data/skeletons/lalr1.java | 16 ++++++++--------
src/output.c | 10 +++++++++-
3 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/data/skeletons/java.m4 b/data/skeletons/java.m4
index 5c015640..920f34bc 100644
--- a/data/skeletons/java.m4
+++ b/data/skeletons/java.m4
@@ -226,6 +226,13 @@ m4_define([b4_symbol_translate],
[[_($1)]])
+# b4_trans(STRING)
+# ----------------
+# Translate a symbol. Avoid collision with b4_translate.
+m4_define([b4_trans],
+[m4_if(b4_has_translations, 0, [$1], [_($1)])])
+
+
# b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
# ----------------------------------------------
diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java
index f74ac646..a85f38b0 100644
--- a/data/skeletons/lalr1.java
+++ b/data/skeletons/lalr1.java
@@ -945,18 +945,18 @@ b4_dollar_popdef[]dnl
String[] yystr = new String[yycount];
for (int yyi = 0; yyi < yycount; ++yyi)
yystr[yyi] = yysymbolName (yyarg[yyi]);
- MessageFormat yyformat;
+ String yyformat;
switch (yycount)
{
default:
- case 0: yyformat = new MessageFormat ("syntax error"); break;
- case 1: yyformat = new MessageFormat ("syntax error, unexpected
{0}"); break;
- case 2: yyformat = new MessageFormat ("syntax error, unexpected
{0}, expecting {1}"); break;
- case 3: yyformat = new MessageFormat ("syntax error, unexpected
{0}, expecting {1} or {2}"); break;
- case 4: yyformat = new MessageFormat ("syntax error, unexpected
{0}, expecting {1} or {2} or {3}"); break;
- case 5: yyformat = new MessageFormat ("syntax error, unexpected
{0}, expecting {1} or {2} or {3} or {4}"); break;
+ case 0: yyformat = ]b4_trans(["syntax error"])[; break;
+ case 1: yyformat = ]b4_trans(["syntax error, unexpected {0}"])[;
break;
+ case 2: yyformat = ]b4_trans(["syntax error, unexpected {0},
expecting {1}"])[; break;
+ case 3: yyformat = ]b4_trans(["syntax error, unexpected {0},
expecting {1} or {2}"])[; break;
+ case 4: yyformat = ]b4_trans(["syntax error, unexpected {0},
expecting {1} or {2} or {3}"])[; break;
+ case 5: yyformat = ]b4_trans(["syntax error, unexpected {0},
expecting {1} or {2} or {3} or {4}"])[; break;
}
- return yyformat.format (yystr);
+ return new MessageFormat (yyformat).format (yystr);
}
]])[
return "syntax error";
diff --git a/src/output.c b/src/output.c
index ac143fa0..459023c2 100644
--- a/src/output.c
+++ b/src/output.c
@@ -194,6 +194,7 @@ prepare_symbol_names (char const *muscle_name)
{
/* We assume that the table will be output starting at column 2. */
const bool quote = STREQ (muscle_name, "tname");
+ bool has_translations = false;
int j = 2;
struct quoting_options *qo = clone_quoting_options (0);
set_quoting_style (qo, c_quoting_style);
@@ -219,7 +220,10 @@ prepare_symbol_names (char const *muscle_name)
if (i)
obstack_1grow (&format_obstack, ' ');
if (!quote && symbols[i]->translatable)
- obstack_sgrow (&format_obstack, "]b4_symbol_translate([");
+ {
+ has_translations = true;
+ obstack_sgrow (&format_obstack, "]b4_symbol_translate([");
+ }
obstack_escape (&format_obstack, cp);
if (!quote && symbols[i]->translatable)
obstack_sgrow (&format_obstack, "])[");
@@ -232,6 +236,10 @@ prepare_symbol_names (char const *muscle_name)
/* Finish table and store. */
muscle_insert (muscle_name, obstack_finish0 (&format_obstack));
+
+ /* Announce whether translation support is needed. */
+ if (!quote)
+ MUSCLE_INSERT_BOOL ("has_translations", has_translations);
}
--
2.25.0
- [PATCH 0/7] Java: add support for parse.error custom|detailed, Akim Demaille, 2020/02/09
- [PATCH 1/7] m4: fix b4_token_format, Akim Demaille, 2020/02/09
- [PATCH 2/7] java: add support for parse.error=detailed, Akim Demaille, 2020/02/09
- [PATCH 5/7] java: let the Context give access to yyntokens, Akim Demaille, 2020/02/09
- [PATCH 4/7] java: make the syntax error format string translatable,
Akim Demaille <=
- [PATCH 3/7] java: introduce yyexpectedTokens, Akim Demaille, 2020/02/09
- [PATCH 6/7] java: add support for parse.error custom, Akim Demaille, 2020/02/09
- [PATCH 7/7] java: provide Context with a more OO interface, Akim Demaille, 2020/02/09