Catalyst-Plugin-I18N-PathPrefix
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/I18N/PathPrefix.pm view on Meta::CPAN
overriding it can be a significant performance saving. YMMV.
=cut
sub uri_for_in_language
{
my ($c, $language_code, @uri_for_args) = (shift, @_);
$language_code = lc $language_code;
my $scope_guard = $c->_set_language_prefix_temporarily($language_code);
return $c->uri_for(@uri_for_args);
}
=head2 switch_language
$c->switch_language($language_code)
Returns: N/A
Changes C<< $c->req->base >> to end with C<$language_code> and calls C<<
$c->set_languages_from_language_prefix >> with C<$language_code>.
Useful if you want to switch the language later in the request processing (eg.
from a request parameter, from the session or from the user object).
=cut
sub switch_language
{
my ($c, $language_code) = (shift, @_);
$language_code = lc $language_code;
$c->_set_language_prefix($language_code);
$c->set_languages_from_language_prefix($language_code);
}
=head2 language_switch_options
$c->language_switch_options()
Returns: C<< { $language_code => { name => $language_name, uri => $uri }, ... } >>
Returns a data structure that contains all the necessary data (language code,
name, URL of the same page) for displaying a language switch widget on the
page.
The data structure is a hashref with one key for each valid language code (see
the L</valid_languages> config option) (in all-lowercase format) and the value
is a hashref that contains the following key-value pairs:
=over
=item name
The localized (translated) name of the language. (The actual msgid used in C<<
$c->loc() >> is the English name of the language, returned by
L<I18N::LangTags::List/name>.)
=item url
The URL of the equivalent of the current page in that language (ie. the
language prefix replaced).
=back
You can find an example TT2 HTML template for the language switch included in
the distribution.
=cut
sub language_switch_options
{
my ($c) = (shift, @_);
return {
map {
$_ => {
name => $c->loc(I18N::LangTags::List::name($_)),
uri => $c->uri_for_in_language($_ => '/' . $c->req->path, $c->req->params),
}
} map { lc $_ }
@{ $c->config->{'Plugin::I18N::PathPrefix'}->{valid_languages} }
};
}
=begin internal
$c->_set_language_prefix($language_code)
Sets the language to C<$language_code>: Mangles C<< $c->req->uri >> and C<<
$c->req->base >>.
=end internal
=cut
sub _set_language_prefix
{
my ($c, $language_code) = (shift, @_);
if ($c->req->path !~
$c->config->{'Plugin::I18N::PathPrefix'}->{language_independent_paths}) {
my ($actual_base_path) = $c->req->base->path =~ m{ ^ / [^/]+ (.*) $ }x;
$c->req->base->path($language_code . $actual_base_path);
my ($actual_uri_path) = $c->req->uri->path =~ m{ ^ / [^/]+ (.*) $ }x;
$c->req->uri->path($language_code . $actual_uri_path);
$c->req->_clear_path;
}
}
=begin internal
( run in 1.798 second using v1.01-cache-2.11-cpan-97f6503c9c8 )