Home : Documentation : Embperl
Google Web perl.apache.org

 
Home
 
Features
 
Introduction
 
Documentation
 
README
 
README.v2
 
Configuration
 
Embperl
 
Embperl::Object
 
Embperl::Form::Validate
 
Embperl::Syntax
 
Embperl::Recipe
 
Embperl::Mail
 
1.3.6 documentation
 
DBIx::Recordset
 
Installation
 
Download
 
Support
 
Changes
 
Wiki
 
More infos
 
Add info about Embperl
 
Login

    Stable 2.4.0
    Beta 2.5.0_3
Support the development of Embperl! More...
Internationalisation (I18N)
[ << Prev: Caching ] [ Content ] [ Next: Encoding/UTF-8 >> ]

Starting with 2.0b6 Embperl has buildin support for multi-language applications. There are two things to do. First inside your pages marks which parts are translateable, by using the [= =]. Inside the [= =] blocks you could either put id, which are symbolic names for the text, or you put the text in your primary lanaguage inside the blocks. An example code could look like:

[= heading =]

<input name="foo" value="[=bar=]" type="submit">

Now you run the embpmsgid.pl utility, which extracts all the ids from your page:

    perl embpmsgid.pl -l de -l en -d msg.pl foo.htm

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. As next step fill the empty definition with the correct translation. The last thing to do, is tell Embperl which language set to use. You do this inside the init method of the application object. Create an application object, which reads in the message and when the init method is called, pass the correct one to Embperl. There are tow methods $r -> message and $r -> default_message. Both returns a array ref on which you can push your message hashs. Embperl consults first the message array and if not found afterwards the default_message array for the correct message. Because both are arrays you can push multiple message sets on it. This is handy when your application object calls it's base class, which also may define some messages. Starting with version 2.3.0 it is also possible, to add a code ref instead of a hash ref to the arrays. The code is than called with the key as argument and must return the translated text.

Here is an example:

    package My::App ; 

    @ISA = ('Embperl::App') ;

    %messages =
        (
        'de' =>
            {
            'heading' => '\[:U]berschrift',
            'bar'     => 'Absenden',
            },
        'en' =>
            {
            'heading' => 'Heading',
            'bar'     => 'Submit',
            },
        ) ;

    sub init
        {
        my $self = shift ;
        my $r = $self -> curr_req ;

        $lang = $r -> param -> language || 'de' ;
        push @{$r -> messages}, $messages{$lang} ;
        push @{$r -> default_messages}, $messages{'en'} if ($lang ne 'en') ;
        }

    # Code ref works too...
    @{$r -> messages} = (\&ecos::I18L::translate::gettext) ;

    # and gettext is defined as
    sub gettext 
	{
	my ($key) = @_ ;

return "translated text" ; }

    1 ;

Just load this package and set EMBPERL_APP_HANDLER_CLASS to My::App, then Embperl will call the init method on the start of the request.

If you are using Embperl::Object, you may instead save it as a file in your document hiearchie make the filename know to Embperl::Object with the EMBPERL_OBJECT_APP directive and Embperl::Object will retrieve the correct application file, just in the same way it retrieves other files.

NOTE: When using with Embperl::Object, don't make a package declaration at the top of your application object, Embperl::Object assign it's own namespace to the application object.

In case you need to retrieve a text inside your Perl code, you can do this with $r -> gettext('bar')


[ << Prev: Caching ] [ Content ] [ Next: Encoding/UTF-8 >> ]

© 1997-2012 Gerald Richter / ecos gmbh