# Selima Website Content Management System # SetL10N.pm: The subroutine to set the localization handler. # Copyright (c) 2004-2018 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Author: imacat # First written: 2004-10-26 package Selima::SetL10N; use 5.008; use strict; use warnings; use base qw(Exporter); use vars qw(@EXPORT @EXPORT_OK); BEGIN { @EXPORT = qw(set_l10n); @EXPORT_OK = @EXPORT; # Prototype declaration sub set_l10n(;$); } # Ensure that these modules are installed use Encode::Big5Common qw(); use Encode::HanExtra qw(); # For GB18030, real encoding used in GB2312 forms use Encode::Alias qw(define_alias); # Set Big5 to Big5-Common define_alias("Big5", "Big5-Common"); use Selima::Cache qw(:setl10n); use Selima::DataVars qw(:l10n :lninfo :siteconf); use Selima::GetLang; use Selima::HTTP; use Selima::LnInfo; use Selima::L10N; use vars qw(%LH %CLH); # set_l10n: Set the current language handler sub set_l10n(;$) { local ($_, %_); my $lang; $lang = $_[0]; $lang = getlang if !defined $lang; # Set the package language handler # Cached -- use the cache if (exists $LH{$PACKAGE} && exists ${$LH{$PACKAGE}}{$lang}) { $LH = ${$LH{$PACKAGE}}{$lang}; # Automatic reload text whenever it is updated if (-e $LH->{"MOFILE"}) { $SetL10N_checked{$PACKAGE} = {} if !exists $SetL10N_checked{$PACKAGE}; if (!exists ${$SetL10N_checked{$PACKAGE}}{$lang}) { $_ = (stat $LH->{"MOFILE"})[9]; if ($LH->{"MOFILE_MTIME"} < $_) { $LH->{"MOFILE_MTIME"} = $_; $LH->reload_text; } ${$SetL10N_checked{$PACKAGE}}{$lang} = 1; } } # Not cached yet } else { $_ = "Selima::" . $PACKAGE . "::L10N"; $LH = $_->get_handle($lang) or http_500 "Failed creating language handle from $_ for $lang"; $LH->encoding(undef); $LH->bindtextdomain($PACKAGE, $LOCALEDIR); $LH->textdomain($PACKAGE); # Cache it $LH{$PACKAGE} = {} if !exists $LH{$PACKAGE}; ${$LH{$PACKAGE}}{$lang} = $LH; $LH->{"MOFILE_MTIME"} = (stat $LH->{"MOFILE"})[9] if -e $LH->{"MOFILE"}; } # Set the common language handler # Cached -- use the cache if (exists $CLH{$lang}) { $CLH = $CLH{$lang}; # Automatic reload text whenever it is updated if (-e $CLH->{"MOFILE"}) { $SetL10N_checked{COMMON_DOMAIN()} = {} if !exists $SetL10N_checked{COMMON_DOMAIN()}; if (!exists ${$SetL10N_checked{COMMON_DOMAIN()}}{$lang}) { $_ = (stat $CLH->{"MOFILE"})[9]; if ($CLH->{"MOFILE_MTIME"} < $_) { $CLH->{"MOFILE_MTIME"} = $_; $CLH->reload_text; } ${$SetL10N_checked{COMMON_DOMAIN()}}{$lang} = 1; } } # Not cached yet } else { $CLH = Selima::L10N->get_handle($lang) or http_500 "Failed creating language handle from Selima::L10N for $lang"; $CLH->encoding(undef); $CLH->bindtextdomain(COMMON_DOMAIN, COMMON_LOCALEDIR); # Cache it $CLH->textdomain(COMMON_DOMAIN); $CLH{$lang} = $CLH; $CLH->{"MOFILE_MTIME"} = (stat $CLH->{"MOFILE"})[9] if -e $CLH->{"MOFILE"}; } return; } return 1;