# Selima Website Content Management System # LnInfo.pm: The language information. # Copyright (c) 2003-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: 2003-03-23 package Selima::LnInfo; use 5.008; use strict; use warnings; use base qw(Exporter); use vars qw(@EXPORT @EXPORT_OK); BEGIN { @EXPORT = qw(ln); @EXPORT_OK = @EXPORT; # Prototype declaration sub init_lninfo(); sub ln($$); sub name($); sub charset($); sub filename($); sub locale($); sub db($); sub htmlid($); sub space_break($); sub country_first($); sub desc($); sub desc_curlc($); sub desc_selflc($); sub switch_title($); } use Selima::DataVars qw(:l10n :lninfo); use Selima::GetLang; use Selima::SetL10N; use Selima::ShortCut; use vars qw(%LANGS %DESC_SELFLC %SWITCH_TITLE); %LANGS = ( "zh-tw" => { "name" => "zh-tw", "filename" => "zh-tw", "charset" => "UTF-8", "db" => "zhtw", "htmlid" => "zhtw", "locale" => "zh_TW", "sp_break" => 0, "ctfirst" => 1, "longdesc" => N_("Traditional Chinese")}, "zh-cn" => { "name" => "zh-cn", "filename" => "zh-cn", "charset" => "UTF-8", "db" => "zhcn", "htmlid" => "zhcn", "locale" => "zh_CN", "sp_break" => 0, "ctfirst" => 1, "longdesc" => N_("Simplified Chinese")}, "en" => { "name" => "en-us", "filename" => "en", "charset" => "UTF-8", "db" => "en", "htmlid" => "en", "locale" => "en_US", "sp_break" => 1, "ctfirst" => 0, "longdesc" => N_("English")}, "ja" => { "name" => "ja", "filename" => "ja", "charset" => "UTF-8", "db" => "ja", "htmlid" => "ja", "locale" => "ja_JP", "sp_break" => 0, "ctfirst" => 1, "longdesc" => N_("Japanese")}, "de" => { "name" => "de", "filename" => "de", "charset" => "UTF-8", "db" => "de", "htmlid" => "de", "locale" => "de_DE", "sp_break" => 0, "ctfirst" => 1, "longdesc" => N_("German")}); # Subroutines # ln: Uniformed wrapper for everything sub ln($$) { local ($_, %_); my ($lang, $type); ($lang, $type) = @_; if ($type == LN_NAME) { return name($lang); } elsif ($type == LN_CHARSET) { return charset($lang); } elsif ($type == LN_FILENAME) { return filename($lang); } elsif ($type == LN_LOCALE) { return locale($lang); } elsif ($type == LN_DATABASE) { return db($lang); } elsif ($type == LN_HTMLID) { return htmlid($lang); } elsif ($type == LN_SPACE_BREAK) { return space_break($lang); } elsif ($type == LN_COUNTRY_FIRST) { return country_first($lang); } elsif ($type == LN_DESC) { return desc($lang); } elsif ($type == LN_DESC_CURLC) { return desc_curlc($lang); } elsif ($type == LN_DESC_SELFLC) { return desc_selflc($lang); } elsif ($type == LN_SWITCH_TITLE) { return switch_title($lang); } else { return $lang; } } # name: Language complete name # Return en-us for en, to be more specified sub name($) { return exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"name"}? ${$LANGS{$_[0]}}{"name"}: $_[0]; } # charset: File system name sub charset($) { return exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"charset"}? ${$LANGS{$_[0]}}{"charset"}: $_[0]; } # filename: Most common charset that is used with the specified language sub filename($) { return exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"filename"}? ${$LANGS{$_[0]}}{"filename"}: $_[0]; } # locale: Locale used by GNU glibc and gettext # Replace hyphens with underscores, and upper-case the district divisions, # as used in GNU glibc sub locale($) { local ($_, %_); return ${$LANGS{$_[0]}}{"locale"} if exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"locale"}; $_ = $_[0]; s/-/_/; s/_([a-z]+)/"_" . uc $1/e; return $_; } # db: Database column name # Hyphens are not allowed, to avoid confusion # with the substraction operator "-" sub db($) { local ($_, %_); return ${$LANGS{$_[0]}}{"db"} if exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"db"}; $_ = $_[0]; s/-//g; return $_; } # htmlid: HTML id value # Hyphens are not allowed, as specified in HTML 4.01 specification sub htmlid($) { local ($_, %_); return ${$LANGS{$_[0]}}{"htmlid"} if exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"htmlid"}; $_ = $_[0]; s/-//g; return $_; } # space_break: Whether words are seperated at spaces sub space_break($) { # Default to true, for alphabetic languages return exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"sp_break"}? ${$LANGS{$_[0]}}{"sp_break"}: 1; } # country_first: Whether country should be listed first in a mail address sub country_first($) { # Default to false, for European languages return exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"ctfirst"}? ${$LANGS{$_[0]}}{"ctfirst"}: 0; } # desc: Long description sub desc($) { return exists $LANGS{$_[0]} && ${$LANGS{$_[0]}}{"longdesc"}? ${$LANGS{$_[0]}}{"longdesc"}: $_[0]; } # desc_curlc: Long description in the current locale sub desc_curlc($) { return C_(desc($_[0])); } # desc_selflc: Long description in its own locale sub desc_selflc($) { local ($_, %_); my ($lang, $lh); $lang = $_[0]; # Obtain the description list if (scalar(keys %DESC_SELFLC) == 0) { foreach my $ln (keys %LANGS) { # Switch the locale set_l10n $ln; # Obtain the long description $DESC_SELFLC{$ln} = C_(desc $ln); } # Switch back the locale set_l10n; } # Return the proper description return exists $DESC_SELFLC{$lang}? $DESC_SELFLC{$lang}: $lang; } # switch_title: Title of the language switch sub switch_title($) { my ($lang, $lh); $lang = $_[0]; # Obtain the description list if (scalar(keys %SWITCH_TITLE) == 0) { # Find the descriptions in their own locale first # Obtain the description list foreach my $ln (keys %LANGS) { # Switch the locale set_l10n $ln; # Obtain the long description $SWITCH_TITLE{$ln} = C_("Switch to the %s version of this page."); } # Switch back the locale set_l10n; } # Not found -- use description in its own locale instead return desc_selflc($lang) if !exists $SWITCH_TITLE{$lang}; # Return the proper description return sprintf $SWITCH_TITLE{$lang}, desc_selflc($lang); } return 1;