From 731b789855914cb94ec091604e32aa68a678404a Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Fri, 18 Jul 2025 15:33:42 +0200 Subject: Localization with Locale::Maketext WiP, no suitable foundation for merge requests yet. Still todo: * override Accept-Language header via account settings * Adjust all the templates and frontend javascript Related to #223 --- cpanfile | 2 ++ lib/Travelynx.pm | 33 +++++++++++++++++++++++ lib/Travelynx/Helper/Locales.pm | 22 ++++++++++++++++ share/locales/de_DE.po | 58 +++++++++++++++++++++++++++++++++++++++++ share/locales/en_GB.po | 58 +++++++++++++++++++++++++++++++++++++++++ templates/account.html.ep | 30 ++++++++++----------- 6 files changed, 188 insertions(+), 15 deletions(-) create mode 100644 lib/Travelynx/Helper/Locales.pm create mode 100644 share/locales/de_DE.po create mode 100644 share/locales/en_GB.po diff --git a/cpanfile b/cpanfile index 04083c7..6fccfd1 100644 --- a/cpanfile +++ b/cpanfile @@ -9,6 +9,8 @@ requires 'GIS::Distance::Fast'; requires 'IO::Socket::Socks', '>= 0.64'; requires 'IO::Socket::SSL', '>= 2.009'; requires 'List::UtilsBy'; +requires 'Locale::Maketext'; +requires 'Locale::Maketext::Lexicon'; requires 'Math::Polygon'; requires 'MIME::Entity'; requires 'Mojolicious'; diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 7dba658..3ddce00 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -26,6 +26,7 @@ use Travelynx::Helper::DBRIS; use Travelynx::Helper::EFA; use Travelynx::Helper::HAFAS; use Travelynx::Helper::IRIS; +use Travelynx::Helper::Locales; use Travelynx::Helper::MOTIS; use Travelynx::Helper::Sendmail; use Travelynx::Helper::Traewelling; @@ -157,6 +158,31 @@ sub startup { } ); + $self->hook( + 'before_render' => sub { + my ($self) = @_; + + # TODO load languages from user profile, if set + + my @languages = ('en-GB'); + if ( my $languages = $self->req->headers->accept_language ) { + @languages = (); + + #say "-- Accept-Language: $languages"; + 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' ); + } + } + } + $self->stash( loc_handle => + Travelynx::Helper::Locales->get_handle(@languages) ); + } + ); + $self->attr( cache_iris_main => sub { my ($self) = @_; @@ -411,6 +437,13 @@ sub startup { } ); + $self->helper( + 'L' => sub { + my ( $self, @args ) = @_; + $self->stash('loc_handle')->maketext(@args); + } + ); + $self->helper( 'now' => sub { return DateTime->now( time_zone => 'Europe/Berlin' ); diff --git a/lib/Travelynx/Helper/Locales.pm b/lib/Travelynx/Helper/Locales.pm new file mode 100644 index 0000000..12e95d1 --- /dev/null +++ b/lib/Travelynx/Helper/Locales.pm @@ -0,0 +1,22 @@ +package Travelynx::Helper::Locales; + +use strict; +use warnings; + +use base qw(Locale::Maketext); + +our %lexicon = ( + _AUTO => 1, +); + +use Locale::Maketext::Lexicon { + _decode => 1, + '*' => [ Gettext => 'share/locales/*.po' ], +}; + +sub init { + my ($self) = @_; + return $self->SUPER::init( @_[ 1 .. $#_ ] ); +} + +1; diff --git a/share/locales/de_DE.po b/share/locales/de_DE.po new file mode 100644 index 0000000..190b577 --- /dev/null +++ b/share/locales/de_DE.po @@ -0,0 +1,58 @@ +msgid "" +msgstr "" +"Language: de-DE\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" + +# Global + +msgid "button.logout" +msgstr "Abmelden" + +# account.html.ep + +msgid "account.account" +msgstr "Account" + +msgid "account.name" +msgstr "Name" + +msgid "account.mail" +msgstr "E-Mail" + +msgid "account.password" +msgstr "Passwort" + +msgid "account.connections" +msgstr "Verbindungen" + +msgid "account.connections.enabled" +msgstr "Vorschläge aktiv" + +msgid "account.connections.disabled" +msgstr "Vorschläge deaktiviert" + +msgid "account.visibility" +msgstr "Sichtbarkeit" + +msgid "account.interaction" +msgstr "Interaktion" + +msgid "account.interaction.accept-follows" +msgstr "Accounts können dir direkt folgen" + +msgid "account.interaction.accept-follow-requests" +msgstr "Accounts können dir auf Anfrage folgen" + +msgid "account.interaction.one" +msgstr "eine" + +msgid "account.interaction.open-request" +msgstr "offene Anfrage" + +msgid "account.interaction.open-requests" +msgstr "offene Anfragen" + +msgid "account.interaction.disabled" +msgstr "Accounts können dir nicht folgen" diff --git a/share/locales/en_GB.po b/share/locales/en_GB.po new file mode 100644 index 0000000..1bfcbc3 --- /dev/null +++ b/share/locales/en_GB.po @@ -0,0 +1,58 @@ +msgid "" +msgstr "" +"Language: de-DE\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" + +# Global + +msgid "button.logout" +msgstr "Logout" + +# account.html.ep + +msgid "account.account" +msgstr "Account" + +msgid "account.name" +msgstr "Name" + +msgid "account.mail" +msgstr "E-Mail" + +msgid "account.password" +msgstr "Password" + +msgid "account.connections" +msgstr "Connections" + +msgid "account.connections.enabled" +msgstr "Suggestions enabled" + +msgid "account.connections.disabled" +msgstr "Suggestions disabled" + +msgid "account.visibility" +msgstr "Visibility" + +msgid "account.interaction" +msgstr "Interaction" + +msgid "account.interaction.accept-follows" +msgstr "Accounts may follow you" + +msgid "account.interaction.accept-follow-requests" +msgstr "Accounts may send follow requests" + +msgid "account.interaction.one" +msgstr "one" + +msgid "account.interaction.open-request" +msgstr "open request" + +msgid "account.interaction.open-requests" +msgstr "open requests" + +msgid "account.interaction.disabled" +msgstr "Accounts cannot follow you" diff --git a/templates/account.html.ep b/templates/account.html.ep index e4bf38d..4c5add8 100644 --- a/templates/account.html.ep +++ b/templates/account.html.ep @@ -46,34 +46,34 @@ % my $use_history = users->use_history(uid => $acc->{id});
-

