Unverified Commit bd3e2d4a authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

refactor(frontend): language settings improved (#1362)

issue #1357
parent 4e96ecdf
......@@ -1290,19 +1290,53 @@ any '/user_settings' => sub {
user_settings($c);
};
sub _translations($c) {
my %lang_name=(
ar => 'Arab'
,en => 'English'
,ca => 'Catalan'
,es => 'Spanish'
,gl => 'Galician'
,fr => 'French'
,de => 'German'
,hi => 'Hindi'
,id => 'Indonesian'
,it => 'Italian'
,fa => 'Persian'
,pt => 'Portuguese'
,pt => 'Portuguese'
,tr => 'Turkish'
,ja => 'Japanese'
,'cat@valencia' => 'Valencian'
,vi => 'Vietnamese'
);
$c->stash(lang_name => \%lang_name);
my $language = $USER->language;
if (!$language) {
for my $detected_lang (_detect_languages($c)) {
if ( $lang_name{$detected_lang}) {
$language = $detected_lang;
last;
}
}
}
$c->stash( language => ( $language or 'en') );
}
sub user_settings {
my $c = shift;
my $changed_lang;
my $changed_pass;
if ($c->req->method('POST')) {
$USER->language($c->param('tongue'));
$changed_lang = $c->param('tongue');
if ($c->param('language')) {
$USER->language($c->param('language'));
$changed_lang = $c->param('language');
Ravada::Request->post_login(
user => $USER->name
, locale => $changed_lang);
_logged_in($c);
}
$c->param('tongue' => $USER->language);
_translations($c);
my @errors;
if ($c->param('button_click')) {
my $auth_ok;
......@@ -1463,6 +1497,18 @@ sub _logged_in {
return $USER;
}
sub _detect_languages($c) {
my @languages = I18N::LangTags::implicate_supers(
I18N::LangTags::Detect::detect()
);
my $header = ( $c->req->headers->header('accept-language') or '');
my @languages2 = map {my $lang = $_ ; $lang =~ s/^(.*?)[;-].*/$1/; $lang } split /,/,$header;
my @languages_browser = map {my $lang = $_ ;$lang =~ s/;.*//; $lang } split /,/,$header;
return (@languages_browser, @languages, @languages2);
}
sub login {
my $c = shift;
......@@ -1487,16 +1533,9 @@ sub login {
$c->session('login' => $login);
my $expiration = $SESSION_TIMEOUT;
$expiration = $SESSION_TIMEOUT_ADMIN if $auth_ok->is_admin;
my @languages = I18N::LangTags::implicate_supers(
I18N::LangTags::Detect::detect()
);
my $header = ( $c->req->headers->header('accept-language') or '');
my @languages2 = map {my $lang = $_ ; $lang =~ s/^(.*?)[;-].*/$1/; $lang } split /,/,$header;
my @languages_browser = map {my $lang = $_ ;$lang =~ s/;.*//; $lang } split /,/,$header;
Ravada::Request->post_login(
user => $auth_ok->name
, locale => [@languages_browser, @languages, @languages2]
, locale => [_detect_languages($c)]
);
$auth_ok = Ravada::Auth::SQL->new(name => $auth_ok->name);
......
......@@ -16,25 +16,18 @@
<div>
<div class="card-body">
<h3 class="mb-3"><%=l 'Setting your default language' %></h3>
<form method='post' enctype="multipart/form-data">
<%=l 'Language:' %> <select onchange="this.form.submit();" id="tongue" name="tongue" >
<option <%= $c->param("tongue") eq 'tr' ? "selected" : "" %> value="tr" ><%=l 'Turkish' %></option>
<option <%= $c->param("tongue") eq 'ar' ? "selected" : "" %> value="ar" ><%=l 'Arab' %></option>
<option <%= $c->param("tongue") eq 'en' ? "selected" : "" %> value="en" ><%=l 'English' %></option>
<option <%= $c->param("tongue") eq 'ca' ? "selected" : "" %> value="ca" ><%=l 'Catalan' %></option>
<option <%= $c->param("tongue") eq 'es' ? "selected" : "" %> value="es" ><%=l 'Spanish' %></option>
<option <%= $c->param("tongue") eq 'gl' ? "selected" : "" %> value="gl" ><%=l 'Galician' %></option>
<option <%= $c->param("tongue") eq 'fr' ? "selected" : "" %> value="fr" ><%=l 'French' %></option>
<option <%= $c->param("tongue") eq 'de' ? "selected" : "" %> value="de" ><%=l 'German' %></option>
<option <%= $c->param("tongue") eq 'hi' ? "selected" : "" %> value="hi" ><%=l 'Hindi' %></option>
<option <%= $c->param("tongue") eq 'id' ? "selected" : "" %> value="id" ><%=l 'Indonesian' %></option>
<option <%= $c->param("tongue") eq 'it' ? "selected" : "" %> value="it" ><%=l 'Italian' %></option>
<option <%= $c->param("tongue") eq 'fa' ? "selected" : "" %> value="fa" ><%=l 'Persian' %></option>
<option <%= $c->param("tongue") eq 'pt' ? "selected" : "" %> value="pt" ><%=l 'Portuguese' %></option>
<option <%= $c->param("tongue") eq 'ja' ? "selected" : "" %> value="ja" ><%=l 'Japanese' %></option>
<option <%= $c->param("tongue") eq 'vi' ? "selected" : "" %> value="vi" ><%=l 'Vietnamese' %></option>
<option <%= $c->param("tongue") eq 'cat@valencia' ? "selected" : "" %> value="cat@valencia" ><%=l 'Valencian' %></option>
</select>
<form>
<%=l 'Language:' %> <select onchange="this.form.submit();" id="language" name="language" >
% for my $code (sort { $i18n->localize($lang_name->{$a}) cmp $i18n->localize($lang_name->{$b}) } keys %$lang_name) {
<option <%= $language eq $code ? "selected" : "" %>
value="<%= $code %>">
<%=l $lang_name->{$code} %>
</option>
% }
</select>
% if ($changed_lang) {
<font color="green"> <%=l 'Your language has been changed successfully' %></font>
% }
......@@ -47,7 +40,7 @@
<div class="from-group row">
<label for="password" class="col-xl-3 col-form-label"><%=l 'Current Password:' %></label><input type='password' name='current_password' id='current_password'>
</div>
<div class="from-group row">
<div class="from-group row">
<label for="password" class="col-xl-3 col-form-label"><%=l 'New Password:' %></label><input type= 'password' name= 'password' id='password'>
</div>
<div class="from-group row">
......
Supports Markdown
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