Skip to content

Instantly share code, notes, and snippets.

@shoorick shoorick/3_td.pl
Created Dec 13, 2010

Embed
What would you like to do?
URL-driven language choosing example with Mojolicious::Lite and Mojolicious::Plugin::Textdomain
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
$self->render(
'template' => 'index',
'count' => int rand 33,
);
};
get '/:language/:page' => sub {
my $self = shift;
# Choose language
my $language = $self->param('language');
my $default_language = 'en'; # TODO Get it from config
my @available_languages = qw( de en ru ); # TODO Search in locale dir
# if selected language is unavailable
$language = $self->detect_language( \@available_languages, $default_language )
unless grep { $language eq $_ } @available_languages;
$self->set_language( $language );
$self->stash->{'language'} = $language;
$self->render(
'template' => 'index',
'count' => int rand 33,
);
};
plugin 'charset' => { 'charset' => 'utf8' };
plugin 'textdomain' => {
'domain' => 'example',
'search_dirs' => [ 'locale' ],
};
app->start;
__DATA__
@@ index.html.ep
% layout 'page';
%= __ 'It works?';
<p>
%= __nx '{count} apple was found', '{count} apples were found', $count, 'count' => $count;
</p>
<a href="<%= url_for( "/$language/second" ) %>"><%= __ 'second page' %></a>
@@ layouts/page.html.ep
<!doctype html><html>
<head><title>i18n <%= __ 'Example' %></title></head>
<body>
<h1><%= param 'page' %></h1>
<%== content %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.