Internationalization support

Loosely related since you mention language files. If anyone can share a bit of experience with swipl and internationalization (e.g., interoperability with gettext), that would be very helpful.

It would not be a big deal to add an interface to gettext. Might be a good idea. The overall idea of message support is derived from Quintus and available as print_message/2 and friends. This idea is that a message is represented by a Prolog term that conveys the semantics, for example, incomparable(apples, pears) Next, you define a rules that currently looks like

prolog:message(incomparable(X, Y)) -->
     [ 'Thou shalt not compare ~p with ~p!'-[X, Y] ].

Now Quintus implemented these DCG rules in a file per language. An application can at startup load the applicable file and we are done.

System messages are or SWI-Prolog all defined in boot/messages.pl and many libraries define their own set of rules for specific messages that are (thus) loaded with the library.

I think we should refine this to be able to deal with multiple languages in the same executable. That allows for plugins/libraries to support multiple languages include multiple languages into an executable without difficult procedures at startup and have e.g. web servers talk to users in their own language while providing services to people with different languages.

One option might be to simply add a language code to the prolog:message rule, so we get

prolog:message(en, incomparable(X, Y)) -->
     [ 'Thou shalt not compare ~p with ~p!'-[X, Y] ].

Following the language identification rules we can now select the most applicable rule and have e.g., rules for en_GB and en_US while if there is no matching en_GB we fallback to just en.

Note that as these are multifile rules we can provide additional language files and load them on demand.

Does that make sense? It seems pretty trivial to add …

I’ve pushed some preliminary work to make print_message/2 language sensitive.
Attached is a demo file that illustrates how to use it and that makes the system talk a little Dutch :slight_smile: You can use it by updating Prolog to the git version, download the file and run (at least on Linux, might also work on MacOS)

LANG=nl_NL.utf8 swipl nl.pl

nl.pl (3.3 KB)

Besides need for more documentation, the main issue seems that, at least if you want to create a Prolog system that talks another language, you want to reuse a lot of stuff from the system message translation. Not sure how to solve that. Possibly by putting part (most) of the actual translation into a normal user library?

If you want multilingual console applications this is now surely a lot easier. One of the other things I’m still struggling with is interpolating color sequences. It can be done, but it is rather tedious. Possibly we can use a simplified Markdown notation?

If you run it without the LANG setting you get the following:

$ swipl /tmp/nl.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.1-DIRTY)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

    CMake built from "/home/u2/tmp/swipl-devel/build.release"

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

1 ?- hi
|    .
FOUT: Unknown procedure: hi/0 (DWIM could not correct goal)

I figured it is because of :

user:message_property(error,   tag('FOUT')).
user:message_property(warning, tag('Waarschuwing')).

Is there a way to specify those tags apply only to nl language?

Thanks Jan.

I have tried to add “positional arguments” to format/2,3.

27 ?- format(string(A), "~w ~w ~w", [1, 2, 3]).
A = "1 2 3".

28 ?- format(string(A), "~1$w ~2$w ~3$w", [1, 2, 3]).
A = "1 2 3".

29 ?- format(string(A), "~1$w ~3$w ~2$w", [1, 2, 3]).
A = "1 3 2".

30 ?- format(string(A), "~1$w ~w ~w", [1, 2, 3]).
A = "1 2 3".

31 ?- format(string(A), "~1$w ~1$w ~w", [1, 2, 3]).
A = "1 1 2".

32 ?- format(string(A), "~1$w ~1$w ~0$w", [1, 2, 3]).
ERROR: Format error: positional argument must be positive

33 ?- format(string(A), "~1$w ~1$w ~5$w", [1, 2, 3]).
ERROR: Format error: overflow in positional argument

34 ?- format(string(A), "~1$w ~2$w", [1, 2, 3]). % no error/warning "too many arguments"
A = "1 2".

35 ?- format(string(A), "~w ~w", [1, 2, 3]). % error still there
ERROR: Format error: too many arguments

44 ?- format(string(A), "~1$3d ~2$3d ~3$3d", [1, 2, 3]). % other modifiers still work
A = "0.001 0.002 0.003".

47 ?- format(string(A), "~3d ~3d ~3d", [1, 2, 3]).
A = "0.001 0.002 0.003".

The use case is internationalization, where the order of arguments in formatted strings differs between languages. GNU gettext, among others, requires support for positional arguments in formatted strings.

Would a PR be welcome?

I’m a little unsure. format/2,3 is fairly standard these days and I’m reluctant to start a new round of changes. The main reason is that internationalization support is provided for by SWI-Prolog’s message infrastructure that uses DCGs. There this problem does not exist.

It matters when doing gettext like conversion. That might be a good idea for many applications as it removes the need to use print_message/2 all along, inventing a term that represents the message and its translation as a DCG. print_message/2 is a lot more flexible though.

Final thoughts, one could easily define the translation along these lines:

map_lang("~w xy ~w"-[A,B], 
         "~w yx ~w"-[B,A]).

Both a runtime and goal_expansion/2 based compile time translation work fine with that. The advantage of the latter is that the existing lint checks for format/2,3 keep working.

edit a last final though :slight_smile: If the mapped format comes from gettext, we can easily rewrite “~2$w yx ~1$w” to the above.

Could you elaborate the map_lang/2 example a little bit more? I don’t get it.

We can write e.g.,

user:goal_expansion(nl_format(Format0,Args0), format(Format1,Args1)) :-
    ground(Format0),
    map_lang(Format0-Args0, Format1-Args1).

Or a runtime solution

nl_format(Format, Args) :-
    ground(Format),   % otherwise error
    map_lang(Format-Args, Format1-Args1),
    !,
    format(Format1, Args1).
nl_format(Format, Args) :-
    format(Format, Args) :-

It’s not important, I can make a “pack” out of it. Whether to include it into main swipl can be postponed until someone else asks for it.

Do I understand correctly that there are already some built-in code checkers that would not recognize the format string while formatting the prolog code (e.g., in the development environment that we currently have)? This would indeed be a bit unfortunate.

See below. **s by me.

?- check.
% Checking undefined predicates ...
% Checking trivial failures ...
**% Checking format/2,3 and debug/3 templates ...**
% Checking redefined system and global predicates ...
% Checking predicates with declarations but without clauses ...
% Checking predicates that need autoloading ...
% Disabled autoloading (loaded 296 files)
% Checking predicate options lists ...
% Checking confusable identifier names (UTS #39) ...