Skip to content

Commit

Permalink
(#1143) Fix i18n using generic app logger instead of the LRR-specific…
Browse files Browse the repository at this point in the history
… ones
  • Loading branch information
Difegue committed Dec 31, 2024
1 parent ff9a9b4 commit b2770b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
15 changes: 8 additions & 7 deletions lib/LANraragi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ sub startup {
$self->secrets( [ $secret . hostname() ] );
$self->plugin('RenderFile');

# Load i18n
LANraragi::Utils::I18NInitializer::initialize($self);

# Set Template::Toolkit as default renderer so we can use the LRR templates
$self->plugin('TemplateToolkit');
$self->renderer->default_handler('tt2');
Expand Down Expand Up @@ -91,6 +88,9 @@ sub startup {
die;
}

# Load i18n
LANraragi::Utils::I18NInitializer::initialize($self);

# Catch Redis errors on our first connection. This is useful in case of temporary LOADING errors,
# Where Redis lets us send commands but doesn't necessarily reply to them properly.
# (https://github.com/redis/redis/issues/4624)
Expand Down Expand Up @@ -194,18 +194,19 @@ sub startup {

my $prefix = $self->LRR_BASEURL;
if ($prefix) {
if (!$prefix =~ m|^/[^"]*[^/"]$|) {
if ( !$prefix =~ m|^/[^"]*[^/"]$| ) {
say "Warning: configured URL prefix '$prefix' invalid, ignoring";

# if prefix is invalid, then set it to empty for the cookie
$prefix = "";
}
else {
} else {
$c->req->url->base->path($prefix);
}
}

# SameSite=Lax is the default behavior here; I set it
# explicitly to get rid of a warning in the browser
$c->cookie("lrr_baseurl" => $prefix, { samesite => "lax" });
$c->cookie( "lrr_baseurl" => $prefix, { samesite => "lax" } );
}
);

Expand Down
12 changes: 6 additions & 6 deletions lib/LANraragi/Utils/I18NInitializer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ sub initialize {
if ($accept_language) {
($lang) = split /,/, $accept_language;
$lang =~ s/-.*//;
$c->log->trace("Detected language: $lang");
$c->LRR_LOGGER->trace("Detected language: $lang");
}

my $handle = LANraragi::Utils::I18N->get_handle($lang);
unless ($handle) {
$c->log->trace("No handle for language: $lang, fallback to en");
$c->LRR_LOGGER->trace("No handle for language: $lang, fallback to en");
$handle = LANraragi::Utils::I18N->get_handle('en');
return $key unless $handle;
}

$c->log->trace( "Key: $key, Args: " . join( ", ", map { defined($_) ? $_ : 'undef' } @args ) );
$c->LRR_LOGGER->trace( "Key: $key, Args: " . join( ", ", map { defined($_) ? $_ : 'undef' } @args ) );

# make sure all args are encoded in UTF-8
my @encoded_args = map { Encode::encode( 'UTF-8', $_ ) } @args;

my $translated;
eval { $translated = $handle->maketext( $key, @encoded_args ); };
if ($@) {
$c->log->error("Maketext error: $@");
$c->LRR_LOGGER->error("Maketext error: $@");
return $key;
}

Expand All @@ -47,12 +47,12 @@ sub initialize {
$translated = Encode::decode_utf8($translated);
}

$c->log->trace("Translated result: $translated");
$c->LRR_LOGGER->trace("Translated result: $translated");
return $translated;
}
);

$app->log->debug("I18N system initialized.");
$app->LRR_LOGGER->debug("I18N system initialized.");
}

1;

0 comments on commit b2770b4

Please sign in to comment.