Re: How to create a simple multilingual GUI



On Mon, May 14, 2007 at 12:30:14PM +0200, John Zoidberg wrote:
Well, it's possible to change the language in Firefox and in a lot of other
programs.

That does not make it a good idea.

Firefox is designed for Microsoft Windows -- where one had
to reinstall the system it to switch languages.  Applications
tried to work around it and each invented its own language
switching mechanism.

We've had a standard mechanism since ever.  All programs
that support localization respect -- or should respect
-- language preferences expressed with locale variables.
Running

  LC_ALL=cs_CZ app

should run app in Czech.  By editing the command used to
run the application this should be relatively easy to do
with a GUI launcher too -- in fact, the launcher editors in
desktop environments should offer the possibility to set the
language for individual applications to make it easier.

What we don't need is every program inventing its own
method to express language preferences.

I don't mind having to restart the program to do it.

With restarting it is easy.  The only thing you need to do
is to set locale to the desired one on startup, overriding
what you have inherited from environment.  See below.

I just don't want to have to change the system's local language to do it.

What is `the system local language'?  Probably there's no
such thing and therefore you cannot change it.  You can set
locale [categories] for each program individually (several
possibilities have been listed), and that's what you also
should do.

I know that gettext is mostly used for internationalisation. However, I have
problems creating a program with it where I choose the language in the
program, independently of the system settings.

Call setlocale() or set LC_* environment variables as
suggested, each process has its own.  And you should use the
inherited locale as the default.

If somebody knows how to do this, I would be really grateful for a simple
example program, even if it's only command-line and without a GUI.

=============================================
#include <stdio.h>
#include <time.h>
#include <locale.h>

int
main(int argc, char *argv[])
{
    time_t t;
    char s[1024];

    if (argc > 1)
        setlocale(LC_ALL, argv[1]);
    time(&t);
    strftime(s, sizeof(s), "%c", localtime(&t));
    printf("%s\n", s);
    return 0;
}
=============================================

Pass it a locale name and it will print the current date and
time in the form preferred for that locale (i.e. language
+ region) because "%c" means locale-defined date and time
format.  Observe it leaves your system local language
(whatever it is) intact.

Everything else (e.g. adding gettext which is controlled by
the same variables or storing the selected language in
some preferences file) is a straighforward extension.

In the meanwhile, I began looking at the source code of tuxtyping, where
changing the language at runtime is possible.
But having a clear simple source code for something like that really makes
it easier.

It is a lot easier if your user interface is a set of
independent screens, exactly one active at any time.  When
you switch languages, you just discard the current screen
and construct it again.  This is not how a typical Gtk+
application looks like though.

Yeti

--
http://gwyddion.net/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]