[Dlang] Internationalisation WIP

Adela Vais <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <CAPk8xGeceG2pF=AjWh6Da5GVW9xjv1SxWyEcu_r5+=Uv5=QHyw@mail.gmail.com>
Hello!

I am trying to write the code for internationalization. The functions I use
are the ones from C's parser flow (dgettext, setlocale, etc).

I never worked with gettext (as a bash command) before, and I have some
trouble understanding how I automatically generate the .mo files and put
them into /usr/local/share/locale (where the code from the
examples/c/bisthromatic hints they should be found). My understanding is
that from the .po files I have to generate .mo files, and these are used by
the programs generated with Bison.

If I try to run "sudo make install" in the po directory I get this error:

    [...]
    /usr/bin/install: cannot stat './bg.gmo': No such file or directory
    installing ./bg.gmo as /usr/local/share/locale/bg/LC_MESSAGES/bison.mo
    [...]

and so on for each language file. After the command, no files exist in the
LC_MESSAGES directories.
If I run make update-po or update-gmo, I also get errors for missing files.

If I create a .mo file by hand, using msgfmt, and then move it
into /usr/local/share/locale/<language>/LC_MESSAGES/, the output gets
translated as expected.

The second problem is that I expected dgettext to receive the argument
"bison-runtime", like in C, not "bison", as it happens now in D. If I use
"bison-runtime" the messages remain in English.

Here [1] (and also as attachments) is a work in progress for this task,
with the mention that examples/d/c/calc/calc.y is modified just for testing
reasons, it will not be changed in the official commit.

