d: add internationalisation support

The D parser implements this feature similarly to the C parser,
by using Gettext. Functions gettext() and dgettext() are
imported using extern(C). The internationalisation uses yysymbol_name
to report the name of the SymbolKinds.

* data/skeletons/d.m4 (SymbolKind.toString.yytranslatable,
SymbolKind.toString.yysymbol_name: New), data/skeletons/lalr1.d: Here.
* doc/bison.texi: Document it.
* tests/calc.at: Test it.
This commit is contained in:
Adela Vais
2020-11-28 21:09:39 +02:00
committed by Akim Demaille
parent e51e89856a
commit 594cae57ca
4 changed files with 145 additions and 15 deletions

View File

@@ -13953,6 +13953,66 @@ or nonzero, full tracing.
Identify the Bison version and skeleton used to generate this parser.
@end deftypecv
The internationalization in D is very simmilar to the one in C. The D
parser uses @code{dgettext} for translating Bison messages.
To enable internationalisation, compile using
@code{-version ENABLE_NLS -version YYENABLE_NLS} and import
@code{bindtextdomain} and @code{textdomain} from C:
@example
extern(C) char* bindtextdomain(const char* domainname, const char* dirname);
extern(C) char* textdomain(const char* domainname);
@end example
The main function should load the translation catalogues, similarly to the
@file{c/bistromathic} example:
@example
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", BISON_LOCALEDIR);
// For the translation catalogue of your own project, use the
// name of your project.
bindtextdomain("bison", LOCALEDIR);
textdomain("bison");
// usual main content
...
@}
@end example
For user messages translations, the user must implement the
@code{string} _(@code{const char*} @var{msg}) function and it is recommended
to use @code{gettext}:
@example
%code imports @{
static if (!is(typeof(_)))
@{
version(ENABLE_NLS)
@{
extern(C) char* gettext(const char*);
string _(const char* s)
@{
return to!string(gettext(s));
@}
@}
@}
static if (!is(typeof(_)))
@{
pragma(inline, true)
string _(string msg) @{ return msg; @}
@}
@}
@end example
@node D Parser Context Interface
@subsection D Parser Context Interface
The parser context provides information to build error reports when you