On Wed, Jan 24, 2001 at 07:57:09PM +0100, Philipp Rotmann, Linksystem Muenchen wrote:
> 
>      Hi there,
> 
> another problem for my brain concerning the Midhoo project ;-)
> 
> I'm thinking about how to implement language independency. From
> the spec:
> 
> | Another requirement would be to make the application as
> | modular and customizable as possible, mostly with regard to
> | layout and language on the one hand and packaging as an easily
> | distributable Midgard Application on the other.
> (...) 
> | For the first revision, localization as well as other
> | configuration options are simply realized as global constants. 
> | Later we may find a more convenient and flexible way.
> 
> I'm quite interested in the concept of Asgard's localization.
> Alexander, David, could you explain the background and concepts
> to me? I guess, the Asgard way is far too complex for my needs
> and current resources, but it's interesting nonetheless -- both
> for future applications and to keep in mind for the Midgard web
> site localization.
I know several ways to localize web applications. In general, we  should
distinguish (a) system messages and (b) content. I'm talking about case
(a) below because localizing content is completely different task with
different goals in mind, etc.

So, you want to make your template language-neutral so that people could
change language without changing much of code thus keeping it under
Repligard's Big Brother Eye for updates. There are different approaches
to separate messages and code but in general it ends up to implement
some kind of templating of message displaying code (yeah, templates for
templates or TfT). In Midgard world you're not bounded by Midgard's
templates only, you can use general PHP solutions and even combine them.

How complex must these templates be? It depends on size of project and
desire to keep things manageable. For simple (read: little amount of
system messages) projects you can map message to style or page element
making message label as element's name (Midgard allows you to use spaces
in template's names). Then just inherit localized sub-styles from some
basic style and re-write messaging elements with localized content.

While this solution is automagical (change style -> get new language
binding), it lacks many features. First of all, style <-> page linking
in Midgard is static, you can't select different style once page being
parsed. Second, number of messages usually tends to grow exponentially
and style starts to be unmanageable quickly. 

Another approach is to keep some kind of message catalogues and use
echo() statements to deliver messages from catalogue to visitor. This
works fine for any kind of project and has wide usage in non-web-based
programming for years. PHP supports one of such solutions: GNU gettext.
Gettext allows you to write messages in one main language (usually, it
is English) in program code using special function gettext() or using
more convinient _() macros which works in PHP too. Little example:

<?
    $articles = mgd_list_topic_articles_all($topic);
    if($articles && $articles->N) {
        printf(_("There are %d articles under this topic"), $articles->N);
        .....
    }
?>

Here we are printing number of articles under topic and its sub-topics
marking this message to gettext as needed to translation. After all
messages marked, special external program xgettext is called for PHP
source file and it produces so called 'initial message catalogue'.
People are copying content of this catalogue to their respective
translation files and translate it.

After that, next program msgfmt is called to make binary representation
of catalogue to allow quick seek of messages. Programmer is responsible
then to bind correct message catalogue to gettext system using
setlocale(), bindtextdomain(), and textdomain(). In usual (non-web)
cases, information about locale is gathered from system environment but
in Web-programming this is, of course, can be set to whatever needed.

For Midgard-powered sites this approach isn't good because message
catalogues' binaries have to be in file system. Thus, in Asgard we are
implemented similar but more simple approach: each message catalogue was
replaced by corresponding snippet.

In these snippets, each message is put into assosiative array (let's
call it $MessageCatalog[]). Then we include needed snippet according
user settings and echo()-ing elements from this array. Little example:

Snippet "/Messages/common":
<?
    /* This snippet gets translated message from $MessageCatalog, if
        it exists there or returns initial message otherwise.
    */
    function getmessage($str) {
        $translations = GLOBALS["MessageCatalog"];
        if(isarray($translations) && isset($translations[$str])) {
            return $translations[$str];
        }
        return $str;
    }
?>

Snippet "/Messages/RU":
<?
    $MessageCatalog["There are %d articles under this topic"] =
        "V etom razdele %d statey"; /* Latinized Russian */
?>

Somewhere in code:

<?
    mgd_include_snippet("/Messages/common");
    
    ....
    
    mgd_include_snippet("/Messages/$LANGCODE");
    
    $articles = mgd_list_topic_articles_all($topic);
    if($articles && $articles->N) {
        printf(getmessage("There are %d articles under this topic"), $articles->N);
        .....
    }
?>

That's all. $LANGCODE may be gathered from whatever you want (cookies,
record_extenstions, etc), further modifications can be made, etc, etc,
etc. Of course, there are many topics to discuss still there, for
example, correct charset manipulation for translations but common scheme
is rather simple.

There is also special on-line editor for such translations in Asgard
which is tightly connected to Asgard itself and uses regular expressions
to allow in-place editing of message catalogues but this is different
story.


-- 
Sincerely yours, Alexander Bokovoy 
  The Midgard Project   | www.midgard-project.org |    Aurora R&D team 
Minsk Linux Users Group |    www.minsk-lug.net    |  www.aurora-linux.com  
   IPLabs Linux Team    |     linux.iplabs.ru     | Architecte Open Source
-- You can't erase a dream, you can only wake me up.
                -- Peter Frampton

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to