382 lines
11 KiB
PHP
382 lines
11 KiB
PHP
<?php
|
|
// File name: lninfo.inc.php
|
|
// Description: PHP subroutines to return specific language information
|
|
// Date: 2002-07-29
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2002-2018 Pristine Communications
|
|
|
|
// Set the include path
|
|
if (!defined("INCPATH_SET")) {
|
|
require_once dirname(__FILE__) . "/incpath.inc.php";
|
|
}
|
|
// Related subroutines
|
|
require_once "monica/getlang.inc.php";
|
|
require_once "monica/gettext.inc.php";
|
|
|
|
// Settings
|
|
$LNINFO = array(
|
|
"en" => array(
|
|
"name" => "en-us",
|
|
"filename" => "en",
|
|
"charset" => "UTF-8",
|
|
"db" => "en",
|
|
"htmlid" => "en",
|
|
"locale" => "en_US",
|
|
"sp_break" => true,
|
|
"icase" => true,
|
|
"ctfirst" => false,
|
|
"longdesc" => N_("English")),
|
|
"zh-tw" => array(
|
|
"name" => "zh-tw",
|
|
"filename" => "zh-tw",
|
|
"charset" => "UTF-8",
|
|
"db" => "zhtw",
|
|
"htmlid" => "zhtw",
|
|
"locale" => "zh_TW",
|
|
"sp_break" => false,
|
|
"icase" => false,
|
|
"ctfirst" => true,
|
|
"longdesc" => N_("Traditional Chinese")),
|
|
"zh-cn" => array(
|
|
"name" => "zh-cn",
|
|
"filename" => "zh-cn",
|
|
"charset" => "UTF-8",
|
|
"db" => "zhcn",
|
|
"htmlid" => "zhcn",
|
|
"locale" => "zh_CN",
|
|
"sp_break" => false,
|
|
"icase" => false,
|
|
"ctfirst" => true,
|
|
"longdesc" => N_("Simplified Chinese")),
|
|
"zh" => array(
|
|
"name" => "zh-tw",
|
|
"filename" => "zh",
|
|
"charset" => "UTF-8",
|
|
"db" => "zh",
|
|
"htmlid" => "zh",
|
|
"locale" => "zh_TW",
|
|
"sp_break" => false,
|
|
"icase" => false,
|
|
"ctfirst" => true,
|
|
"longdesc" => N_("Chinese")),
|
|
"ja" => array(
|
|
"name" => "ja",
|
|
"filename" => "ja",
|
|
"charset" => "UTF-8",
|
|
"db" => "ja",
|
|
"htmlid" => "ja",
|
|
"locale" => "ja_JP",
|
|
"sp_break" => false,
|
|
"icase" => false,
|
|
"ctfirst" => true,
|
|
"longdesc" => N_("Japanese")),
|
|
"ko" => array(
|
|
"name" => "ko",
|
|
"filename" => "ko",
|
|
"charset" => "UTF-8",
|
|
"db" => "ko",
|
|
"htmlid" => "ko",
|
|
"locale" => "ko_KR",
|
|
"sp_break" => false,
|
|
"icase" => false,
|
|
"ctfirst" => true,
|
|
"longdesc" => N_("Korean")),
|
|
"de" => array(
|
|
"name" => "de",
|
|
"filename" => "de",
|
|
"charset" => "UTF-8",
|
|
"db" => "de",
|
|
"htmlid" => "de",
|
|
"locale" => "de_DE",
|
|
"sp_break" => true,
|
|
"icase" => true,
|
|
"ctfirst" => false,
|
|
"longdesc" => N_("German")),
|
|
"es" => array(
|
|
"name" => "es",
|
|
"filename" => "es",
|
|
"charset" => "UTF-8",
|
|
"db" => "es",
|
|
"htmlid" => "es",
|
|
"locale" => "es_ES",
|
|
"sp_break" => true,
|
|
"icase" => true,
|
|
"ctfirst" => false,
|
|
"longdesc" => N_("Spanish"))
|
|
);
|
|
|
|
// Constant symbols
|
|
define("LN_NAME", 0);
|
|
define("LN_CHARSET", 1);
|
|
define("LN_FILENAME", 2);
|
|
define("LN_LOCALE", 3);
|
|
define("LN_DATABASE", 4);
|
|
define("LN_HTMLID", 5);
|
|
define("LN_SPACE_BREAK", 6);
|
|
define("LN_IGNORE_CASE", 7);
|
|
define("LN_COUNTRY_FIRST", 8);
|
|
define("LN_DESC", 9);
|
|
define("LN_DESC_CURLC", 10);
|
|
define("LN_DESC_SELFLC", 11);
|
|
define("LN_SWITCH_TITLE", 12);
|
|
|
|
|
|
// Subroutines
|
|
// ln: Uniformed wrapper for everything
|
|
function ln($lang, $type)
|
|
{
|
|
switch ($type) {
|
|
case LN_NAME:
|
|
return _lninfo_name($lang);
|
|
case LN_CHARSET:
|
|
return _lninfo_charset($lang);
|
|
case LN_FILENAME:
|
|
return _lninfo_filename($lang);
|
|
case LN_LOCALE:
|
|
return _lninfo_lc($lang);
|
|
case LN_DATABASE:
|
|
return _lninfo_db($lang);
|
|
case LN_HTMLID:
|
|
return _lninfo_htmlid($lang);
|
|
case LN_SPACE_BREAK:
|
|
return _lninfo_space_break($lang);
|
|
case LN_IGNORE_CASE:
|
|
return _lninfo_ignore_case($lang);
|
|
case LN_COUNTRY_FIRST:
|
|
return _lninfo_country_first($lang);
|
|
case LN_DESC:
|
|
return _lninfo_desc($lang);
|
|
case LN_DESC_CURLC:
|
|
return _lninfo_desc_curlc($lang);
|
|
case LN_DESC_SELFLC:
|
|
return _lninfo_desc_selflc($lang);
|
|
case LN_SWITCH_TITLE:
|
|
return _lninfo_switch_title($lang);
|
|
default:
|
|
return $lang;
|
|
}
|
|
}
|
|
|
|
// selflc: Return the text in its own locale
|
|
function selflc($text, $lang)
|
|
{
|
|
// Cache the result
|
|
static $cache = array();
|
|
// Obtain the language variants of that text
|
|
if (!array_key_exists($text, $cache)) {
|
|
global $LNINFO;
|
|
$curlc = getenv("LANG");
|
|
if ($curlc === false || $curlc == "") {
|
|
$curlc = getlang(LN_LOCALE);
|
|
}
|
|
$cache[$text] = array();
|
|
foreach (array_keys($LNINFO) as $l) {
|
|
// Switch the locale
|
|
$locale = _lninfo_lc($l);
|
|
$charset = _lninfo_charset($l);
|
|
putenv("LANG=$locale");
|
|
putenv("LANGUAGE=$locale");
|
|
setlocale(LC_ALL, "");
|
|
// Obtain the localed text
|
|
$cache[$text][$l] = _($text);
|
|
}
|
|
// Return the current locale
|
|
putenv("LANG=$curlc");
|
|
putenv("LANGUAGE=$curlc");
|
|
setlocale(LC_ALL, "");
|
|
}
|
|
// Not found -- return itself instead
|
|
if (!array_key_exists($lang, $cache[$text])) {
|
|
return $text;
|
|
}
|
|
// Return the localed text
|
|
return $cache[$text][$lang];
|
|
}
|
|
|
|
// _lninfo_name: Language complete name
|
|
function _lninfo_name($lang)
|
|
{
|
|
global $LNINFO;
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("name", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["name"]: $lang;
|
|
}
|
|
|
|
// _lninfo_charset: Long description
|
|
// Most common charset that is used with the specified language
|
|
function _lninfo_charset($lang)
|
|
{
|
|
global $LNINFO;
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("charset", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["charset"]: $lang;
|
|
}
|
|
|
|
// _lninfo_filename: File system name
|
|
function _lninfo_filename($lang)
|
|
{
|
|
global $LNINFO;
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("filename", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["filename"]: $lang;
|
|
}
|
|
|
|
// _lninfo_lc: Locale used by GNU glibc and gettext
|
|
// Replace hyphens with underscores, and upper-case the district divisions,
|
|
// as used in GNU glibc
|
|
function _lninfo_lc($lang)
|
|
{
|
|
global $LNINFO;
|
|
if ( array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("locale", $LNINFO[$lang])) {
|
|
return $LNINFO[$lang]["locale"];
|
|
}
|
|
$pos = strpos($lang, "-");
|
|
if ($pos !== false) {
|
|
return substr($lang, 0, $pos) . "_"
|
|
. strtoupper(substr($lang, $pos + 1));
|
|
}
|
|
return $lang;
|
|
}
|
|
|
|
// _lninfo_db: Database column name
|
|
// Hyphens are not allowed, to avoid confusion
|
|
// with the substraction operator "-"
|
|
function _lninfo_db($lang)
|
|
{
|
|
global $LNINFO;
|
|
if ( array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("db", $LNINFO[$lang])) {
|
|
return $LNINFO[$lang]["db"];
|
|
}
|
|
return str_replace("-", "", $lang);
|
|
}
|
|
|
|
// _lninfo_htmlid: HTML id value
|
|
// Hyphens are not allowed, as specified in HTML 4.01 specification
|
|
function _lninfo_htmlid($lang)
|
|
{
|
|
global $LNINFO;
|
|
if ( array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("htmlid", $LNINFO[$lang])) {
|
|
return $LNINFO[$lang]["htmlid"];
|
|
}
|
|
return str_replace("-", "", $lang);
|
|
}
|
|
|
|
// _lninfo_space_break: Whether words are seperated at spaces
|
|
function _lninfo_space_break($lang)
|
|
{
|
|
global $LNINFO;
|
|
// Default to true, for alphabetic languages
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("sp_break", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["sp_break"]: true;
|
|
}
|
|
|
|
// _lninfo_ignore_case: Whether case can be ignored
|
|
function _lninfo_ignore_case($lang)
|
|
{
|
|
global $LNINFO;
|
|
// Default to true, for European languages
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("icase", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["icase"]: true;
|
|
}
|
|
|
|
// _lninfo_country_first: Whether country should be listed first in a mail address
|
|
function _lninfo_country_first($lang)
|
|
{
|
|
global $LNINFO;
|
|
// Default to false, for European languages
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("ctfirst", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["ctfirst"]: false;
|
|
}
|
|
|
|
// _lninfo_desc: Long description
|
|
function _lninfo_desc($lang)
|
|
{
|
|
global $LNINFO;
|
|
return array_key_exists($lang, $LNINFO)
|
|
&& array_key_exists("longdesc", $LNINFO[$lang])?
|
|
$LNINFO[$lang]["longdesc"]: $lang;
|
|
}
|
|
|
|
// _lninfo_desc_curlc: Long description in the current locale
|
|
function _lninfo_desc_curlc($lang)
|
|
{
|
|
return C_(_lninfo_desc($lang));
|
|
}
|
|
|
|
// _lninfo_desc_selflc: Long description in its own locale
|
|
function _lninfo_desc_selflc($lang)
|
|
{
|
|
// Cache the result
|
|
static $desc;
|
|
// Obtain the description list
|
|
if (!isset($desc)) {
|
|
global $LNINFO;
|
|
$curlc = getenv("LANG");
|
|
if ($curlc === false || $curlc == "") {
|
|
$curlc = getlang(LN_LOCALE);
|
|
}
|
|
$desc = array();
|
|
foreach (array_keys($LNINFO) as $l) {
|
|
// Switch the locale
|
|
$locale = _lninfo_lc($l);
|
|
$charset = _lninfo_charset($l);
|
|
putenv("LANG=$locale");
|
|
putenv("LANGUAGE=$locale");
|
|
setlocale(LC_ALL, "");
|
|
// Obtain the long description
|
|
$desc[$l] = _lninfo_desc_curlc($l);
|
|
}
|
|
// Return the current locale
|
|
putenv("LANG=$curlc");
|
|
putenv("LANGUAGE=$curlc");
|
|
setlocale(LC_ALL, "");
|
|
}
|
|
// Return the proper description
|
|
return array_key_exists($lang, $desc)?
|
|
$desc[$lang]: $lang;
|
|
}
|
|
|
|
// _lninfo_switch_title: Title of the language switch
|
|
function _lninfo_switch_title($lang)
|
|
{
|
|
// Cache the result
|
|
static $title;
|
|
// Obtain the title list
|
|
if (!isset($title)) {
|
|
global $LNINFO;
|
|
$curlc = getenv("LANG");
|
|
if ($curlc === false || $curlc == "") {
|
|
$curlc = getlang(LN_LOCALE);
|
|
}
|
|
$title = array();
|
|
foreach (array_keys($LNINFO) as $l) {
|
|
// Switch the locale
|
|
$locale = _lninfo_lc($l);
|
|
$charset = _lninfo_charset($l);
|
|
putenv("LANG=$locale");
|
|
putenv("LANGUAGE=$locale");
|
|
setlocale(LC_ALL, "");
|
|
// Obtain the title
|
|
$title[$l] = C_("Switch to the %s version of this page.");
|
|
}
|
|
// Return the current locale
|
|
putenv("LANG=$curlc");
|
|
putenv("LANGUAGE=$curlc");
|
|
setlocale(LC_ALL, "");
|
|
}
|
|
// Not found -- use description in its own locale instead
|
|
if (!array_key_exists($lang, $title)) {
|
|
return _lninfo_desc_selflc($lang);
|
|
}
|
|
// Return the proper title
|
|
return sprintf($title[$lang], _lninfo_desc_selflc($lang));
|
|
}
|
|
|
|
?>
|