| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org> | ||
| 4 | # Copyright (c) 2000-2002 Mitel Networks Corporation | ||
| 5 | # This software is distributed under the same licenses as Perl itself; | ||
| 6 | # see the file COPYING for details. | ||
| 7 | |||
| 8 | |||
| 9 | package CGI::FormMagick; | ||
| 10 | use I18N::LangTags; | ||
| 11 | use Text::Template 'fill_in_string'; | ||
| 12 | use Text::Iconv; | ||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | @ISA = qw(Exporter); | ||
| 16 | @EXPORT = qw(localise); | ||
| 17 | |||
| 18 | =pod | ||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | CGI::FormMagick::L10N - localization routines for FormMagick | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use CGI::FormMagick::L10N; | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | L10N (Localisation) is the name given to the process of providing | ||
| 30 | translations into another language. The previous step to this is I18N | ||
| 31 | (internationalisation) which is the process of making an application | ||
| 32 | ready to accept the translations. | ||
| 33 | |||
| 34 | We've done the work of I18N for you, so all you have to do is provide | ||
| 35 | translations for your apps. This is done using the <lexicon> XML | ||
| 36 | element, like so: | ||
| 37 | |||
| 38 | <form> | ||
| 39 | ... | ||
| 40 | </form> | ||
| 41 | |||
| 42 | <lexicon lang="fr"> | ||
| 43 | <entry> | ||
| 44 | <base>Hello</base> | ||
| 45 | <trans>Bonjour</trans> | ||
| 46 | </entry> | ||
| 47 | </lexicon> | ||
| 48 | |||
| 49 | Sometimes you need to substitute variables into the localised strings. There | ||
| 50 | are three ways to do this: | ||
| 51 | |||
| 52 | 1. Substitute another lexicon entry by it's base name. This substitution will happen automagically: | ||
| 53 | |||
| 54 | <lexicon lang="fr"> | ||
| 55 | <entry> | ||
| 56 | <base>This text includes a {$var}.</base> | ||
| 57 | <trans>A {$var} this text includes.</trans> | ||
| 58 | </entry> | ||
| 59 | <entry> | ||
| 60 | <base>var</base> | ||
| 61 | <trans>variable</trans> | ||
| 62 | </entry> | ||
| 63 | </lexicon> | ||
| 64 | |||
| 65 | 2. Use a custom method in your FormMagick subclass that returns a hash of | ||
| 66 | variables for substitution. This substitution will happen automagically | ||
| 67 | if you use the 'params' attribute of the lexicon XML element, and define | ||
| 68 | the named method in your FormMagick subclass: | ||
| 69 | |||
| 70 | <lexicon lang="fr" params="getLexiconParams()"> | ||
| 71 | <entry> | ||
| 72 | <base>This text includes a {$var}.</base> | ||
| 73 | <trans>A {$var} this text includes.</trans> | ||
| 74 | </entry> | ||
| 75 | </lexicon> | ||
| 76 | |||
| 77 | package MyFormMagick; | ||
| 78 | our @ISA = ('CGI::FormMagick'); | ||
| 79 | sub getLexiconParams | ||
| 80 | { | ||
| 81 | return (var => "variable"); | ||
| 82 | } | ||
| 83 | 1; | ||
| 84 | |||
| 85 | 3. Pass a hashref to the localise() method. This substitution will happen | ||
| 86 | automagically if you pass the hashref as an additional parameter to the | ||
| 87 | localise() method: | ||
| 88 | |||
| 89 | <lexicon lang="fr"> | ||
| 90 | <entry> | ||
| 91 | <base>This text includes a {$var}.</base> | ||
| 92 | <trans>A {$var} this text includes.</trans> | ||
| 93 | </entry> | ||
| 94 | </lexicon> | ||
| 95 | |||
| 96 | ... | ||
| 97 | my $text = 'This text includes a {$var}.'; | ||
| 98 | my $translated = $fm->localise($text, {var => 'variable'}); | ||
| 99 | ... | ||
| 100 | |||
| 101 | Localisation preferences are picked up from the HTTP_ACCEPT_LANGUAGE | ||
| 102 | environment variable passed by the user's browser. In Netscape, you set | ||
| 103 | this by choosing "Edit, Preferences, Navigator, Languages" and then | ||
| 104 | choosing your preferred language. | ||
| 105 | |||
| 106 | Localisation is performed on: | ||
| 107 | |||
| 108 | =over 4 | ||
| 109 | |||
| 110 | =item * | ||
| 111 | |||
| 112 | Form titles | ||
| 113 | |||
| 114 | =item * | ||
| 115 | |||
| 116 | Page titles and descriptions | ||
| 117 | |||
| 118 | =item * | ||
| 119 | |||
| 120 | Field labels and descriptions | ||
| 121 | |||
| 122 | =item * | ||
| 123 | |||
| 124 | Validation error messages | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | If you wish to localise other textual information such as your HTML | ||
| 129 | Templates, you will have to explicitly call the l10n routines. | ||
| 130 | |||
| 131 | =head1 USER METHODS | ||
| 132 | |||
| 133 | =head2 localise($string) | ||
| 134 | |||
| 135 | Translates a string into the end-user's preferred language by checking | ||
| 136 | their HTTP_ACCEPT_LANG variable and looking up a lexicon hash for that | ||
| 137 | language (if it exists). If no translation can be found, returns the | ||
| 138 | original string untranslated. | ||
| 139 | |||
| 140 | Takes the text to translate as the first argument, and optionally, a hashref of | ||
| 141 | variables for substitution as the second argument. | ||
| 142 | |||
| 143 | WARNING WARNING WARNING: The internals of this routine will change | ||
| 144 | significantly in version 0.60, when we remove Locale::Maketext from | ||
| 145 | the equation. However, its output should still be the same. Just FYI. | ||
| 146 | |||
| 147 | =begin testing | ||
| 148 | |||
| 149 | BEGIN: { | ||
| 150 | use_ok('CGI::FormMagick'); | ||
| 151 | use vars qw($fm); | ||
| 152 | use lib "lib/"; | ||
| 153 | } | ||
| 154 | |||
| 155 | $ENV{HTTP_ACCEPT_LANGUAGE} = 'fr, en, de'; | ||
| 156 | my $fm = CGI::FormMagick->new(type => 'file', source => "t/lexicon.xml"); | ||
| 157 | $fm->parse_xml(); # suck in lexicon without display()ing | ||
| 158 | |||
| 159 | is($fm->localise("yes"), "oui", "Simple localisation"); | ||
| 160 | is($fm->localise("Hello"), "Bonjour", "Simple localisation"); | ||
| 161 | is($fm->localise("xyz"), "xyz", "Attempted localisation of untranslated string"); | ||
| 162 | is($fm->localise(""), "", "Fail gracefully on localisation of empty string"); | ||
| 163 | |||
| 164 | # Lexicon variable substitution tests | ||
| 165 | { | ||
| 166 | package MyFormMagick; | ||
| 167 | our @ISA = ('CGI::FormMagick'); | ||
| 168 | sub new { | ||
| 169 | shift; | ||
| 170 | my $self = CGI::FormMagick->new(@_); | ||
| 171 | $self->{calling_package} = (caller)[0]; | ||
| 172 | return bless $self; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub getLexiconParams { | ||
| 176 | return (params_var => "'params' method variable"); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | my $mfm = MyFormMagick->new(type=> 'file', source => "t/lexicon-params.xml"); | ||
| 181 | $mfm->parse_xml(); | ||
| 182 | is($mfm->localise('This text contains a {$var}.', {var => "variable"}), | ||
| 183 | "A variable this text contains.", | ||
| 184 | "Lexicon variable substitution from hashref arg to localise()"); | ||
| 185 | is($mfm->localise('This text contains a {$var}.'), | ||
| 186 | "A lexicon variable this text contains.", | ||
| 187 | "Lexicon variable substitution from lexicon entry"); | ||
| 188 | is($mfm->localise('This text contains a {$params_var}.'), | ||
| 189 | "A 'params' method variable this text contains.", | ||
| 190 | "Lexicon variable substitution from 'params' subclass method"); | ||
| 191 | |||
| 192 | |||
| 193 | =end testing | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub localise { | ||
| 198 | my ($fm, $string, $hashref) = @_; | ||
| 199 | $string = "" unless defined $string; | ||
| 200 | $hashref = {} unless keys %$hashref; | ||
| 201 | my $text; | ||
| 202 | my %params = $fm->{lexicon} ? %{$fm->{lexicon}} : (); | ||
| 203 | %params = (%params, $fm->_get_lexicon_params(), %$hashref); | ||
| 204 | if (my $trans = $fm->{lexicon}->{$string}) { | ||
| 205 | $text = fill_in_string($trans, HASH=>\%params); | ||
| 206 | } else { | ||
| 207 | $text = fill_in_string($string, HASH=>\%params); | ||
| 208 | } | ||
| 209 | return $text; | ||
| 210 | } | ||
| 211 | |||
| 212 | =pod | ||
| 213 | |||
| 214 | =head2 check_l10n() | ||
| 215 | |||
| 216 | print out lexicons to check whether they're what you think they are | ||
| 217 | this is mostly for debugging purposes. If you have DEBUG set to 1 in | ||
| 218 | your call to the new() method, you'll see a link at the bottom of each | ||
| 219 | page that says "Check L10N". This is the subroutine that's called when | ||
| 220 | you follow that link. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub check_l10n { | ||
| 225 | my $self = shift; | ||
| 226 | print qq( <p>Your choice of language: $ENV{HTTP_ACCEPT_LANGUAGE}</p>); | ||
| 227 | my @langs = split(/, /, $ENV{HTTP_ACCEPT_LANGUAGE}); | ||
| 228 | foreach my $lang (@langs) { | ||
| 229 | print qq(<h2>Language: $lang</h2>); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | =head2 get_lexicon() | ||
| 234 | |||
| 235 | Attempts to find a suitable localisation lexicon for use, and returns it | ||
| 236 | as a hash. | ||
| 237 | |||
| 238 | =begin testing | ||
| 239 | |||
| 240 | my $lextest = CGI::FormMagick->new( | ||
| 241 | type => "file", | ||
| 242 | source => "t/lexicon-merge.xml" | ||
| 243 | ); | ||
| 244 | |||
| 245 | $lextest->parse_xml(); # suck in lexicon without display()ing | ||
| 246 | |||
| 247 | $ENV{HTTP_ACCEPT_LANGUAGE} = 'fr'; | ||
| 248 | is($ENV{HTTP_ACCEPT_LANGUAGE}, 'fr', "Set HTTP_ACCEPT_LANGUAGE to 'fr'"); | ||
| 249 | is($lextest->localise("Hello"), "Bonjour", "Retained info from first lexicon"); | ||
| 250 | is($lextest->localise("yes"), "certainement", "Picked up info from second (merged) lexicon"); | ||
| 251 | |||
| 252 | =end testing | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub get_lexicon { | ||
| 257 | my $self = shift; | ||
| 258 | my (@lexicons) = @_; | ||
| 259 | |||
| 260 | my @preferred_languages = $self->get_languages(); | ||
| 261 | |||
| 262 | my %lexicons; | ||
| 263 | foreach my $lex (@lexicons) { | ||
| 264 | $lexlang = lc $lex->[0]->{lang}; | ||
| 265 | |||
| 266 | # merge multiple lexicons of the same language | ||
| 267 | if ($lexicons{$lexlang}) { | ||
| 268 | $lexicons{$lexlang} = [ @{$lexicons{$lexlang}}, $lex ]; | ||
| 269 | } else { | ||
| 270 | $lexicons{$lexlang} = [ $lex ]; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | my %thislex = (); | ||
| 277 | PL: foreach my $pl (@preferred_languages) { | ||
| 278 | if ($lexicons{$pl}) { | ||
| 279 | %thislex = $self->clean_lexicon(@{$lexicons{$pl}}); | ||
| 280 | last PL; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | return %thislex; | ||
| 284 | } | ||
| 285 | |||
| 286 | =head2 _set_lexicon_params(@lexicons) | ||
| 287 | |||
| 288 | Given a hash ref of lexicon attributes, find the 'params' attribute | ||
| 289 | containing a method that returns a params hash (if set), and save it as an | ||
| 290 | object attribute. | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub _set_lexicon_params | ||
| 295 | { | ||
| 296 | my $self = shift; | ||
| 297 | my $lexicon = shift; | ||
| 298 | |||
| 299 | return unless $lexicon->{params}; | ||
| 300 | |||
| 301 | push @{$self->{lexicon_params}}, $lexicon->{params}; | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 _get_lexicon_params() | ||
| 305 | |||
| 306 | Return the merged params hash for the preferred lexicon. | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | sub _get_lexicon_params | ||
| 311 | { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | my %params; | ||
| 315 | foreach my $p (@{$self->{lexicon_params}}) | ||
| 316 | { | ||
| 317 | my %lex = eval("\$self->$p"); | ||
| 318 | %params = (%params, %lex) if keys %lex; | ||
| 319 | } | ||
| 320 | return %params; | ||
| 321 | } | ||
| 322 | |||
| 323 | =head2 clean_lexicon(@lexicons) | ||
| 324 | |||
| 325 | Given an array of messy lexicons, cleans them up into a nice neat hash of | ||
| 326 | base/translation pairs. | ||
| 327 | |||
| 328 | It's an array because you might have more than one lexicon for the same | ||
| 329 | langauge. These get merged into one lexicon hash, so that the first | ||
| 330 | lexicon of a given language will be overridden by the second. | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub clean_lexicon { | ||
| 335 | my $self = shift; | ||
| 336 | my @dirty_lexicons = @_; | ||
| 337 | my %return_lexicon; | ||
| 338 | my $iconv; | ||
| 339 | |||
| 340 | if ($self->{charset}) | ||
| 341 | { | ||
| 342 | $iconv = Text::Iconv->new('UTF8', $self->{charset}); | ||
| 343 | } | ||
| 344 | |||
| 345 | foreach my $dl (@dirty_lexicons) { | ||
| 346 | # strip first element (the language) which is not needed | ||
| 347 | my @stripped = @$dl; | ||
| 348 | # grab params hash from the lexicon attributes | ||
| 349 | $self->_set_lexicon_params(shift @stripped); | ||
| 350 | my @entries = CGI::FormMagick->clean_xml_array(@stripped); | ||
| 351 | foreach my $e (@entries) { | ||
| 352 | $base = $e->{content}->[4]->[2]; | ||
| 353 | $trans = $e->{content}->[8]->[2]; | ||
| 354 | if ($base && $trans) { | ||
| 355 | $trans =~ s/^\s*//; # Remove leading whitespace | ||
| 356 | $trans =~ s/\s*$//; # Remove trailing space | ||
| 357 | $return_lexicon{$base} = ($iconv | ||
| 358 | ? $iconv->convert($trans) | ||
| 359 | : $trans); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | } | ||
| 363 | return %return_lexicon; | ||
| 364 | } | ||
| 365 | |||
| 366 | =head2 get_languages() | ||
| 367 | |||
| 368 | Picks up the preferred language(s) from $ENV{HTTP_ACCEPT_LANGUAGE}. | ||
| 369 | Figures out super-languages (eg "en" is a super-language of "en-US") and | ||
| 370 | pushes them onto the list, along with the fallback language specified by | ||
| 371 | $fm->fallback_language() (if any). Smashes them all to lowercase before | ||
| 372 | returning them. | ||
| 373 | |||
| 374 | =begin testing | ||
| 375 | |||
| 376 | my $fm = CGI::FormMagick->new(); | ||
| 377 | $ENV{HTTP_ACCEPT_LANGUAGE} = "fr, de, en"; | ||
| 378 | $fm->fallback_language("sv"); | ||
| 379 | is($fm->fallback_language(), "sv", "Set fallback language"); | ||
| 380 | my @langs = $fm->get_languages(); | ||
| 381 | is($langs[1], "de", "pick up list of languages"); | ||
| 382 | is($langs[$#langs], "sv", "pick up fallback language"); | ||
| 383 | |||
| 384 | $ENV{HTTP_ACCEPT_LANGUAGE} = "en-US"; | ||
| 385 | @langs = $fm->get_languages(); | ||
| 386 | ok(grep(/^en$/, @langs), "pick up super-languages"); | ||
| 387 | ok(grep(/^en-us$/, @langs), "smash langtags to lowercase."); | ||
| 388 | |||
| 389 | $ENV{HTTP_ACCEPT_LANGUAGE} = "en-us,de;q=0.7,fr-ca;q=0.3"; | ||
| 390 | @langs = $fm->get_languages(); | ||
| 391 | is($langs[1], "de", "parsed q-format language header"); | ||
| 392 | ok(grep(/^fr$/, @langs), "pick up super-languages"); | ||
| 393 | is($langs[$#langs], "sv", "pick up fallback language"); | ||
| 394 | |||
| 395 | =end testing | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | # | ||
| 400 | # This routine as written in v<=0.79 only supported Netscape. IE uses an | ||
| 401 | # optional feature of the HTTP_ACCEPT_LANGUAGE header to provide a preference | ||
| 402 | # value (q=n.n) for languages. It also does not put spaces in the header, | ||
| 403 | # whereas Netscape does. The code below should parse both types. | ||
| 404 | # | ||
| 405 | # An example Netscape header: HTTP_ACCEPT_LANGUAGE=en-US, en, fr-CA | ||
| 406 | # The same choices in IE 5: HTTP_ACCEPT_LANGUAGE=en-us,en;q=0.7,fr-ca;q=0.3 | ||
| 407 | # | ||
| 408 | # --m. knox <segfault@hardline.org> | ||
| 409 | # | ||
| 410 | |||
| 411 | sub get_languages { | ||
| 412 | my $self = shift; | ||
| 413 | |||
| 414 | # split on the comma to get elements. some may have ;q=n.n attached | ||
| 415 | # and there may be spaces. | ||
| 416 | my $hal = CGI->http('HTTP_ACCEPT_LANGUAGE') || ""; | ||
| 417 | my @temp = split ",", $hal; | ||
| 418 | |||
| 419 | my @langs; | ||
| 420 | foreach my $item (@temp) | ||
| 421 | { | ||
| 422 | # lose any spaces | ||
| 423 | $item =~ /(\S+)/; | ||
| 424 | $item = $1; | ||
| 425 | |||
| 426 | # lose any q= preference values (the ordering is enough preference) | ||
| 427 | if ($item =~ /^(\S+);q=\d+.\d+/) | ||
| 428 | { | ||
| 429 | $item = $1; | ||
| 430 | } | ||
| 431 | push @langs, $item; | ||
| 432 | } | ||
| 433 | |||
| 434 | push @langs, map { I18N::LangTags::super_languages($_) } @langs; | ||
| 435 | push @langs, $self->{fallback_language} if $self->{fallback_language}; | ||
| 436 | return map lc(), @langs; | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | =head1 SEE ALSO | ||
| 441 | |||
| 442 | The general documentation for FormMagick (C<perldoc CGI::FormMagick>) | ||
| 443 | |||
| 444 | More information about FormMagick may be found at | ||
| 445 | http://sourceforge.net/projects/formmagick/ | ||
| 446 | |||
| 447 | =cut | ||
| 448 | |||
| 449 | 1; |