1 package Onis::Language;
8 use Onis::Config (qw(get_config));
12 Onis::Language - Translate strings to a user-defined language.
16 @Onis::Language::EXPORT_OK = qw/translate/;
17 @Onis::Language::ISA = ('Exporter');
19 our %Translations = ();
22 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
24 read_language_file ();
28 =head1 CONFIGURATION OPTIONS
32 =item B<language_file>: I<german.lang>;
34 Tries to open and read the language-definitions from this file. If it fails
35 (file does not exist, is not readable, uses an unknown syntax and the like) the
36 default-language, english, will be used.
42 sub read_language_file
46 my $file = get_config ('language_file');
53 unless (open ($fh, "< $file"))
55 print STDERR $/, __FILE__, ": Unable to open language file ``$file''. Will use default-language english.", $/;
65 if ($line =~ m/^((?:"(?:[^\\"]|\\.)*"|[^#])*)#/)
70 while ($line =~ m/"((?:[^\\"]|\\.)+)"/g)
75 if (scalar (@strings) < 2)
80 my $key = shift (@strings);
81 $Translations{$key} = \@strings;
88 =head1 EXPORTED FUNCTIONS
92 =item B<translate> (I<$string>)
94 Translates the given string using the language file loaded. If no translation
95 is found returns the original string.
104 if (defined ($Translations{$string}))
106 my $array = $Translations{$string};
108 if (scalar (@$array) == 1)
110 $retval = $array->[0];
114 my $num = scalar (@$array);
115 my $pick = int (rand ($num));
117 $retval = $array->[$pick];
124 $retval = '<span style="color: red; background-color: yellow;">'
125 . $string . '</span>';
140 Florian octo Forster E<lt>octo at verplant.orgE<gt>