Example of output with LANG=ro_RO.UTF-8:
~/bison/examples/d/calc$ ./calc
5 +
5 5
1.4-2.0: eroare de sintaxă, end of line neaşteptat, aştept + sau - sau (
sau number
2.3: eroare de sintaxă, number neaşteptat

Adela

[1] https://github.com/adelavais/bison/tree/internationalisation-gettext
0001-added-dgettext.patch (application/x-patch, 3.1 KB)
From 15d821087a1117409064fff3252d25d959848cb8 Mon Sep 17 00:00:00 2001
From: Adela Vais <[email protected]>
Date: Sat, 28 Nov 2020 21:09:39 +0200
Subject: [PATCH for Dlang support 1/2] added dgettext

---
 data/skeletons/lalr1.d | 49 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 9 deletions(-)

diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d
index d126d499..854cc463 100644
--- a/data/skeletons/lalr1.d
+++ b/data/skeletons/lalr1.d
@@ -41,6 +41,8 @@ version(D_Version2) {
 ]b4_percent_code_get([[imports]])[
 import std.format;
 
+extern(C) char* dgettext(const char*, const char*);
+
 /**
  * A Bison parser, automatically generated from <tt>]m4_bpatsubst(b4_file_name, [^"\(.*\)"$], [\1])[</tt>.
  *
@@ -704,20 +706,49 @@ m4_popdef([b4_at_dollar])])dnl
       immutable int argmax = 5;
       SymbolKind[] yyarg = new SymbolKind[argmax];
       int yycount = yysyntaxErrorArguments(yyctx, yyarg, argmax);
-      string res = "syntax error, unexpected ";
-      res ~= format!"%s"(yyarg[0]);
-      if (yycount < argmax + 1)
+      string[] yystr = new string[yycount];
+      for (int yyi = 0; yyi < yycount; yyi++)
+        yystr[yyi] = format!"%s"(yyarg[yyi]);
+      string res, yyformat;
+      char *text;
+      import std.string;
+      import std.conv;
+      switch (yycount)
       {
-        for (int yyi = 1; yyi < yycount; yyi++)
-        {
-          res ~= yyi == 1 ? ", expecting " : " or ";
-          res ~= format!"%s"(SymbolKind(yyarg[yyi]));
-        }
+        case  1:
+          text = dgettext("bison-runtime", "syntax error, unexpected %s");
+          yyformat = to!string(text);
+          res = format(yyformat, yystr[0]);
+         break;
+        case  2:
+          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s");
+          yyformat = to!string(text);
+          res = format(yyformat, yystr[0], yystr[1]);
+          break;
+        case  3:
+          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s or %s");
+          yyformat = to!string(text);
+          res = format(yyformat, yystr[0], yystr[1], yystr[2]);
+          break;
+        case  4:
+          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s or %s or %s");
+          yyformat = to!string(text);
+          res = format(yyformat, yystr[0], yystr[1], yystr[2], yystr[3]);
+          break;
+        case  5:
+          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s or %s or %s or %s");
+          yyformat = to!string(text);
+          res = format(yyformat, yystr[0], yystr[1], yystr[2], yystr[3], yystr[4]);
+          break;
+        default:
+          text = dgettext("bison-runtime", "syntax error");
+          res = to!string(text);
+          break;
       }
       yyerror(]b4_locations_if([yyctx.getLocation(), ])[res);
     }]],
 [[simple]], [[
-    yyerror(]b4_locations_if([yyctx.getLocation(), ])["syntax error");]])[
+    yyerror(]b4_locations_if([yyctx.getLocation(), ])[dgettext("bison-runtime", "syntax error"));]])[
   }
 
 ]b4_parse_error_bmatch(
-- 
2.17.1
0002-added-a-temporary-test-in-examples-d-calc-calc.y.patch (application/x-patch, 3.6 KB)
From 79b46bd91b0fa1cdd29c08e96cd8f407a3c5c5d3 Mon Sep 17 00:00:00 2001
From: Adela Vais <[email protected]>
Date: Mon, 30 Nov 2020 13:52:45 +0200
Subject: [PATCH for Dlang support 2/2] added a temporary test in
 examples/d/calc/calc.y

---
 data/skeletons/lalr1.d | 14 +++++++-------
 examples/d/calc/calc.y | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d
index 854cc463..fd9adad1 100644
--- a/data/skeletons/lalr1.d
+++ b/data/skeletons/lalr1.d
@@ -716,39 +716,39 @@ m4_popdef([b4_at_dollar])])dnl
       switch (yycount)
       {
         case  1:
-          text = dgettext("bison-runtime", "syntax error, unexpected %s");
+          text = dgettext("bison", "syntax error, unexpected %s");
           yyformat = to!string(text);
           res = format(yyformat, yystr[0]);
          break;
         case  2:
-          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s");
+          text = dgettext("bison", "syntax error, unexpected %s, expecting %s");
           yyformat = to!string(text);
           res = format(yyformat, yystr[0], yystr[1]);
           break;
         case  3:
-          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s or %s");
+          text = dgettext("bison", "syntax error, unexpected %s, expecting %s or %s");
           yyformat = to!string(text);
           res = format(yyformat, yystr[0], yystr[1], yystr[2]);
           break;
         case  4:
-          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s or %s or %s");
+          text = dgettext("bison", "syntax error, unexpected %s, expecting %s or %s or %s");
           yyformat = to!string(text);
           res = format(yyformat, yystr[0], yystr[1], yystr[2], yystr[3]);
           break;
         case  5:
-          text = dgettext("bison-runtime", "syntax error, unexpected %s, expecting %s or %s or %s or %s");
+          text = dgettext("bison", "syntax error, unexpected %s, expecting %s or %s or %s or %s");
           yyformat = to!string(text);
           res = format(yyformat, yystr[0], yystr[1], yystr[2], yystr[3], yystr[4]);
           break;
         default:
-          text = dgettext("bison-runtime", "syntax error");
+          text = dgettext("bison", "syntax error");
           res = to!string(text);
           break;
       }
       yyerror(]b4_locations_if([yyctx.getLocation(), ])[res);
     }]],
 [[simple]], [[
-    yyerror(]b4_locations_if([yyctx.getLocation(), ])[dgettext("bison-runtime", "syntax error"));]])[
+    yyerror(]b4_locations_if([yyctx.getLocation(), ])[dgettext("bison", "syntax error"));]])[
   }
 
 ]b4_parse_error_bmatch(
diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y
index 24ec85d4..86b9de66 100644
--- a/examples/d/calc/calc.y
+++ b/examples/d/calc/calc.y
@@ -180,10 +180,28 @@ if (isInputRange!R && is(ElementType!R : dchar))
   }
 }
 
+extern(C) char * bindtextdomain (const char * domainname, const char * dirname);
+extern(C) char * textdomain (const char * domainname);
+
 int main()
 {
+  import core.stdc.locale;
+
+  // Set up internationalization.
+  setlocale (LC_ALL, "");
+
+  // Use Bison's standard translation catalogue for error messages
+  // (the generated messages).
+  bindtextdomain ("bison-runtime", "/usr/local/share/locale");
+  bindtextdomain ("bison", "/usr/local/share/locale");
+
+  textdomain ("bison");
+  //printf("%s\n", dgettext("bison", "syntax error"));
+
   auto l = calcLexer(stdin);
   auto p = new Calc(l);
   p.parse();
   return l.exit_status;
+
+  //return 0;
 }
-- 
2.17.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.