Commit cafcd6d9 authored by Luc Didry's avatar Luc Didry

Fixed #3 : Internationalization

I had to transform Lstu into a full Mojolicious application to make I18N
working.
parent 937b8dbb
This diff is collapsed.
requires 'Mojolicious';
requires 'ORLite';
requires 'Data::Validate::URI';
requires 'Mojolicious::Plugin::I18N';
......@@ -264,6 +264,16 @@ DISTRIBUTIONS
ojo undef
requirements:
ExtUtils::MakeMaker 0
Mojolicious-Plugin-I18N-1.0
pathname: S/SH/SHARIFULN/Mojolicious-Plugin-I18N-1.0.tar.gz
provides:
Mojolicious::Plugin::I18N 1.0
requirements:
I18N::LangTags 0.35
Module::Build 0.36
Mojolicious 3
Test::More 0
perl 5.010001
Net-Domain-TLD-1.70
pathname: A/AL/ALEXP/Net-Domain-TLD-1.70.tar.gz
provides:
......
package Lstu;
use Mojo::Base 'Mojolicious';
use LstuModel;
use Data::Validate::URI qw(is_uri);
# This method will run once at server start
sub startup {
my $self = shift;
my $config = $self->plugin('Config');
# Default values
$config->{provisionning} = 100 unless (defined($config->{provisionning}));
$config->{provis_step} = 5 unless (defined($config->{provis_step}));
$config->{length} = 8 unless (defined($config->{length}));
$config->{secret} = 'hfudsifdsih' unless (defined($config->{secret}));
$self->plugin('I18N');
$self->secret($config->{secret});
$self->helper(
provisionning => sub {
my $c = shift;
# Create some short patterns for provisionning
if (LstuModel::Lstu->count('WHERE url IS NULL') < $c->config->{provisionning}) {
for (my $i = 0; $i < $c->config->{provis_step}; $i++) {
my $short;
do {
$short= $c->shortener($c->config->{length});
} while (LstuModel::Lstu->count('WHERE short = ?', $short));
LstuModel::Lstu->create(
short => $short
);
}
}
}
);
$self->helper(
shortener => sub {
my $c = shift;
my $length = shift;
my @chars = ('a'..'z','A'..'Z','0'..'9');
my $result = '';
foreach (1..$length) {
$result .= $chars[rand scalar(@chars)];
}
return $result;
}
);
# For the first launch (after, this isn't really useful)
$self->provisionning();
# Router
my $r = $self->routes;
# Normal route to controller
$r->get('/' => sub {
my $c = shift;
$c->render(template => 'index');
# Check provisionning
$c->on(finish => sub {
shift->provisionning();
});
})->name('index');
$r->get('/:short' => sub {
my $c = shift;
my $short = $c->param('short');
my @urls = LstuModel::Lstu->select('WHERE short = ?', $short);
if (scalar(@urls)) {
$c->res->code(301);
$c->redirect_to($urls[0]->url);
# Update counter and check provisionning
$c->on(finish => sub {
my $counter = $urls[0]->counter + 1;
$urls[0]->update (counter => $counter);
shift->provisionning();
});
} else {
$c->flash(
msg => $c->l('url_not_found', $c->url_for('/')->to_abs.$short)
);
$c->redirect_to('/');
}
})->name('short');
my $add = sub {
my $c = shift;
my $url = $c->param('lsturl');
if (is_uri($url)) {
my $short;
my @keys = $c->param;
my @params;
foreach my $key (sort @keys) {
push @params, $key.'='.$c->param($key) unless ($key eq 'lsturl');
}
$url .= '?'.join('&', @params) if (scalar(@params));
my @records = LstuModel::Lstu->select('WHERE url = ?', $url);
if (scalar(@records)) {
# Already got this URL
$c->flash(
short => $records[0]->short,
url => $url
);
} else {
@records = LstuModel::Lstu->select('WHERE url IS NULL LIMIT 1');
if (scalar(@records)) {
$records[0]->update(
url => $url,
counter => 0,
timestamp => time()
);
$c->flash(
short => $records[0]->short,
url => $url
);
} else {
# Houston, we have a problem
$c->flash(
msg => $c->l('no_more_short', $c->config->{contact}, $url)
);
}
}
} else {
$c->app->log->debug($c->l('no_valid_url', $url));
$c->flash(
msg => $c->l('no_valid_url', $url)
);
}
$c->redirect_to('/');
# Check provisionning
$c->on(finish => sub {
shift->provisionning();
});
};
$r->post('/a/' => $add)->name('add');
$r->get('/a/*lsturl' => $add)->name('addurl');
}
1;
package Lstu::I18N::en;
use Mojo::Base 'Lstu::I18N';
our %Lexicon = (
'about' => 'About',
'go' => 'Go !',
'license' => 'License:',
'no_more_short' => 'No shortened URL available. Please retry or contact the administrator at [_1]. Your URL to shorten: [_2]',
'no_valid_url' => '[_1] is not a valid URL.',
'url_not_found' => 'The shortened URL [_1] doesn\'t exist.',
'url_to_shorten' => 'URL to shorten',
);
1;
package Lstu::I18N::fr;
use Mojo::Base 'Lstu::I18N';
our %Lexicon = (
'about' => 'À propos',
'go' => 'Allons-y !',
'license' => 'Licence : ',
'no_more_short' => 'Il n\'y a plus d\'URL raccourcie disponible. Veuillez réessayer ou contactez l\'administrateur sur [_1]. Rappel de l\'URL à raccourcir : [_2]',
'no_valid_url' => '[_1] n\'est pas une URL valide.',
'url_not_found' => 'L\'URL raccourcie [_1] n\'existe pas.',
'url_to_shorten' => 'URL à raccourcir',
);
1;
package LstuModel;
# Create database
use ORLite {
file => 'lstu.db',
unicode => 1,
create => sub {
my $dbh = shift;
$dbh->do(
'CREATE TABLE lstu (
short TEXT PRIMARY KEY,
url TEXT,
counter INTEGER,
timestamp INTEGER)'
);
return 1;
}
};
1;
This diff is collapsed.
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
# Start command line interface for application
require Mojolicious::Commands;
Mojolicious::Commands->start_app('Lstu');
% # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab:
% layout 'default';
% title 'Lstu';
<h1>Let's Shorten That Url</h1>
<p>
<small>&copy; 2013 <%= link_to 'http://www.fiat-tux.fr' => begin %>Luc Didry<% end %>
<%=l 'license' %> <%= link_to 'http://www.wtfpl.net/' => begin %>WTFPL<% end %>
<%= link_to 'https://github.com/ldidry/lstu' => begin %><%=l 'about' %><% end %>
</small>
</p>
<form class="form-inline" role="form" method="POST" action="<%== url_for 'add' %>">
<div class="form-group">
<label class="sr-only" for="lsturl"><%=l 'url_to_shorten' %></label>
<input type="url" name="lsturl" id="lsturl" class="form-control" placeholder="<%=l 'url_to_shorten' %>" required></input>
</div>
<%= submit_button l('go'), class => 'btn btn-default btn-primary' %>
</form>
% if (defined(flash('short')) && defined(flash('url'))) {
<h3 class="alert alert-success">
<%= link_to flash('url') => begin %><%== flash('url') %><%= end %></a> =&gt;
<%= link_to url_for('/')->to_abs.flash('short') => begin %><%= url_for('/')->to_abs.flash('short') %><%= end %>
</h3>
% }
% if (defined(flash('msg'))) {
<h3 class="alert alert-danger"><%== flash('msg') %><h3>
% }
% # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
%= stylesheet 'css/bootstrap.min.css', media => 'screen'
%= stylesheet begin
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}
.container {
padding: 15px;
margin: 0 auto;
}
% end
</head>
<body>
<div class="container">
<%= content %>
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment