Created
December 13, 2010 15:44
-
-
Save shoorick/739116 to your computer and use it in GitHub Desktop.
URL-driven language choosing example with Mojolicious::Lite and Mojolicious::Plugin::Textdomain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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