I think I wouuld reverse the order of the indexes, $lang first, so you can take a reference to the whole hash of all translations for one language. I do assume that you actually want to use just one language for the output, and this way, you can drop the reference to the language itself throughout the code. Yes, I would use hashes, and string indexes, in order to get something you can read. A bit like:
(but not actually generation it this way), then, in your code, you can do:$translat{English}{red} = 'red'; $translat{English}{blue} = 'blue'; $translat{English}{yellow} = 'yellow'; $translat{Spanish}{red} = 'rojo'; $translat{Spanish}{blue} = 'azul'; $translat{Spanish}{yellow} = 'amarillo';
producing:$t = $translat{Spanish}; print "The proper word for red is '$t->{red}'\n";
Now, as for maintaining the translations, you could use a spreadsheet-like file, perhaps as a CSV file or tab delimited text. Your people can maintain the translation data even using Excel. Part of the data can look like:The proper word for red is 'rojo'
| English | Spanish | French | |
|---|---|---|---|
| red | red | rojo | rouge |
| blue | blue | azul | bleu |
| yellow | yellow | amarillo | jaune |
The first row is the languages, the first column the hash keys. You might think that it's redundant with the English word, but this is a way to distinguish between synonyms, the hash key doesn't have to be the same as the English word, for example with a word "blue" having more than one meaning, you can use "colour_blue" as the hash key for the colour.
Back to your question... You can combine strings into one hash key, for example "colour_0" for red, "colour_1" for blue, and "colour_2" for yellow, thus you can do your lookup using $t->{"colour_$colour"}. Maintainable and flexible. I like that.
Addendum: Some Perl code to read such a tab delimited text file into a HoH as described:
You can do this once, and then save the data with, for example, Storable, or Data::Dumper, so it can be loaded more quickly when in live use. Hmm... the latter can actually produce such a Perl library file as you contemplated to use.$_ = <TABLE>; chomp; (undef, my @lang) = split /\t/; while(<TABLE>) { chomp; my($key, @text) = split /\t/; for my $i (0 .. $#lang) { $translat{$lang[$i]}{$key} = $text[$i]; } }
In reply to Re: Do I need an array within an array
by bart
in thread Do I need an array within an array
by meetn2veg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |