Skip to content

Commit

Permalink
Merge 7487642 into a877f7f
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasfranck committed Jun 29, 2018
2 parents a877f7f + 7487642 commit 3f4ec5d
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 42 deletions.
11 changes: 5 additions & 6 deletions lib/LibreCat/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ get '/login' => sub {
error_message => params->{error_message} || '',
login => params->{login} || '',
return_url => params->{return_url} || '',
lang => session->{lang} || h->config->{default_lang}
lang => h->locale()
};
};

Expand All @@ -233,10 +233,8 @@ post '/login' => sub {
$return_url =~ s{^[a-zA-Z:]+(\/\/)[^\/]+}{};

if ($user) {

h->login_user($user);
redirect uri_for($return_url);

}
else {
forward '/login', {error_message => 'Wrong username or password!'},
Expand All @@ -259,13 +257,14 @@ any '/logout' => sub {

=head2 GET /set_language
Route to call when changing language in session
Route to call when changing language
=cut

get '/set_language' => sub {
my $referer = request->{referer} // '/?';
session lang => params->{lang};
my $referer = request->referer // '/?';
my $lang = param('lang');
h->set_locale( $lang ) if h->locale_exists( $lang );
$referer =~ s/lang=\w{2}\&*//g;
redirect $referer;
};
Expand Down
9 changes: 5 additions & 4 deletions lib/LibreCat/App/Catalogue/Route/person.pm
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ User can choose default language for the librecat backend

get '/set_language' => sub {

my $person = h->get_person(session('user_id'));
my $lang = params->{lang};
if ($lang eq "en" or $lang eq "de") {
my $h = h();
my $person = $h->get_person(session('user_id'));
my $lang = param('lang');
if ( $h->locale_exists( $lang ) ) {
$person->{lang} = $lang;
user->add($person);
session lang => $lang;
$h->set_locale( $lang );
}

redirect uri_for('/librecat');
Expand Down
4 changes: 2 additions & 2 deletions lib/LibreCat/App/Catalogue/Route/publication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ Some fields are pre-filled.
}
}

if (params->{lang}) {
$data->{lang} = params->{lang};
if ( h->locale_exists( param('lang') ) ) {
$data->{lang} = param('lang');
}

my $templatepath = "backend/forms";
Expand Down
73 changes: 69 additions & 4 deletions lib/LibreCat/App/Helper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Catmandu qw(export_to_string);
use Catmandu::Util qw(:io :is :array :hash :human trim);
use Catmandu::Fix qw(expand);
use Catmandu::Store::DBI;
use Dancer qw(:syntax params request session vars);
use Dancer qw(:syntax params request session vars cookie);
use Dancer::FileUtils qw(path);
use File::Basename;
use POSIX qw(strftime);
Expand All @@ -19,6 +19,7 @@ use NetAddr::IP::Lite;
use URI::Escape qw(uri_escape_utf8);
use Role::Tiny ();
use Moo;
use Clone qw(clone);

sub BUILD {
my ($self) = @_;
Expand Down Expand Up @@ -429,6 +430,13 @@ sub uri_base {
sub uri_for {
my ($self, $path, $params) = @_;

if ( $self->show_locale ) {

$params = clone($params);
$params->{lang} = $self->locale();

}

my $uri = $self->uri_base();

$uri .= $path if $path;
Expand Down Expand Up @@ -492,9 +500,44 @@ sub get_access_store {
$pkg->new(%$access_opts);
}

# TODO don't store in session, make it a param
sub show_locale {
$_[0]->config->{i18n}->{show_locale};
}

sub locale {
session('lang') // $_[0]->config->{default_lang};
cookie('lang') // $_[0]->default_locale();
}

sub set_locale {
cookie( 'lang', $_[1] );
}

sub available_locales {
state $locales = [
sort
grep { index($_,"_") != 0 }
keys %{ $_[0]->config->{i18n}->{lexicon} }
];
}
#everything that starts with an underscore is a lexicon option, not a language
sub locale_exists {
is_string( $_[1] ) &&
index( $_[1], "_" ) != 0 &&
exists( $_[0]->config->{i18n}->{lexicon}->{ $_[1] } );
}

sub default_locale {
state $dl = do {
is_string( $_[0]->config->{default_lang} ) or die( "default_lang is not set in config" );
$_[0]->config->{default_lang};
};
}

sub locale_long {
my ( $self, $locale ) = @_;
is_string( $locale ) &&
is_string( $self->config->{i18n}->{locale_long}->{$locale} ) ?
$self->config->{i18n}->{locale_long}->{$locale} : $locale;
}

sub localize {
Expand All @@ -504,6 +547,14 @@ sub localize {
my $i18n = $locales->{$loc} //= LibreCat::I18N->new(locale => $loc);
$i18n->localize($str);
}
sub uri_for_locale {
my ( $self, $locale ) = @_;
my $request = request();
my $path_info = $request->path_info();
my %params = (params("query"),params("body"));
$params{lang} = $locale;
$request->uri_for( $path_info, \%params );
}

*loc = \&localize;

Expand Down Expand Up @@ -563,7 +614,7 @@ LibreCat::App::Helper - a helper package with utility functions
my $h = LibreCat::App::Helper::Helpers->new;

use Catmandu::Sane;
use Dancer qw(:syntax hook);
use Dancer qw(:syntax hook param request);
use Dancer::Plugin;

register h => sub {$h};
Expand All @@ -572,6 +623,20 @@ hook before_template => sub {
$_[0]->{h} = $h;
$_[0]->{uri_base} = $h->uri_base();

};
hook before => sub {

#set lang when sent
{
my $lang = param("lang");
if ( request->is_get() && $h->locale_exists( $lang ) ) {

$h->set_locale( $lang );

}

}

};

register_plugin;
Expand Down
2 changes: 1 addition & 1 deletion lib/LibreCat/App/Search/Route/publication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ get '/embed' => sub {

$hits->{embed} = 1;

my $lang = $p->{lang} || session->{lang} || h->config->{default_lang};
my $lang = h->locale_exists( $p->{lang} ) ? $p->{lang} : h->locale();
$hits->{lang} = $lang;

if (params->{fmt} && params->{fmt} eq 'js') {
Expand Down
21 changes: 21 additions & 0 deletions t/LibreCat/App/Helper.t
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,25 @@ is h->uri_for_file(123, 456, 'test.pdf'),

ok h->can('my_helper'), 'load helpers';

is_deeply h->available_locales(), [qw(de en)], "available locales";

h->config->{i18n}->{locale_long} = {};
is h->locale_long("en"), "en";
is h->locale_long("de"), "de";

h->config->{i18n}->{locale_long} = { en => "English", de => "German" };
is h->locale_long("en"), "English";
is h->locale_long("de"), "German";

is h->default_locale(), "en";

ok h->locale_exists("en");
ok !(h->locale_exists("abc"));

h->config->{i18n}->{show_locale} = 1;
is h->uri_for("/librecat",{ a => "a" }), "http://localhost:5001/librecat?a=a&lang=en";

h->config->{i18n}->{show_locale} = 0;
is h->uri_for("/librecat",{ a => "a" }), "http://localhost:5001/librecat?a=a";

done_testing;
4 changes: 2 additions & 2 deletions views/admin/account.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = request.params.hash %]
[% qp.delete('splat') %]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% backend = request.path_info.match('librecat') ? 1 : 0 %]

[% INCLUDE header.tt %]
Expand Down Expand Up @@ -71,4 +71,4 @@

</div><!-- outer row -->
<!-- END admin/account.tt -->
[% INCLUDE footer.tt %]
[% INCLUDE footer.tt %]
4 changes: 2 additions & 2 deletions views/admin/generator/master_account.tt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]
[% backend = request.path_info.match('librecat') ? 1 : 0 %]

Expand Down Expand Up @@ -163,4 +163,4 @@ $('.del_del').click(function(){

<!-- END admin/generator/master_account.tt -->

[% INCLUDE footer.tt %]
[% INCLUDE footer.tt %]
2 changes: 1 addition & 1 deletion views/admin/generator/master_project.tt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]
[% backend = request.path_info.match('librecat') ? 1 : 0 %]

Expand Down
2 changes: 1 addition & 1 deletion views/admin/generator/master_research_group.tt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]
[% backend = request.path_info.match('librecat') ? 1 : 0 %]

Expand Down
2 changes: 1 addition & 1 deletion views/admin/project.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = request.params.hash %]
[% qp.delete('splat') %]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]
[% backend = request.path_info.match('librecat') ? 1 : 0 %]

Expand Down
2 changes: 1 addition & 1 deletion views/admin/research_group.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = request.params.hash %]
[% qp.delete('splat') %]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]
[% backend = request.path_info.match('librecat') ? 1 : 0 %]

Expand Down
2 changes: 1 addition & 1 deletion views/backend/add_new.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[% backend = request.path_info.match('librecat') ? 1 : 0 %]
[% lang = session.lang %]
[% lang = h.locale() %]

[% INCLUDE header.tt %]

Expand Down
2 changes: 1 addition & 1 deletion views/backend/generator/master.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% backend = request.path_info.match('librecat') ? 1 : 0 -%]
[% thisPerson = h.get_person(session.user_id) -%]
[% lang = session.lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang.forms -%]

[% INCLUDE header.tt %]
Expand Down
4 changes: 2 additions & 2 deletions views/embed/iframe.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = request.params.hash -%]
[% qp.delete('splat') -%]
[% lang = qp.lang ? qp.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% style = qp.style ? qp.style : h.config.citation.csl.default_style -%]
<!DOCTYPE html>
<html>
Expand All @@ -23,4 +23,4 @@
</div>
</div>
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions views/embed/javascript.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- BEGIN embed/javascript.tt -->
[% qp = request.params.hash -%]
[% lang = qp.lang ? qp.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
document.write ('<ul[% IF qp.listyle %] style="[% qp.listyle %]"[% END %]>') ;
[%- style = qp.style %]
[%- FOREACH entry IN hits %]
Expand All @@ -17,4 +17,4 @@ document.write('[% PROCESS citation.tt entry=entry FILTER replace("'", "\\'") %]
document.write('</li>');
[%- END %]
document.write ('</ul>');
<!-- END embed/javscript.tt -->
<!-- END embed/javscript.tt -->
4 changes: 2 additions & 2 deletions views/filters.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = {} -%]
[% qp = h.extract_params -%]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]

<!-- BEGIN filters.tt -->
[%- USE JSON.Escape %]
Expand Down Expand Up @@ -240,4 +240,4 @@ $('button.collapse').click(function(e){
});
</script>

<!-- END filters.tt -->
<!-- END filters.tt -->
2 changes: 1 addition & 1 deletion views/header.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html>
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]
[% path_info = request.path_info -%]

