From ca8a854cf95f24c37fc83740953cc6f47ea0a9a3 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 19 Feb 2018 13:35:14 +0100 Subject: [PATCH] Bug 15395: Fix for libintl-perl >= 1.25 Since 1.25, LANGUAGE environment variable is ignored unless LANG is set to a valid locale. This patch sets LANG to a valid locale and calls setlocale. If no valid locale can be found, a warning message is written in logs. See https://rt.cpan.org/Public/Bug/Display.html?id=81315 and also https://github.com/gflohr/libintl-perl/commit/5910980b6e --- Koha/I18N.pm | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/Koha/I18N.pm b/Koha/I18N.pm index 95c82ff4f4..e2b88b7beb 100644 --- a/Koha/I18N.pm +++ b/Koha/I18N.pm @@ -24,7 +24,7 @@ use C4::Languages; use C4::Context; use Encode; -use Locale::Messages qw(:locale_h nl_putenv); +use Locale::Messages qw(:locale_h nl_putenv setlocale LC_MESSAGES); use Koha::Cache::Memory::Lite; use parent 'Exporter'; @@ -50,24 +50,34 @@ sub init { my $cache = Koha::Cache::Memory::Lite->get_instance(); my $cache_key = 'i18n:initialized'; unless ($cache->get_from_cache($cache_key)) { - my $langtag = C4::Languages::getlanguage; - my @subtags = split /-/, $langtag; - my ($language, $region) = @subtags; - if ($region && length $region == 4) { - $region = $subtags[2]; - } Locale::Messages->select_package('gettext_pp'); - my $locale = $language; - if ($region) { - $locale .= '_' . $region; + my @system_locales = grep { chomp; not (/^C/ || $_ eq 'POSIX') } qx/locale -a/; + if (@system_locales) { + # LANG needs to be set to a valid locale, + # otherwise LANGUAGE is ignored + nl_putenv('LANG=' . $system_locales[0]); + setlocale(LC_MESSAGES, ''); + + my $langtag = C4::Languages::getlanguage; + my @subtags = split /-/, $langtag; + my ($language, $region) = @subtags; + if ($region && length $region == 4) { + $region = $subtags[2]; + } + my $locale = $language; + if ($region) { + $locale .= '_' . $region; + } + + nl_putenv("LANGUAGE=$locale"); + nl_putenv('OUTPUT_CHARSET=UTF-8'); + + my $directory = _base_directory(); + textdomain($textdomain); + bindtextdomain($textdomain, $directory); + } else { + warn "No locale installed. Localization cannot work and is therefore disabled"; } - nl_putenv("LANGUAGE=$locale"); - nl_putenv("LANG=$locale"); - nl_putenv('OUTPUT_CHARSET=utf-8'); - - my $directory = _base_directory(); - textdomain($textdomain); - bindtextdomain($textdomain, $directory); $cache->set_in_cache($cache_key, 1); } -- 2.14.2