Updates for language auto detect, fixes for PHP 7.4
There was a bug in ther SQL interface class where the folder was just called "Interface" which is not an allowed Namespace name. Renamed to SqlInterface Moved the detect lang/etc function used in Form/Generate, Template/SmartyExtend and Admin/Backend to Language/GetSettings. Fixed some test class calls
This commit is contained in:
@@ -168,7 +168,7 @@ class Login
|
||||
* @param \CoreLibs\DB\IO $db Database connection class
|
||||
* @param \CoreLibs\Debug\Logging $log Logging class
|
||||
* @param \CoreLibs\Language\L10n|null $l10n l10n language class
|
||||
* if null, auto set
|
||||
* if null, auto set
|
||||
*/
|
||||
public function __construct(
|
||||
\CoreLibs\DB\IO $db,
|
||||
|
||||
@@ -92,6 +92,8 @@ class Backend
|
||||
/** @var string */
|
||||
public $lang_short;
|
||||
/** @var string */
|
||||
public $domain;
|
||||
/** @var string */
|
||||
public $encoding;
|
||||
/** @var \CoreLibs\Debug\Logging logger */
|
||||
public $log;
|
||||
@@ -171,27 +173,13 @@ class Backend
|
||||
*/
|
||||
private function setLangEncoding(): void
|
||||
{
|
||||
// just emergency fallback for language
|
||||
// set encoding
|
||||
if (isset($_SESSION['DEFAULT_CHARSET'])) {
|
||||
$this->encoding = $_SESSION['DEFAULT_CHARSET'];
|
||||
} else {
|
||||
$this->encoding = DEFAULT_ENCODING;
|
||||
}
|
||||
// gobal override
|
||||
if (isset($GLOBALS['OVERRIDE_LANG'])) {
|
||||
$this->lang = $GLOBALS['OVERRIDE_LANG'];
|
||||
} elseif (isset($_SESSION['DEFAULT_LANG'])) {
|
||||
// session (login)
|
||||
$this->lang = $_SESSION['DEFAULT_LANG'];
|
||||
} else {
|
||||
// mostly default SITE LANG or DEFAULT LANG
|
||||
$this->lang = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG;
|
||||
}
|
||||
// create the char lang encoding
|
||||
$this->lang_short = substr($this->lang, 0, 2);
|
||||
// set the language folder
|
||||
$this->lang_dir = BASE . INCLUDES . LANG . CONTENT_PATH;
|
||||
list (
|
||||
$this->encoding,
|
||||
$this->lang,
|
||||
$this->lang_short,
|
||||
$this->domain,
|
||||
$this->lang_dir
|
||||
) = \CoreLibs\Language\GetSettings::setLangEncoding();
|
||||
}
|
||||
|
||||
// PUBLIC METHODS |=================================================>
|
||||
|
||||
@@ -382,7 +382,7 @@ class IO
|
||||
public function __construct(
|
||||
array $db_config,
|
||||
?\CoreLibs\Debug\Logging $log = null,
|
||||
?bool $db_debug_override = null,
|
||||
?bool $db_debug_override = null
|
||||
) {
|
||||
// attach logger
|
||||
$this->log = $log ?? new \CoreLibs\Debug\Logging();
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace CoreLibs\DB\SQL;
|
||||
// as main system. Currently all @var sets are written as object
|
||||
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
|
||||
|
||||
class PgSQL implements Interface\SqlFunctions
|
||||
class PgSQL implements \CoreLibs\DB\SQL\SqlInterface\SqlFunctions
|
||||
{
|
||||
/** @var string */
|
||||
private $last_error_query;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\DB\SQL\Interface;
|
||||
namespace CoreLibs\DB\SQL\SqlInterface;
|
||||
|
||||
interface SqlFunctions
|
||||
{
|
||||
123
www/lib/CoreLibs/Language/GetSettings.php
Normal file
123
www/lib/CoreLibs/Language/GetSettings.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Internal function for getting languange and encodig settings
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language;
|
||||
|
||||
class GetSettings
|
||||
{
|
||||
/**
|
||||
* Sets encoding and language
|
||||
* Can be overridden with language + path to mo file
|
||||
* If language is set it must be in the format of:
|
||||
* <lang>.<encoding>
|
||||
* <lang>_<country>.<encoding>
|
||||
* <lang>_<country>@<subset>.<encoding>
|
||||
*
|
||||
* @param string|null $language
|
||||
* @param string|null $path
|
||||
* @return array
|
||||
*/
|
||||
public static function setLangEncoding(
|
||||
?string $language = null,
|
||||
?string $domain = null,
|
||||
?string $path = null
|
||||
): array {
|
||||
$lang = '';
|
||||
$lang_short = '';
|
||||
$encoding = '';
|
||||
// if language is set, extract
|
||||
if (!empty($language)) {
|
||||
preg_match(
|
||||
"/^(([a-z]{2,})(_[A-Z]{2,})?(@[a-z]{1,})?)(\.([\w-])+)?$/",
|
||||
$language,
|
||||
$matches
|
||||
);
|
||||
// 1: lang (always)
|
||||
$lang = $matches[1] ?? '';
|
||||
// 2: lang short part
|
||||
$lang_short = $matches[2] ?? '';
|
||||
// 3: [ignore] sub part, if set, combined with lang
|
||||
// 4: [ignore] possible sub part, combined with lang in 1
|
||||
// 6: encoding if set
|
||||
$encoding = $matches[5] ?? '';
|
||||
}
|
||||
// if domain is set, must be alphanumeric, if not unset
|
||||
if (
|
||||
!empty($domain) &&
|
||||
!preg_match("/^\w+$/", $domain)
|
||||
) {
|
||||
$domain = '';
|
||||
}
|
||||
// path checks if set, if not valid path unset
|
||||
if (
|
||||
!empty($path) &&
|
||||
!is_dir($path)
|
||||
) {
|
||||
$path = '';
|
||||
}
|
||||
|
||||
// just emergency fallback for language
|
||||
// set encoding
|
||||
if (empty($encoding)) {
|
||||
if (!empty($_SESSION['DEFAULT_CHARSET'])) {
|
||||
$encoding = $_SESSION['DEFAULT_CHARSET'];
|
||||
} else {
|
||||
$encoding = DEFAULT_ENCODING;
|
||||
}
|
||||
}
|
||||
// gobal override
|
||||
if (empty($lang)) {
|
||||
if (!empty($GLOBALS['OVERRIDE_LANG'])) {
|
||||
$lang = $GLOBALS['OVERRIDE_LANG'];
|
||||
} elseif (!empty($_SESSION['DEFAULT_LANG'])) {
|
||||
// session (login)
|
||||
$lang = $_SESSION['DEFAULT_LANG'];
|
||||
} else {
|
||||
// mostly default SITE LANG or DEFAULT LANG
|
||||
$lang = defined('SITE_LANG') && !empty(SITE_LANG) ?
|
||||
SITE_LANG :
|
||||
DEFAULT_LANG;
|
||||
}
|
||||
}
|
||||
// create the char lang encoding
|
||||
if (empty($lang_short)) {
|
||||
$lang_short = substr($lang, 0, 2);
|
||||
}
|
||||
// set the language folder
|
||||
if (empty($path)) {
|
||||
// LEGACY
|
||||
$path = BASE . INCLUDES . LANG . CONTENT_PATH;
|
||||
// will be BASE . INCLUDES . LANG . $language . /LC_MESSAGES/
|
||||
// so CONTENT_PATH has to be removed
|
||||
}
|
||||
// if no domain is set, fall back to content path
|
||||
if (empty($domain)) {
|
||||
$domain = str_replace('/', '', CONTENT_PATH);
|
||||
}
|
||||
// return
|
||||
return [
|
||||
// as array
|
||||
0 => $encoding,
|
||||
1 => $lang,
|
||||
2 => $lang_short,
|
||||
3 => $domain,
|
||||
4 => $path,
|
||||
// with index name
|
||||
// encoding
|
||||
'encoding' => $encoding,
|
||||
// language full string, eg en_US
|
||||
'lang' => $lang,
|
||||
// lang short, if eg en_US only en
|
||||
'lang_short' => $lang_short,
|
||||
// translation domain (CONTENT_PATH)
|
||||
'domain' => $domain,
|
||||
// folder BASE ONLY
|
||||
'path' => $path,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
* PUBLIC METHODS
|
||||
* __: returns string (translated or original if not found)
|
||||
* __e: echos out string (translated or original if not found)
|
||||
* __ngettext: should return plural. never tested this.
|
||||
* __n: should return plural. never tested this.
|
||||
*
|
||||
* PRIVATE METHODS
|
||||
*
|
||||
@@ -24,6 +24,13 @@
|
||||
* 2005/10/17 (cs) made an on the fly switch method (reload of lang)
|
||||
*********************************************************************/
|
||||
|
||||
// TODO: default path change to <base>/lang/LC_MESSAGES/domain.encoding.mo
|
||||
// for example: lang: ja_JP.UTF-8, domain: admin
|
||||
// <base>/ja_JP/LC_MESSAGES/admin.UTF-8.mo
|
||||
// OLD: includes/lang/admin/ja_utf8.mo
|
||||
// NEW: includes/lang/ja_JP/LC_MESSAGES/admin.UTF-8.mo
|
||||
// or fallback: includes/lang/ja/LC_MESSAGES/admin.UTF-8.mo
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Language;
|
||||
@@ -106,12 +113,30 @@ class L10n
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set language
|
||||
* @return string current set language string
|
||||
*/
|
||||
public function __getLang(): string
|
||||
{
|
||||
return $this->lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set mo file
|
||||
* @return string current set mo language file
|
||||
*/
|
||||
public function __getMoFile(): string
|
||||
{
|
||||
return $this->mofile;
|
||||
}
|
||||
|
||||
/**
|
||||
* translates a string and returns translated text
|
||||
* @param string $text text to translate
|
||||
* @return string translated text
|
||||
*/
|
||||
public function __($text): string
|
||||
public function __(string $text): string
|
||||
{
|
||||
return $this->l10n->translate($text);
|
||||
}
|
||||
@@ -121,7 +146,7 @@ class L10n
|
||||
* @param string $text text to translate
|
||||
* @return void has no return
|
||||
*/
|
||||
public function __e($text): void
|
||||
public function __e(string $text): void
|
||||
{
|
||||
echo $this->l10n->translate($text);
|
||||
}
|
||||
@@ -129,33 +154,46 @@ class L10n
|
||||
// Return the plural form.
|
||||
/**
|
||||
* Return the plural form.
|
||||
* @param string $single string for single word
|
||||
* @param string $plural string for plural word
|
||||
* @param string $number number value
|
||||
* @return string translated plural string
|
||||
* @param string $single string for single word
|
||||
* @param string $plural string for plural word
|
||||
* @param int|float $number number value
|
||||
* @return string translated plural string
|
||||
*/
|
||||
public function __ngettext($single, $plural, $number)
|
||||
public function __n(string $single, string $plural, $number): string
|
||||
{
|
||||
return $this->l10n->ngettext($single, $plural, $number);
|
||||
}
|
||||
|
||||
// alias functions to mimic gettext calls
|
||||
|
||||
/**
|
||||
* get current set language
|
||||
* @return string current set language string
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
public function __getLang()
|
||||
public function gettext(string $text): string
|
||||
{
|
||||
return $this->lang;
|
||||
return $this->__($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* get current set mo file
|
||||
* @return string current set mo language file
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $single
|
||||
* @param string $plural
|
||||
* @param int|float $number
|
||||
* @return string
|
||||
*/
|
||||
public function __getMoFile()
|
||||
public function ngettext(string $single, string $plural, $number): string
|
||||
{
|
||||
return $this->mofile;
|
||||
return $this->__n($single, $plural, $number);
|
||||
}
|
||||
|
||||
// TODO: dgettext(string $domain, string $message): string
|
||||
// TODO: dngettext(string $domain, string $singular, string $plural, int $count): string
|
||||
// TODO: dcgettext(string $domain, string $message, int $category): string
|
||||
// TODO: dcngettext(string $domain, string $singular, string $plural, int $count, int $category): string
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -286,6 +286,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
|
||||
/** @var string */
|
||||
public $lang_short;
|
||||
/** @var string */
|
||||
public $domain;
|
||||
/** @var string */
|
||||
public $encoding;
|
||||
// language
|
||||
/** @var \CoreLibs\Language\L10n */
|
||||
@@ -299,10 +301,13 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
|
||||
* construct form generator
|
||||
* @param array<mixed> $db_config db config array
|
||||
* @param \CoreLibs\Debug\Logging|null $log Logging class
|
||||
* @param \CoreLibs\Language\L10n|null $l10n l10n language class
|
||||
* if null, auto set
|
||||
*/
|
||||
public function __construct(
|
||||
array $db_config,
|
||||
\CoreLibs\Debug\Logging $log = null
|
||||
\CoreLibs\Debug\Logging $log = null,
|
||||
?\CoreLibs\Language\L10n $l10n = null
|
||||
) {
|
||||
global $table_arrays;
|
||||
// replace any non valid variable names
|
||||
@@ -310,7 +315,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
|
||||
$this->my_page_name = str_replace(['.'], '_', System::getPageName(System::NO_EXTENSION));
|
||||
$this->setLangEncoding();
|
||||
// init the language class
|
||||
$this->l = new \CoreLibs\Language\L10n($this->lang);
|
||||
$this->l = $l10n ?? new \CoreLibs\Language\L10n($this->lang);
|
||||
// load config array
|
||||
// get table array definitions for current page name
|
||||
|
||||
@@ -452,27 +457,13 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
|
||||
*/
|
||||
private function setLangEncoding(): void
|
||||
{
|
||||
// just emergency fallback for language
|
||||
// set encoding
|
||||
if (isset($_SESSION['DEFAULT_CHARSET'])) {
|
||||
$this->encoding = $_SESSION['DEFAULT_CHARSET'];
|
||||
} else {
|
||||
$this->encoding = DEFAULT_ENCODING;
|
||||
}
|
||||
// gobal override
|
||||
if (isset($GLOBALS['OVERRIDE_LANG'])) {
|
||||
$this->lang = $GLOBALS['OVERRIDE_LANG'];
|
||||
} elseif (isset($_SESSION['DEFAULT_LANG'])) {
|
||||
// session (login)
|
||||
$this->lang = $_SESSION['DEFAULT_LANG'];
|
||||
} else {
|
||||
// mostly default SITE LANG or DEFAULT LANG
|
||||
$this->lang = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG;
|
||||
}
|
||||
// create the char lang encoding
|
||||
$this->lang_short = substr($this->lang, 0, 2);
|
||||
// set the language folder
|
||||
$this->lang_dir = BASE . INCLUDES . LANG . CONTENT_PATH;
|
||||
list (
|
||||
$this->encoding,
|
||||
$this->lang,
|
||||
$this->lang_short,
|
||||
$this->domain,
|
||||
$this->lang_dir
|
||||
) = \CoreLibs\Language\GetSettings::setLangEncoding();
|
||||
}
|
||||
|
||||
// PUBLIC METHODS |=================================================>
|
||||
|
||||
@@ -34,6 +34,8 @@ class SmartyExtend extends \Smarty
|
||||
/** @var string */
|
||||
public $lang_short;
|
||||
/** @var string */
|
||||
public $domain;
|
||||
/** @var string */
|
||||
public $encoding;
|
||||
// page name
|
||||
/** @var string */
|
||||
@@ -152,8 +154,10 @@ class SmartyExtend extends \Smarty
|
||||
* constructor class, just sets the language stuff
|
||||
* calls L10 for pass on internaly in smarty
|
||||
* also registers the getvar caller plugin
|
||||
* @param \CoreLibs\Language\L10n|null $l10n l10n language class
|
||||
* if null, auto set
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(?\CoreLibs\Language\L10n $l10n = null)
|
||||
{
|
||||
// call basic smarty
|
||||
// or Smarty::__construct();
|
||||
@@ -161,7 +165,7 @@ class SmartyExtend extends \Smarty
|
||||
// set lang vars
|
||||
$this->setLangEncoding();
|
||||
// iinit lang
|
||||
$this->l10n = new \CoreLibs\Language\L10n($this->lang);
|
||||
$this->l10n = $l10n ?? new \CoreLibs\Language\L10n($this->lang);
|
||||
// Smarty 3.x
|
||||
// $this->registerPlugin('modifier', 'getvar', [&$this, 'get_template_vars']);
|
||||
$this->registerPlugin('modifier', 'getvar', [&$this, 'getTemplateVars']);
|
||||
@@ -185,29 +189,13 @@ class SmartyExtend extends \Smarty
|
||||
*/
|
||||
private function setLangEncoding(): void
|
||||
{
|
||||
// just emergency fallback for language
|
||||
// set encoding
|
||||
if (!empty($_SESSION['DEFAULT_CHARSET'])) {
|
||||
$this->encoding = $_SESSION['DEFAULT_CHARSET'];
|
||||
} else {
|
||||
$this->encoding = DEFAULT_ENCODING;
|
||||
}
|
||||
// gobal override
|
||||
if (!empty($GLOBALS['OVERRIDE_LANG'])) {
|
||||
$this->lang = $GLOBALS['OVERRIDE_LANG'];
|
||||
} elseif (!empty($_SESSION['DEFAULT_LANG'])) {
|
||||
// session (login)
|
||||
$this->lang = $_SESSION['DEFAULT_LANG'];
|
||||
} else {
|
||||
// mostly default SITE LANG or DEFAULT LANG
|
||||
$this->lang = defined('SITE_LANG') && !empty(SITE_LANG) ?
|
||||
SITE_LANG :
|
||||
DEFAULT_LANG;
|
||||
}
|
||||
// create the char lang encoding
|
||||
$this->lang_short = substr($this->lang, 0, 2);
|
||||
// set the language folder
|
||||
$this->lang_dir = BASE . INCLUDES . LANG . CONTENT_PATH;
|
||||
list (
|
||||
$this->encoding,
|
||||
$this->lang,
|
||||
$this->lang_short,
|
||||
$this->domain,
|
||||
$this->lang_dir
|
||||
) = \CoreLibs\Language\GetSettings::setLangEncoding();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user