perl-i18n

Re: How to organize lexicons with Locale::Maketext and oo-development ?

2002-08-31 15:05:53
Le Samedi 31 Août 2002 01:07, vous avez écrit :
At 14:06 2002-08-30 +0200, Guillaume Rousse wrote:
              A
       /            \
     A1           A2
   /     \         /    \
A1.1 A1.2  A2.1 A2.2

If i get the language handle in A constructor, it will be automatically
shared
by all its descendants, but i'll have to use a unique lexicon class (per
language, of course) for the whole hierachy, wich is not very practical.

Okay, Im lost.  Do these A* classes represent Maketext language classes, or
something else?
Sorry, it seems i was not not clear enough. Those A* classes represent my 
application classes, those i want to localise.

let's have some code if this can help

First strategy is:

package foo::A;
use foo::L10N::A;

sub new {
    my $class = shift;
    my $self = bless {
        _lh => foo::L10N::A->get_handle();
    }, $class;            
    return $self;
}

package foo::A1;
use base 'foo::A'

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    return $self;
}

package foo::A2;
use base 'foo::A'

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    return $self;
}

package foo::A::L10N;
use base 'Locale::Maketext'

This way, all instance share automatically a langage handle via ancestor's 
constructor, but foo::L10N::A::en, foo::L10N::A::fr, etc... have to contain 
translations for the whole hierarchy.


Second strategy is:

package foo::A;
use foo::L10N::A;

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    $self->{_lh} => foo::L10N::A->get_handle();
    return $self;
}

package foo::A1;
use base 'foo::A';
use foo::L10N::A1;

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    $self->{_lh} => foo::L10N::A1->get_handle();
    return $self;
}

package foo::A2;
use base 'foo::A';
use foo::L10N::A2;

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    $self->{_lh} => foo::L10N::A2->get_handle();
    return $self;
}

package foo::L10N::A;
use base 'Locale::Maketext';

package foo::L10N::A1;
use base 'foo::L10N::A';

package foo::L10N::A2;
use base 'foo::L10N::A';

This way, each lexicon class foo::L10N::A2::en, foo::L10N::A2::fr, 
foo::L10N::A1::en, foo::L10N::A1::fr has only to contains translations for 
its corresponding class, but each class has to retrieve its own language 
handle.

In my personal case, i found second strategy was the only solution, as i had 
static content to localise, so sharing a language handle in instances was not 
enough.

A third strategy (actually, just a variation around second one) was to keep 
lexicons classes in same file as corresponding application classes, this way, 
to avoid file cluttering, and to use magic soft references:

----- first file -----
package foo::A;

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    $self->{_lh} => __PACKAGE__::L10N->get_handle();
    return $self;
}

package foo::A::L10N;
use base 'Locale::Maketext';

----- second file -----
package foo::A1;
use base 'foo::A';

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    $self->{_lh} => __PACKAGE__::L10N->get_handle();
    return $self;
}

package foo::A1::L10N;
use base 'foo::A::L10N';

----- third file -----
package foo::A2;
use base 'foo::A';

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    $self->{_lh} => __PACKAGE__::L10N->get_handle();
    return $self;
}

package foo::A2::L10N;
use base 'foo::A::L10N';

However, this last one fails with error message:
'Maketext doesn't know how to translate xyz' for every call of maketext 
method. This seems to be mere a perl class loading problem instead of a 
maketext-related one.

Hope this help understanding my elucubrations :-)
-- 
Guillaume Rousse <rousse(_at_)ccr(_dot_)jussieu(_dot_)fr>
GPG key http://lis.snv.jussieu.fr/~rousse/gpgkey.html


<Prev in Thread] Current Thread [Next in Thread>