On Tue, 9 Jan 2007, Kovacs Baldvin wrote:
> I am wondering if a different approach would be possible: why don't we
> hack the mason compiler to compile not just .obj files from the
> templates, but .en.obj, .fr.obj, .de.obj, ... When a component is to be
> run, then the appropriate .obj file is taken, and would run the same
> speed as there were no internationalization would be in place.

The way I do it is to override Mason's Resolver class.  The language is
part of the URL, and my Resolver actually reads the html file from a
common directory.  This way there is only one copy of the untranslated
mason source, but the mason cache has one copy for each language.

We use <trans key=xyzzy> tags to delimit the translatable text.  Mason's
"<% %>" tags are replaced by "{1}" in the translation database, so they
can be reordered.  (In my experience, even that much flexibility confuses
many contractual translators.)

I cannot share my company's translation code, but here is the get_info
function from our Resolver subclass.  It just a modified version
of the code from HTML::Mason::Resolver::File.  Modifying this use to
Maketext is left as an exercise for the reader.

sub get_info {
        my ($self, $path) = @_;

        # get the language handle
        my $tr = TNI::Translation->new(
                comp_root => $self->comp_root,
                path => $path,
        );
        # language is determined from the URL
        my $language = $tr->language;
        # when the translation database was last modified
        my $modified = $tr->domain_modified( unix=>1 ) || 0;

        # find the untranslated file
        my $english = $path;
        $english =~ s{//+}{/}g;
        $english =~ s{^/([^/]*?)/\Q$language\E/}{/default/english/} if 
$language;

        foreach my $pair ($self->comp_root_array) {
                my $fname = File::Spec->canonpath( File::Spec->catfile( 
$pair->[1], $path ) );
                my $srcfile = File::Spec->canonpath( File::Spec->catfile( 
$pair->[1], $english ) );

                next unless -f $srcfile;
                my $filemodified = (stat _)[9];
                $modified = $filemodified if $filemodified > $modified;

                my $key = $pair->[0];
                my $base = $key eq 'MAIN' ? '' : "/$key";
                $key = undef if $key eq 'MAIN';

                return
                        HTML::Mason::ComponentSource->new
                                        ( friendly_name => $fname,
                                          comp_id => "$base$path",
                                          last_modified => $modified,
                                          comp_path => $path,
                                          comp_class => 
'HTML::Mason::Component::FileBased',
                                          extra => { comp_root => $key },
                                          #source_callback => sub { 
read_file_ref($srcfile) },
                                          source_callback => sub {
# read the source file and translate
TNI::Translation::Parse::HTML->new( tr=>$tr 
)->translate_page_ref(read_file_ref($srcfile));
                                          },
                                        );
        }

        # see if path corresponds to real filesystem path, a common new user 
mistake
        my $fs_path = File::Spec->catfile( split /\//, $path );
        if ( defined $fs_path && -e $fs_path )
        {
                warn "Your component path ($path) matches a real file on disk 
($fs_path).  Have you read about the component root in the Administrator's 
Manual (HTML::Mason::Admin)?";
        }

        return;
}





-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to