Embperl - building dynamic websites with Perl

[ << Prev: Converting different formats: Providers and Recipes ] [ Content ] [ Next: Form Validation >> ]

Like many other website, the Embperl website, must exist in multiple languages. In this case english and german. Embperl provides some support, that makes life easier. First of all it retrieves the Accept-Language HTTP header and sets the language parameter of the request object accodingly. Next you can take this information and feed the correct table of messages into Embperl, which Embperl uses to resolve message ids which are embedded into the sources. Here is a part of the file messages.pl which contains the messages for the Embperl website:

    %messages =
        'de' =>
            'addsel1' => 'Klicken Sie auf die Kategorie zu der Sie etwas hinzufügen möchten:',
            'addsel2' => 'oder fügen Sie eine neue Kategorie hinzu. Bitte geben Sie die Beschreibung in so....',
            'addsel3' => 'Falls Sie die Übersetzung nicht wissen, lassen Sie das entsprechende Eingabefeld leer.',
            'addsel4' => 'Kategorie hinzufügen',
            'user_email'     => 'E-Mail Adresse',
            'user_password'  => 'Kennwort',
         'en' =>
            'addsel1' => 'Click on the category for wich you want to add a new item:',
            'addsel2' => 'or add new category. Please enter the description in as much languages as possible.',
            'addsel3' => 'If you don\'t know the translation leave the corresponding input field empty.',
            'addsel4' => 'Add category',
            'user_email'     => 'E-Mail address',
            'user_password'  => 'Password',
        ) ;
    $lang = $request -> param -> language ;
    push @{$request -> messages}, $messages{$lang} ;
    push @{$request -> default_messages}, $messages{'en'} if ($lang ne 'en') ;

$request - param -> language> retrieves the language as given by the browsers Language-Accept HTTP header (or set before in your program). Then it selects the correct message table (either german or english) from the %messages hash. This message table is pushed on the array given by $request -> messages. This array holds a set of hash which are search for the correct message id, when a message id needs to be resolved. The $request - default_messages> is a second array that is searched, when nothing is found in the messages array.

Inside a page you can now insert

    [= addsel1 =]

which will be replaced by

    Klicken Sie auf die Kategorie zu der Sie etwas hinzufügen möchten:

if the language was german (de) or

    Click on the category for wich you want to add a new item:

if the language was english (en). The last will also be the case for all other languages because we use the method default_messages to set the english message table as the default if Embperl can't find anything inside the tables set by the messages method.

Sometimes you don't want to insert the result of a message lookup directly into the outpt. In this case you can use the request method gettext, e.g.

    $msg = $request -> gettext('addsel1') ;

This will assign the same text as seen above to the variable $msg.

Now when you have created all your pages with message ids inserted, you have to create the table that holds the actual message. Embperl ships with a script that helps you doing so. By running

    perl embpmsgid.pl /path/to/sourcefile.epl

For example running this on the file add.epl of the Embperl website give the following result:

    #perl embpmsgid.pl eg/web/db/add.epl
    $msgids = {
            'state' => '',
            'add2a' => '',
            'add1' => '',
            'hide' => '',
            'add2b' => '',
            'add3' => '',
            'display' => '',
            'delete3' => '',
            'update3' => '',
            'edit1' => ''

As you see it's a list of all message ids and it's up to you to assign the correct texts. You can also give the desired language already on the command line with -l option and write the output to a file with the -d option, e.g.

    perl embpmsgid.pl -l de -l en -d msg.pl eg/web/db/add.epl

This will create a file msg.pl which contains empty definitions for 'en' and 'de' with all the ids found in the page. If the file msg.pl already exists, the definitions are added. You can give more then one filename to the commandline. The format of the msg.pl file is written with Data::Dumper, so it can be easily read in via 'do' and postprocessed.

The remaining question is, where is the best place inside a request to select message tables for the request and the answer is in the init method of the application object. The Embperl website application object's init method does this by calling the file messages.pl:

        Execute ({inputfile => 'messages.pl', syntax => 'Perl'}) ;

which contains the message table and the code which assigns the correct table as we saw it above.

[ << Prev: Converting different formats: Providers and Recipes ] [ Content ] [ Next: Form Validation >> ]

© 1997-2023 Gerald Richter / actevy