| 1 | # Benjamin H Kram <ben@base16consulting.com> | ||
| 2 | # This program is free software; you can redistribute it and/or modify | ||
| 3 | # it under the same terms as Perl itself. | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | MasonX::Resolver::Polyglot - Component path resolver for easy internationalization | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | In your http.conf: | ||
| 12 | |||
| 13 | PerlInitHandler MasonX::Resolver::Polyglot | ||
| 14 | <Directory /var/www/html> | ||
| 15 | PerlSetVar PolyglotDefaultLang en | ||
| 16 | PerlSetVar PolyglotDefaultURILang en | ||
| 17 | PerlAddVar MasonDataDir "/var/www/mason" | ||
| 18 | PerlAddVar MasonCompRoot "/var/www/html" | ||
| 19 | <FilesMatch "^..$|\.html(\...)?$"> | ||
| 20 | SetHandler perl-script | ||
| 21 | PerlSetVar MasonResolverClass MasonX::Resolver::Polyglot | ||
| 22 | PerlHandler HTML::Mason::ApacheHandler | ||
| 23 | </FilesMatch> | ||
| 24 | </Directory> | ||
| 25 | |||
| 26 | Or, in your Mason guts: | ||
| 27 | |||
| 28 | my $resolver = MasonX::Resolver::Polyglot->new( comp_root => '/var/www/mason' ); | ||
| 29 | my $info = $resolver->get_info('/some/comp.html'); | ||
| 30 | my $comp_root = $resolver->comp_root; | ||
| 31 | |||
| 32 | =head1 CONFIGURATION | ||
| 33 | |||
| 34 | =item C<PolyglotDefaultLang> sets the fall back language. If unset, Polyglot will fall back on a file with no suffix (foo.html). | ||
| 35 | |||
| 36 | =item C<PolyglotDefaultURILang> overrides language selection based upon Language-Accept. It is equivalent to prefixing the url with a language code (http://foo.com/en-us/foo.html) | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This C<HTML::Mason::Resolver::File::ApacheHandler> subclass enables Mason to determine the client's language preference and find the best matching component to fulfill it. | ||
| 41 | |||
| 42 | This allows a web designer to provide structure in language independant components, and confine language-centric HTML to other components that the top level pages use. | ||
| 43 | |||
| 44 | Components are labeled by suffix. | ||
| 45 | |||
| 46 | Examples: | ||
| 47 | index.html - language independant component. Either last try component (if no other languages are acceptable) or the default language (if C<PolyglotDefaultLang> is set). | ||
| 48 | index.html.es - Spanish component. If a browser's Language-Accept describes Spanish as more preferable than English, requests for index.html will return this component. | ||
| 49 | |||
| 50 | There is nothing magic about the html suffix; these do not have to be top level components. Let us suppose that index.html has a component called "menubar" which has text or image buttons of the site map. We may write the following components: | ||
| 51 | menubar - the English version (we have set PolyglotDefaultLang to "en") | ||
| 52 | menubar.en-us - the American English version | ||
| 53 | menubar.fr - the French version | ||
| 54 | menubar.it - the Italian version | ||
| 55 | |||
| 56 | The code in index.html just calls "menubar" normally, and the resolver will pick the "right" component, ultimately falling back on the unsuffixed version if it can't find a better match. | ||
| 57 | |||
| 58 | There are really two pieces to Polyglot. The Mason resolver piece is a child of HTML::Mason::Resolver::File::ApacheHandler and compares the Language-Accept preferences a web client presents with what is available on the filesystem, and finds the best match. | ||
| 59 | |||
| 60 | The other piece is the PerlInitHandler which scans (and potentially alters) the URL for a leading language code. The effect this has is to override all preferences. | ||
| 61 | If, for some reason, you want to peek at the URI that actually was typed in before Polyglot ate the language code, it is stashed away in $r->pnotes('POLYGLOT_URI'). | ||
| 62 | |||
| 63 | Like our aformentioned English/Spanish site, we have an English index.html, and a Spanish index.html.es. My site wants to provide the ability to choose the site language without mucking with the brower's language preference. | ||
| 64 | In my index.html, I have a "Spanish" link which links to "/es/index.html", and an "English" link in my index.html.es that links to "/index.html". I make all other links in the site _relative_. | ||
| 65 | |||
| 66 | The effect this has is to propagate the /es/ prefix, consistantly overriding the browser's language preference until the user clicks on an absolute URL. | ||
| 67 | |||
| 68 | Polyglot now makes its language decision order array available through the Apache request pnotes() interface as an array ref. | ||
| 69 | If you call: | ||
| 70 | |||
| 71 | my @langs = @{$r->pnotes('POLYGLOT_LANGS')}; | ||
| 72 | |||
| 73 | @langs will contain a ranked list of language preference. | ||
| 74 | |||
| 75 | It makes the language decision it made available by: | ||
| 76 | |||
| 77 | my $lang = $r->pnotes('POLYGLOT_LANG'); | ||
| 78 | |||
| 79 | And also, the original pre-language-stripped URI available like so: | ||
| 80 | |||
| 81 | my $origuri = $r->pnotes('POLYGLOT_URI') | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | package MasonX::Resolver::Polyglot; | ||
| 86 | $VERSION = q(0.95); | ||
| 87 | |||
| 88 | use strict; | ||
| 89 | |||
| 90 | # We need this, since our parent is embedded in the HTML::Mason::ApacheHandler file | ||
| 91 | use HTML::Mason::ApacheHandler; | ||
| 92 | use base qw(HTML::Mason::Resolver::File); | ||
| 93 | |||
| 94 | use HTML::Mason::Tools qw(paths_eq); | ||
| 95 | use Locale::Language qw(code2language); | ||
| 96 | use Locale::Country qw(LOCALE_CODE_ALPHA_2 LOCALE_CODE_ALPHA_3 code2country); | ||
| 97 | use Apache::Constants; | ||
| 98 | |||
| 99 | my $DEBUG = 0; | ||
| 100 | # This is the name of the env variable that uri_override uses | ||
| 101 | my $POLYGLOT_LANG = q(POLYGLOT_LANG); | ||
| 102 | my $PolyglotDefaultLang = q(PolyglotDefaultLang); | ||
| 103 | my $PolyglotDefaultURILang = q(PolyglotDefaultURILang); | ||
| 104 | |||
| 105 | sub new{ | ||
| 106 | my $class = shift; | ||
| 107 | my $self = $class->SUPER::new(@_); | ||
| 108 | $self->{default_lang} = lc Apache->request->dir_config($PolyglotDefaultLang); | ||
| 109 | $self->{default_uri_lang} = lc Apache->request->dir_config($PolyglotDefaultURILang); | ||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub get_info{ | ||
| 114 | my ($self, $path, $comp_root_key, $comp_root_path) = @_; | ||
| 115 | |||
| 116 | # Is this already stored somewhere I can grab it? | ||
| 117 | # I suspect this is wasteful. | ||
| 118 | my $r = Apache->request; | ||
| 119 | |||
| 120 | $DEBUG && $r->log_error(qq(URI:) . $r->uri . qq(, path: $path)); | ||
| 121 | $DEBUG && $r->log_error(qq(Header says: ), $r->header_in('Accept-Language')); | ||
| 122 | # Get a ranked list of language prefs based on the Accept-Language and URI | ||
| 123 | # everything in get_langs will need an $r | ||
| 124 | $self->{r} = $r; | ||
| 125 | my @langs = @{$self->get_langs(\$path)}; | ||
| 126 | $DEBUG && $r->log_error("Languages Accepted: ", join(",", @langs)); | ||
| 127 | delete $self->{r}; | ||
| 128 | |||
| 129 | # If we have a default language set, then "" gets spliced in | ||
| 130 | # immediately after that language in the pref list. | ||
| 131 | |||
| 132 | if($self->{default_lang}){ | ||
| 133 | for(0..$#langs){ | ||
| 134 | if($langs[$_] eq $self->{default_lang}){ | ||
| 135 | splice(@langs, $_+1, 0, ""); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # No matter what, lastly look for the "pure" version | ||
| 141 | push @langs, ""; # so we check a no extension lang last | ||
| 142 | |||
| 143 | # CHECK to see if any exist in filesystem | ||
| 144 | my $comp; | ||
| 145 | |||
| 146 | # Make language order available through $r->pnotes | ||
| 147 | my @POLYGLOT_LANGS = @langs; | ||
| 148 | $r->pnotes('POLYGLOT_LANGS', \@POLYGLOT_LANGS); | ||
| 149 | while(defined ($_ = shift @langs)){ | ||
| 150 | $DEBUG && $r->log_error(join("", $path, $_?('.', $_):""), $comp_root_key, $comp_root_path); | ||
| 151 | if($comp = $self->SUPER::get_info(join("", $path, $_?('.', $_):""), $comp_root_key, $comp_root_path)){ | ||
| 152 | $DEBUG && $r->log_error("picked '$_'"); | ||
| 153 | return $comp; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | =head1 METHODS | ||
| 160 | |||
| 161 | =over 4 | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | # This resolver has a few new methods that it uses internally to determine what component to choose. | ||
| 166 | #=item get_langs | ||
| 167 | #This stores and returns a ranked list of components to try, using the URL and the client's language preferences to order them. | ||
| 168 | #=cut | ||
| 169 | |||
| 170 | sub get_langs{ | ||
| 171 | my ($self, $path) = @_; | ||
| 172 | # path is a scalar ref to the path that was fed to the resolver | ||
| 173 | |||
| 174 | my %Accept; | ||
| 175 | $self->_get_client_pref(\%Accept); | ||
| 176 | # URL overrides browser | ||
| 177 | $self->_get_env_pref(\%Accept); | ||
| 178 | my @langs = sort { $Accept{$b}{q} <=> $Accept{$a}{q} } keys %Accept; | ||
| 179 | $self->{langs} = \@langs; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | # =item _get_client_pref | ||
| 184 | |||
| 185 | # This takes a hashref, which it will populate with the client's language preferences. | ||
| 186 | # This looks for and parses the I<Accept-Language> header, and stores the q values as values. | ||
| 187 | |||
| 188 | #=cut | ||
| 189 | |||
| 190 | sub _get_client_pref{ | ||
| 191 | my ($self, $Accept) = @_; | ||
| 192 | |||
| 193 | my $r = $self->{r}; | ||
| 194 | # Determine Client preference | ||
| 195 | my $accept = $r->header_in('Accept-Language'); | ||
| 196 | my ($lang, @quality); | ||
| 197 | my ($qkey, $qval); | ||
| 198 | if($accept){ | ||
| 199 | for(split(/\s*,\s*/, $accept)){ | ||
| 200 | ($lang, @quality) = split(/\s*;\s*/); | ||
| 201 | $lang =~ tr/A-Z_/a-z-/; | ||
| 202 | unless(@quality){ | ||
| 203 | $$Accept{$lang}{q} = 1; | ||
| 204 | } | ||
| 205 | # Can there be more than one ';' tag on a lang? | ||
| 206 | for(@quality){ | ||
| 207 | ($qkey,$qval) = split(/\s*=\s*/); | ||
| 208 | # Thanks to Dorian Taylor <dorian@foobarsystems.com> for this | ||
| 209 | # Some UAs use 'qs' | ||
| 210 | if($qkey =~ /^qs?$/){ | ||
| 211 | $$Accept{$lang}{q} = | ||
| 212 | $qval eq '' ? 1 : | ||
| 213 | $qval > 1 ? 1 : | ||
| 214 | $qval < 0 ? 0 : $qval; | ||
| 215 | }else{ | ||
| 216 | # Some other key type | ||
| 217 | $$Accept{$lang}{$qkey} = $qval; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | =item uri_override | ||
| 225 | |||
| 226 | This has an alias as I<handler> so you don't have to specify the method if you set MasonX::Resolver::Polyglot as a PerlInitHandler. | ||
| 227 | |||
| 228 | This examines the URL for a leading, lowercase language tag of the format langcode<-sublangcode> (I<en>, I<en-us>, I<es> etc.). | ||
| 229 | |||
| 230 | If it finds one, it will give that language the highest precidence, and MODIFY THE URL, REMOVING THE LANGUAGE TAG. | ||
| 231 | |||
| 232 | The upshot of this is that regardless of the browser's Accept-Language preference, it can be overriden using the URL. | ||
| 233 | |||
| 234 | =over 4 | ||
| 235 | |||
| 236 | http://www.mydomain.com/colors/red.html - gives me an English page | ||
| 237 | |||
| 238 | http://www.mydomain.com/es/colors/red.html - forces it to give me the Spanish page (if it exists) | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | # This is so we can just use the class as a PerlInitHandler without one of them fancy arrows | ||
| 243 | *handler = \&uri_override; | ||
| 244 | |||
| 245 | sub uri_override{ | ||
| 246 | # This a a mod_perl Apache handler - | ||
| 247 | # it is intended as a PerlInitHandler so it can manipulate the incoming request's URL | ||
| 248 | my $r = shift; | ||
| 249 | # CHECK URL to see preferred language - it will be prepended | ||
| 250 | # This method allows the url to override the client pref, | ||
| 251 | # but still expresses it as a preference - | ||
| 252 | # we can still fall back on another lang for a component | ||
| 253 | # !!! This will MODIFY the URI, extracting out the leading language tag | ||
| 254 | $DEBUG && $r->log_error("URI: uri is: @{[$r->uri]}"); | ||
| 255 | my $urilang; | ||
| 256 | # Save uri in case we need it | ||
| 257 | $r->pnotes('POLYGLOT_URI' => $r->uri); | ||
| 258 | my @uri = split(/\/+/, $r->uri); | ||
| 259 | # leading slash = leading "" | ||
| 260 | shift @uri; | ||
| 261 | # check to see if first segment is std lang tag with optional sub lang | ||
| 262 | $DEBUG && $r->log_error("URI: checking URL: tag $uri[0]"); | ||
| 263 | if($uri[0] =~ /^([a-z]{2})(?:-([a-z]{2,3}))?$/ | ||
| 264 | and code2language($1) and | ||
| 265 | (!$2 || | ||
| 266 | code2country($2, length($2) == 2 | ||
| 267 | ? LOCALE_CODE_ALPHA_2 : LOCALE_CODE_ALPHA_3 ))){ | ||
| 268 | $DEBUG && $r->log_error("$uri[0] is a valid lang tag!"); | ||
| 269 | $urilang = $2?join('-', lc($1), lc($2)):$1; | ||
| 270 | # Stash language preference in ENV and pnotes | ||
| 271 | $ENV{$POLYGLOT_LANG} = $urilang || lc $r->dir_config($PolyglotDefaultLang); | ||
| 272 | $r->pnotes('POLYGLOT_URILANG', $urilang); | ||
| 273 | $r->pnotes('POLYGLOT_LANG', $urilang); | ||
| 274 | # 86 the language tag, rebuild the URI | ||
| 275 | shift @uri; | ||
| 276 | $r->uri(join('/', "", @uri)); | ||
| 277 | $DEBUG && $r->log_error("new path is " . $r->uri); | ||
| 278 | }else{ | ||
| 279 | $DEBUG && $r->log_error("Path unchanged: " . $r->uri); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | # =item _get_env_pref | ||
| 284 | |||
| 285 | # This takes a hashref as its argument (usually the hash that I<get_client_pref> populated), | ||
| 286 | # and alters it to reflect the preference stashed in the environment. | ||
| 287 | |||
| 288 | # =cut | ||
| 289 | |||
| 290 | sub _get_env_pref{ | ||
| 291 | my $self = shift; | ||
| 292 | my $Accept = shift||{}; | ||
| 293 | |||
| 294 | # Check the environment to see if there is a favoured language | ||
| 295 | if($self->{default_uri_lang}){ | ||
| 296 | $$Accept{$self->{default_uri_lang}}{q} = 100; # trump everything - top the list | ||
| 297 | $self->{r}->pnotes('POLYGLOT_URILANG', $self->{default_uri_lang}); | ||
| 298 | return $Accept; | ||
| 299 | } | ||
| 300 | |||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | =head1 SEE ALSO | ||
| 306 | |||
| 307 | L<HTML::Mason::Resolver::File|HTML::Mason::Resolver::File> | ||
| 308 | |||
| 309 | =head1 CREDIT | ||
| 310 | |||
| 311 | Thanks to Dorian Taylor <dorian@foobarsystems.com> for his nice Accept-Language code. | ||
| 312 | |||
| 313 | =head1 AUTHOR | ||
| 314 | |||
| 315 | Benjamin H Kram <ben@base16consulting.com> | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | __END__ |