Code optimization request and i18n issues 
Author Message
 Code optimization request and i18n issues

Hello

I'm writing a simple i18n translation class for a web app and trying to
keep things portable, so I've assumed it's safer to keep clear of gettext()
etc.

The blessed hash is the translation hash itself eg:

  bless {
    hello => { en => 'Hello %1',
               fr => 'Bonjour %1',
               es => 'Hola %1',
             },
  }, "Rich::I18N";

And the translation method is currently:

  sub translate
  {

    my $value = $this->{$key};

    my $message;

    foreach ( split(',', $ENV{'HTTP_ACCEPT_LANGUAGE'}) )
    {
      last if ( $message = $value->{ lc($_) } );
    }

    $message ||= $value->{'en'} ||
     (warn "Default 'en' value missing for key '$key'", return '');


    # Replace % placeholders with actual data

    $message =~ s/\%(\d+)/$args[$1 - 1]/g;

    return $message;
  }

So, in my code I do something like eg:

  print $i18n->translate("hello", "Rich");

Things work fine as they are, but I'm wondering if there are ways to
optimize the translate method or replace it with a better solution and
would be interested to hear any ideas from folks - preferably ones that
have been tested on real world sites ;->

I'm also wondering how to handle such issues as character sorting - I'm  
guessing that something like switching locales per HTTP request is going to
fail miserably, so do I rely solely on the back end database handling
sorting, or is there an alternative perl solution - I've looked at a couple
of sites offering advice but found it pretty hard going - of course I
realise that's mainly because it's not a simple issue!

Any suggestions/comments gladly received.

Rich



Wed, 07 Jul 2004 23:24:53 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Optimization of Perl code

2. code optimization and readability

3. Optimization of the code - suggestions?

4. Code Optimization-- Can I do this better?

5. Code Optimization-- Can I do this better?

6. Code optimization: Anyone got a few spare minutes?

7. Optimization of date calculation code

8. Request: code fragment of message queues or shared memory on Solaris

9. tzset() module: code and request for comments

10. Slow code, improvements requested

11. REQUEST: for indent which handles perl code

12. Code review requested, please.

 

 
Powered by phpBB® Forum Software