Account

+

<%= L('account.account') %>

- + - + - + - + - + - + @@ -163,7 +163,7 @@ %= form_for 'logout' => begin %= csrf_field %= end -- cgit v1.2.3
Name<%= L('account.name') %> edit<%= $acc->{name} %>
Mail<%= L('account.mail') %> edit<%= $acc->{email} %>
Passwort<%= L('account.password') %> edit
Verbindungen<%= L('account.connections') %> edit % if ($use_history & 0x03) { - Vorschläge aktiv + %= L('account.connections.enabled') % } % else { - Vorschläge deaktiviert + <%= L('account.connections.disabled') %> % }
Sichtbarkeit<%= L('account.visibility') %> edit check<%= visibility_icon($acc->{default_visibility_str}) %> @@ -81,23 +81,23 @@
Interaktion<%= L('account.interaction') %> edit % if ($acc->{accept_follows}) { - Accounts können dir direkt folgen + <%= L('account.interaction.accept-follows') %> % } % elsif ($acc->{accept_follow_requests}) { - Accounts können dir auf Anfrage folgen + <%= L('account.interaction.accept-follow-requests') %> % if ($num_rx_follow_requests == 1) { - – eine offene Anfrage + – <%= L('account.interaction.one') %> <%= L('account.interaction.open-request') %> % } elsif ($num_rx_follow_requests) { - – <%= $num_rx_follow_requests %> offene Anfragen + – <%= $num_rx_follow_requests %> <%= L('account.interaction.open-requests') %> % } % } % else { - Accounts können dir nicht folgen + <%= L('account.interaction.disabled') %> % }