<!-- Turn on template-toolkit debugging, which reports on undefined blocks, macros, etc. -->
<debug_template>false</debug_template>
+ <!--
+ Supported locales. Locales with no message catalog will use the native template strings.
+ All locales will fall back to native strings when a given string is not in the catalog
+ -->
+ <locales>
+ <en_US/>
+ <en_CA>/openils/var/data/locale/messages.en_CA.po</en_CA>
+ <fr_CA>/openils/var/data/locale/messages.fr_CA.po</fr_CA>
+ </locales>
+
<!-- Where templates can be found. Paths will be checked in the order entered here.
It's possible to override individual or sets of templates by putting them into
a path in front of the default template path -->
my $web_config;
my $web_config_file;
my $web_config_edit_time;
+my %lh_cache; # locale handlers
sub import {
my $self = shift;
DEBUG => $ctx->{debug_template}
});
- unless($tt->process($template, {ctx => $ctx})) {
+ unless($tt->process($template, {ctx => $ctx, l => set_text_handler($ctx, $r)})) {
$r->log->warn('Template error: ' . $tt->error);
return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
}
return Apache2::Const::OK;
}
+sub set_text_handler {
+ my $ctx = shift;
+ my $r = shift;
+
+ my $locale = $ctx->{locale};
+ $locale =~ s/-/_/g;
+
+ $r->log->info("messages locale = $locale");
+
+ unless($lh_cache{$locale}) {
+ $r->log->info("Unsupported locale: $locale");
+ $lh_cache{$locale} = $lh_cache{'en_US'};
+ }
+
+ return sub { return $lh_cache{$locale}->maketext(@_); };
+}
+
+
sub run_context_loader {
my $r = shift;
$ctx->{base_url} = $cgi->url(-base => 1);
$ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
$ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
+
$ctx->{locale} =
$cgi->cookie(OILS_HTTP_COOKIE_LOCALE) ||
parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en-US';
- $r->log->debug('skin = ' . $ctx->{skin} . ' : theme = ' .
- $ctx->{theme} . ' : locale = ' . $ctx->{locale});
my $mprefix = $ctx->{media_prefix};
if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
$ctx->{media_prefix} = ($cgi->https) ? "https://$mprefix" : "http://$mprefix";
}
-
return $ctx;
}
}
}
+# Create an I18N sub-module for each supported locale
+# Each module creates its own MakeText lexicon by parsing .po/.mo files
+sub load_locale_handlers {
+ my $ctx = shift;
+ my $locales = $ctx->{locales};
+
+ for my $lang (keys %$locales) {
+ my $messages = $locales->{$lang};
+ $messages = '' if ref $messages; # empty {}
+
+ # TODO Can we do this without eval?
+ my $eval = <<EVAL;
+ package OpenILS::WWW::EGWeb::I18N::$lang;
+ use base 'OpenILS::WWW::EGWeb::I18N';
+ if(\$messages) {
+ use Locale::Maketext::Lexicon::Gettext;
+ if(open F, '$messages') {
+ our %Lexicon = (%Lexicon, %{ Locale::Maketext::Lexicon::Gettext->parse(<F>) });
+ close F;
+ } else {
+ warn "unable to open messages file: $messages";
+ }
+ }
+EVAL
+ eval $eval;
+ warn "$@\n" if $@; # TODO better logging
+ $lh_cache{$lang} = "OpenILS::WWW::EGWeb::I18N::$lang"->new;
+ }
+}
+
+
+
sub parse_config {
my $cfg_file = shift;
my $data = XML::Simple->new->XMLin($cfg_file);
$ctx->{debug_template} = ( ($data->{debug_template}||'') =~ /true/io) ? 1 : 0;
$ctx->{default_template_extension} = $data->{default_template_extension} || 'tt2';
$ctx->{web_dir} = $data->{web_dir};
+ $ctx->{locales} = $data->{locales};
+ load_locale_handlers($ctx);
my $tpaths = $data->{template_paths}->{path};
$tpaths = [$tpaths] unless ref $tpaths;
return bless(\%args, $class);
}
+# base class for all supported locales
+package OpenILS::WWW::EGWeb::I18N;
+use base 'Locale::Maketext';
+our %Lexicon = (_AUTO => 1);
1;