Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Translation selection advise

by johnfl68 (Beadle)
on Sep 04, 2015 at 02:30 UTC ( #1140952=perlquestion: print w/replies, xml ) Need Help??
johnfl68 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, looking for advise. I have some stock phrases that need to show up in different languages. Looking for the best means to do this.

Language is set with a variable, and then that is used to select which phrase to use at different places to display. There are only about 20 or so phrases that I need, but I keep adding additional languages.

I started out doing it this way (striped down a bit, but you should get the idea):

my $language = "fr"; # ar, bs, de, en, es, fr, it, nl, pl, pt, ru, sk, sv, tet, tr, + uk, zh if ( $language eq "en" ) { $windTranslation = "Wind:"; $humidityTranslation = "Humidity:"; } if ( $language eq "de" ) { $windTranslation = "Wind:"; $humidityTranslation = "Feuchtigkeit:"; } if ( $language eq "es" ) { $windTranslation = "Viento:"; $humidityTranslation = "Humedad:"; } if ( $language eq "fr" ) { $windTranslation = "Vent:"; $humidityTranslation = "Humidité:"; } if ( $language eq "nl" ) { $windTranslation = "Wind:"; $humidityTranslation = "Vochtigheid:"; }

But maybe it would be better to do it like this, as each phrase set would be together and cleaner code:

my $language = "de"; # ar, bs, de, en, es, fr, it, nl, pl, pt, ru, sk, sv, tet, tr, + uk, zh my %windTranslation = ( 'en' => "Wind:", 'de' => "Wind:", 'es' => "Viento:", 'fr' => "Vent:", 'nl' => "Wind:", ); my %humidityTranslation = ( 'en' => "Humidity:", 'de' => "Feuchtigkeit:", 'es' => "Humedad:", 'fr' => "Humidité:", 'nl' => "Vochtigheid:", ); my $windPhrase = $windTranslation{$language}; my $humidityPhrase = $humidityTranslation{$language};

Or is there maybe a better way, that I am not thinking of?

Please let me know your thoughts?

Thanks!

John

Replies are listed 'Best First'.
Re: Translation selection advise
by afoken (Abbot) on Sep 04, 2015 at 06:11 UTC

    But maybe it would be better to do it like this, as each phrase set would be together and cleaner code:

    my $language = "de"; # ar, bs, de, en, es, fr, it, nl, pl, pt, ru, sk, sv, tet, tr, + uk, zh my %windTranslation = ( 'en' => "Wind:", 'de' => "Wind:", 'es' => "Viento:", 'fr' => "Vent:", 'nl' => "Wind:", ); my %humidityTranslation = ( 'en' => "Humidity:", 'de' => "Feuchtigkeit:", 'es' => "Humedad:", 'fr' => "Humidité:", 'nl' => "Vochtigheid:", ); my $windPhrase = $windTranslation{$language}; my $humidityPhrase = $humidityTranslation{$language};

    Or is there maybe a better way, that I am not thinking of?

    This is at least better than a huge bunch of if-then-else.

    NetWallah++ is right, you better use a locale library.

    BUT: If you really need just about 20 terms, a library might be overkill. Use a HoH instead:

    my %translations=( en => { wind => 'wind', humidity => 'humidity' # ... }, de => { wind => 'Wind', humidity => 'Feuchtigkeit', # ... }, # ... ); print $translations{$language}->{'wind'},':';

    Alternatively, you can use the terms as first-level keys and the language as second-level key:

    my %translations=( wind => { en => 'wind', de => 'Wind', # ... }, humidity => { en => 'humidity', de => 'Feuchtigkeit', # ... }, # ... ); print $translations{'wind'}->{$language},':';

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Translation selection advise
by Laurent_R (Canon) on Sep 04, 2015 at 07:23 UTC
    Using a database for 20 terms or phrases sounds a bit like overkill. A flat file containing the data to be loaded into a hash of hashes should be sufficient and efficient, IMHO.

    Depending on how you're gonna use the data structure afterwards, you could have either a hash of languages, with sub-hashes of terms to be translated and their translations, or a hash of terms to be translated, with sub-hashes of languages with the translated terms.

Re: Translation selection advise
by NetWallah (Canon) on Sep 04, 2015 at 02:51 UTC

      and using modules such as Locale::Framework and

      :) lets skip that one, last updated in 2004, nothing on cpan uses it

Re: Translation selection advise
by QuillMeantTen (Friar) on Sep 04, 2015 at 07:44 UTC

    else you could use the given when feature depending on your perl version :

    Update : I'm not sure whether this will work, can not recall the exact syntax and I dont have the right perl version on this machine to test itt

    given ($language){ when('fr'){ $wind_translation='vent'; } when('es'){ $wind_translation='viento'; } }
      Hm, although you sometimes have to do it (at least for simple cases with just a couple or so of alternatives), storing what is really reference data in conditionals within the code is usually rather poor practice and generally does not scale well.

      If I have to store reference data within the code, I very much prefer to store it all in a self-contained data structure such as a hash, or a HoH, or whatever suits best the needs, at least if possible. Although in the case in point, we were really thinking of storing reference data in a separate file (of DB, or whatever) and loading it in the program on demand.

      Another point is that the smartmatch operator and the given ... when conditional have been sort-of deprecated (well, marked "experimental feature") and will issue a warning as of 5.18, so there are really some caveats to using them.

Re: Translation selection advise
by johnfl68 (Beadle) on Sep 04, 2015 at 13:13 UTC

    Thank you for your responses.

    I am probably going to go with the HOH route, as using Locale looks a bit daunting for me right now, though I understand the concept.

    Again, many thanks!

    John

Re: Translation selection advise
by Anonymous Monk on Sep 04, 2015 at 12:20 UTC
    Just do it, and do not now re-do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1140952]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-08-18 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (184 votes). Check out past polls.

    Notices?