Expand Down
4 changes: 2 additions & 2 deletions views/home.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[% backend = request.path_info.match('librecat') ? 1 : 0 -%]
[% thisPerson = backend ? h.get_person(session.user_id) : h.get_person(id) -%]
[% menu = "" -%]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
[% lf = h.config.locale.$lang -%]

[% INCLUDE header.tt %]
Expand Down Expand Up @@ -179,4 +179,4 @@
<!-- END home.tt -->

[% INCLUDE mark_all_js.tt %]
[% INCLUDE footer.tt %]
[% INCLUDE footer.tt %]
4 changes: 2 additions & 2 deletions views/marked/filters.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = {} -%]
[% qp = h.extract_params -%]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]

<!-- BEGIN marked/filters.tt -->
[%- USE JSON.Escape %]
Expand Down Expand Up @@ -132,4 +132,4 @@ $('button.collapse').click(function(e){
});
</script>

<!-- END marked/filters.tt -->
<!-- END marked/filters.tt -->
2 changes: 1 addition & 1 deletion views/marked/list.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% qp = request.params.hash %]
[% qp.delete('splat') %]
[% lang = session.lang ? session.lang : h.config.default_lang %]
[% lang = h.locale() %]
[%- style = qp.style ? qp.style : h.config.citation.csl.default_style %]
[% PROCESS header.tt %]

Expand Down
2 changes: 1 addition & 1 deletion views/project/record.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[%- PROCESS header.tt %]
[% lang = session.lang ? session.lang : h.config.default_lang -%]
[% lang = h.locale() -%]
<!-- BEGIN project/record.tt -->
<div class="row" itemscope itemtype="http://schema.org/webpage" itemid="[% uri_base %]/project/[% _id %]"><!-- outer border -->
<div class="col-md-11 col-sm-12">
Expand Down

0 comments on commit 3f4ec5d

Please sign in to comment.