diff options
-rwxr-xr-x | lib/Travelynx.pm | 53 | ||||
-rw-r--r-- | lib/Travelynx/Controller/Account.pm | 29 | ||||
-rwxr-xr-x | lib/Travelynx/Model/Journeys.pm | 2 | ||||
-rw-r--r-- | lib/Travelynx/Model/Users.pm | 8 | ||||
-rw-r--r-- | templates/account.html.ep | 7 |
5 files changed, 76 insertions, 23 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 907ce0c..3b7c89c 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -162,29 +162,7 @@ sub startup { 'before_render' => sub { my ($self) = @_; - # TODO load languages from user profile, if set - - my @languages; - if ( $self->is_user_authenticated - and @{ $self->current_user->{languages} } ) - { - @languages = @{ $self->current_user->{languages} }; - } - elsif ( my $languages = $self->req->headers->accept_language ) { - for my $lang ( split( qr{ \s* , \s* }x, $languages ) ) { - if ( $lang =~ m{ ^ de }x ) { - push( @languages, 'de-DE' ); - } - elsif ( $lang =~ m{ ^ en }x ) { - push( @languages, 'en-GB' ); - } - } - } - - # de-DE is our fall-back language and thus always appended - $self->stash( loc_handle => - Travelynx::Helper::Locales->get_handle( @languages, 'de-DE' ) - ); + $self->stash( loc_handle => $self->loc_handle ); } ); @@ -443,6 +421,33 @@ sub startup { ); $self->helper( + loc_handle => sub { + my ($self) = @_; + + my @languages; + if ( $self->is_user_authenticated + and @{ $self->current_user->{languages} } ) + { + @languages = @{ $self->current_user->{languages} }; + } + elsif ( my $languages = $self->req->headers->accept_language ) { + for my $lang ( split( qr{ \s* , \s* }x, $languages ) ) { + if ( $lang =~ m{ ^ de }x ) { + push( @languages, 'en-GB' ); + } + elsif ( $lang =~ m{ ^ en }x ) { + push( @languages, 'en-GB' ); + } + } + } + + # de-DE is our fall-back language and thus always appended + return Travelynx::Helper::Locales->get_handle( @languages, + 'de-DE' ); + } + ); + + $self->helper( 'L' => sub { my ( $self, @args ) = @_; $self->stash('loc_handle')->maketext(@args); @@ -3105,6 +3110,7 @@ sub startup { $authed_r->get('/account/hooks')->to('account#webhook'); $authed_r->get('/account/traewelling')->to('traewelling#settings'); $authed_r->get('/account/insight')->to('account#insight'); + $authed_r->get('/account/language')->to('account#change_language'); $authed_r->get('/ajax/status_card.html')->to('traveling#status_card'); $authed_r->get( '/cancelled' => [ format => [ 'html', 'json' ] ] ) ->to( 'traveling#cancelled', format => undef ); @@ -3135,6 +3141,7 @@ sub startup { $authed_r->post('/account/hooks')->to('account#webhook'); $authed_r->post('/account/traewelling')->to('traewelling#settings'); $authed_r->post('/account/insight')->to('account#insight'); + $authed_r->post('/account/language')->to('account#change_language'); $authed_r->post('/account/select_backend')->to('account#change_backend'); $authed_r->post('/checkin/add')->to('traveling#add_intransit_form'); $authed_r->post('/journey/add')->to('traveling#add_journey_form'); diff --git a/lib/Travelynx/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm index bf1eac2..8121f0a 100644 --- a/lib/Travelynx/Controller/Account.pm +++ b/lib/Travelynx/Controller/Account.pm @@ -874,6 +874,35 @@ sub webhook { $self->render( 'webhooks', hook => $hook ); } +sub change_language { + my ($self) = @_; + + my $action = $self->req->param('action'); + my $language = $self->req->param('language'); + + if ( $action and $action eq 'save' ) { + if ( $self->validation->csrf_protect->has_error('csrf_token') ) { + $self->render( + 'bad_request', + csrf => 1, + status => 400 + ); + return; + } + $self->users->set_language( + uid => $self->current_user->{id}, + language => $language, + ); + $self->flash( success => 'language' ); + $self->redirect_to('account'); + } + else { + my @languages = @{ $self->current_user->{languages} }; + $self->param( language => $languages[0] // q{} ); + $self->render('language'); + } +} + sub change_mail { my ($self) = @_; diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm index b07511a..5e6195f 100755 --- a/lib/Travelynx/Model/Journeys.pm +++ b/lib/Travelynx/Model/Journeys.pm @@ -50,6 +50,8 @@ sub epoch_to_dt { ); } +# TODO turn into a travelynx helper called from templates so that +# loc_handle is available for localization sub min_to_human { my ( $self, $minutes ) = @_; diff --git a/lib/Travelynx/Model/Users.pm b/lib/Travelynx/Model/Users.pm index 367523d..3ef7f33 100644 --- a/lib/Travelynx/Model/Users.pm +++ b/lib/Travelynx/Model/Users.pm @@ -216,6 +216,14 @@ sub set_backend { ); } +sub set_language { + my ( $self, %opt ) = @_; + $opt{db} //= $self->{pg}->db; + + $opt{db} + ->update( 'users', { language => $opt{language} }, { id => $opt{uid} } ); +} + sub set_privacy { my ( $self, %opt ) = @_; my $db = $opt{db} // $self->{pg}->db; diff --git a/templates/account.html.ep b/templates/account.html.ep index 4c5add8..837f219 100644 --- a/templates/account.html.ep +++ b/templates/account.html.ep @@ -16,6 +16,9 @@ % elsif ($success eq 'password') { <span class="card-title">Passwort geändert</span> % } + % elsif ($success eq 'language') { + <span class="card-title">Sprache geändert</span> + % } % elsif ($success eq 'privacy') { <span class="card-title">Einstellungen zu öffentlichen Account-Daten geändert</span> % } @@ -61,6 +64,10 @@ <td><a href="/account/password"><i class="material-icons">edit</i></a></td> </tr> <tr> + <th scope="row"><%= L('account.language') %></th> + <td><a href="/account/language"><i class="material-icons">edit</i></a><%= $acc->{languages}[0] // q{} %></td> + </tr> + <tr> <th scope="row"><%= L('account.connections') %></th> <td> <a href="/account/insight"><i class="material-icons">edit</i></a> |