Compare commits

...

1 Commits

Author SHA1 Message Date
Clemens Schwaighofer
fed67e990d Update Language\L10n class and drop all legacy code
new Langauge\GetLocale::setLocale() for getting new type lang info from
session, etc
L10n class call chnage of parameters:
NEW: locale, domain, path
OLD: locale, path, domain, legacy(bool)

Temporary auto detect for possible path/domain switch if domain value
has slash inside

Rename all local files to names matching locale folder
en_US -> en

Delete lang folders with symlinks as they are no longer used

Update all header files and class Backend\Admin, ACL\Login,
Output\Form\Generate, Template\SmartyExtend with new language order:
call ::setLocale() afer login class

Update missing test translation strings in all po files

Update phpUnit tests to match all new changes
2022-04-15 13:29:41 +09:00
37 changed files with 889 additions and 645 deletions

View File

@@ -8,13 +8,13 @@
msgid ""
msgstr ""
"Project-Id-Version: en_US.UTF-8 LC_MESSAGES admin\n"
"Project-Id-Version: en.UTF-8 LC_MESSAGES admin\n"
"Report-Msgid-Bugs-To: clemens.schwaighofer@egplusww.com\n"
"POT-Creation-Date: 2018-03-28 10:40+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: clemens.schwaighofer@egplusww.co\n"
"Language-Team: E-GRAPHICS COMMUNICATIONS Japan <info.jp@egplusww.com>\n"
"Language: en_US\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@@ -32,20 +32,20 @@ msgstr "OUTPUT TEST ADMIN EN"
# testing multi
msgid "single"
msgid_plural "multi"
msgstr[0] "Multi Admin en_US 0"
msgstr[1] "Multi Admin en_US 1"
msgstr[2] "Multi Admin en_US 2"
msgstr[0] "Multi Admin en 0"
msgstr[1] "Multi Admin en 1"
msgstr[2] "Multi Admin en 2"
msgctxt "month name"
msgid "May"
msgstr "May Admin en_US"
msgstr "May Admin en"
msgctxt "month name"
msgid "single"
msgid_plural "multi"
msgstr[0] "Multi Admin month en_US 0"
msgstr[1] "Multi Admin month en_US 1"
msgstr[2] "Multi Admin month en_US 2"
msgstr[0] "Multi Admin month en 0"
msgstr[1] "Multi Admin month en 1"
msgstr[2] "Multi Admin month en 2"
msgid "I should be translated"
msgstr "I should be translated: I WAS TRANSLATED"

View File

@@ -2,13 +2,13 @@
msgid ""
msgstr ""
"Project-Id-Version: en_US.UTF-8 LC_MESSAGES frontend\n"
"Project-Id-Version: en.UTF-8 LC_MESSAGES frontend\n"
"Report-Msgid-Bugs-To: clemens.schwaighofer@egplusww.com\n"
"POT-Creation-Date: 2018-03-28 10:40+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: clemens.schwaighofer@egplusww.co\n"
"Language-Team: E-GRAPHICS COMMUNICATIONS Japan <info.jp@egplusww.com>\n"
"Language: en_US\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@@ -19,6 +19,6 @@ msgstr "OUTPUT TEST FRONTEND EN"
msgid "single"
msgid_plural "multi"
msgstr[0] "Multi frontend en_US 0"
msgstr[1] "Multi frontend en_US 1"
msgstr[2] "Multi frontend en_US 2"
msgstr[0] "Multi frontend en 0"
msgstr[1] "Multi frontend en 1"
msgstr[2] "Multi frontend en 2"

View File

@@ -8,7 +8,7 @@
msgid ""
msgstr ""
"Project-Id-Version: ja_JP.UTF-8 LC_MESSAGES admin\n"
"Project-Id-Version: ja.UTF-8 LC_MESSAGES admin\n"
"Report-Msgid-Bugs-To: clemens.schwaighofer@egplusww.com\n"
"POT-Creation-Date: 2018-03-28 10:40+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
@@ -69,9 +69,20 @@ msgstr "OUTPUT TEST ADMIN JA"
# testing multi
msgid "single"
msgid_plural "multi"
msgstr[0] "Multi Admin ja_JP 0"
msgstr[1] "Multi Admin ja_JP 1"
msgstr[2] "Multi Admin ja_JP 2"
msgstr[0] "Multi Admin ja 0"
msgstr[1] "Multi Admin ja 1"
msgstr[2] "Multi Admin ja 2"
msgctxt "month name"
msgid "May"
msgstr "May Admin ja"
msgctxt "month name"
msgid "single"
msgid_plural "multi"
msgstr[0] "Multi Admin month ja 0"
msgstr[1] "Multi Admin month ja 1"
msgstr[2] "Multi Admin month ja 2"
# login string
msgid "Hello %s"

View File

@@ -2,7 +2,23 @@
msgid ""
msgstr ""
"Project-Id-Version: ja.UTF-8 LC_MESSAGES frontend\n"
"Report-Msgid-Bugs-To: clemens.schwaighofer@egplusww.com\n"
"POT-Creation-Date: 2018-03-28 10:40+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: clemens.schwaighofer@egplusww.com\n"
"Language-Team: E-GRAPHICS COMMUNICATIONS Japan <info.jp@egplusww.com>\n"
"Language: ja\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
msgid "INPUT TEST"
msgstr "OUTPUT TEST FRONTEND JA"
msgid "single"
msgid_plural "multi"
msgstr[0] "Multi frontend ja 0"
msgstr[1] "Multi frontend ja 1"
msgstr[2] "Multi frontend ja 2"

View File

@@ -0,0 +1,291 @@
<?php // phpcs:disable Generic.Files.LineLength
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
/**
* Test class for Language\GetLocale
*
* @coversDefaultClass \CoreLibs\Language\GetLocale
* @testdox \CoreLibs\Language\GetLocale method tests
*/
final class CoreLibsLanguageGetLocaleTest extends TestCase
{
/**
* set all constant variables that must be set before call
*
* @return void
*/
public static function setUpBeforeClass(): void
{
// default web page encoding setting
define('DEFAULT_ENCODING', 'UTF-8');
// default lang + encoding
define('DEFAULT_LOCALE', 'en_US.UTF-8');
// site
define('SITE_ENCODING', DEFAULT_ENCODING);
define('SITE_LOCALE', DEFAULT_LOCALE);
// just set
define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR);
define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR);
define('LOCALE', 'locale' . DIRECTORY_SEPARATOR);
define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR);
// array session
$_SESSION = [];
global $_SESSION;
}
/**
* all the test data
*
* @return array
*/
public function setLocaleProvider(): array
{
return [
// 0: locale
// 1: domain
// 2: encoding
// 3: path
// 4: SESSION: DEFAULT_LOCALE
// 5: SESSION: DEFAULT_CHARSET
// 6: expected array
'no params, all default constants' => [
// lang, domain, encoding, path
null, null, null, null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'en_US.UTF-8',
'lang' => 'en_US',
'domain' => 'frontend',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
'no params, session charset and lang' => [
// lang, domain, encoding, path
null, null, null, null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
'ja_JP', 'UTF-8',
// return array
[
'locale' => 'ja_JP',
'lang' => 'ja_JP',
'domain' => 'frontend',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
'no params, session charset and lang short' => [
// lang, domain, encoding, path
null, null, null, null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
'ja', 'UTF-8',
// return array
[
'locale' => 'ja',
'lang' => 'ja',
'domain' => 'frontend',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
// param lang (no sessions)
'locale param only, no sessions' => [
// lang, domain, encoding, path
'ja.UTF-8', null, null, null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'ja.UTF-8',
'lang' => 'ja',
'domain' => 'frontend',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
// different locale setting
'locale complex param only, no sessions' => [
// lang, domain, encoding, path
'ja_JP.SJIS', null, null, null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'ja_JP.SJIS',
'lang' => 'ja_JP',
'domain' => 'frontend',
'encoding' => 'SJIS',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
// param lang and domain (no override)
'locale, domain params, no sessions' => [
// lang, domain, encoding, path
'ja.UTF-8', 'admin', null, null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'ja.UTF-8',
'lang' => 'ja',
'domain' => 'admin',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
// param lang and domain (no override)
'locale, domain, encoding params, no sessions' => [
// lang, domain, encoding, path
'ja.UTF-8', 'admin', 'UTF-8', null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'ja.UTF-8',
'lang' => 'ja',
'domain' => 'admin',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
// lang, domain, path (no override)
'locale, domain and path, no sessions' => [
// lang, domain, encoding, path
'ja.UTF-8', 'admin', '', __DIR__ . '/locale_other/',
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'ja.UTF-8',
'lang' => 'ja',
'domain' => 'admin',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?locale_other\/$/",
],
],
// all params set (no override)
'all parameter, no sessions' => [
// lang, domain, encoding, path
'ja', 'admin', 'UTF-8', __DIR__ . '/locale_other/',
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'ja',
'lang' => 'ja',
'domain' => 'admin',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?locale_other\/$/",
],
],
// param lang and domain (no override)
'long locale, domain, encoding params, no sessions' => [
// lang, domain, encoding, path
'de_CH.UTF-8@euro', 'admin', 'UTF-8', null,
// SESSION DEFAULT_LOCALE, SESSION: DEFAULT_CHARSET
null, null,
// return array
[
'locale' => 'de_CH.UTF-8@euro',
'lang' => 'de_CH',
'domain' => 'admin',
'encoding' => 'UTF-8',
'path' => "/^\/(.*\/)?includes\/locale\/$/",
],
],
// TODO invalid params (bad path) (no override)
// TODO param calls, but with override set
];
}
/**
* Undocumented function
*
* @covers ::setLocale
* @dataProvider setLocaleProvider
* @testdox lang settings lang $language, domain $domain, encoding $encoding, path $path; session lang: $SESSION_DEFAULT_LOCALE, session char: $SESSION_DEFAULT_CHARSET [$_dataName]
*
* @return void
*/
public function testsetLocale(
?string $language,
?string $domain,
?string $encoding,
?string $path,
?string $SESSION_DEFAULT_LOCALE,
?string $SESSION_DEFAULT_CHARSET,
array $expected,
): void {
$return_lang_settings = [];
global $_SESSION;
// set override
if ($SESSION_DEFAULT_LOCALE !== null) {
$_SESSION['DEFAULT_LOCALE'] = $SESSION_DEFAULT_LOCALE;
}
if ($SESSION_DEFAULT_CHARSET !== null) {
$_SESSION['DEFAULT_CHARSET'] = $SESSION_DEFAULT_CHARSET;
}
// function call
if ($language === null && $domain === null && $encoding === null && $path === null) {
$return_lang_settings = \CoreLibs\Language\GetLocale::setLocale();
} elseif ($language !== null && $domain === null && $encoding === null && $path === null) {
$return_lang_settings = \CoreLibs\Language\GetLocale::setLocale(
$language
);
} elseif ($language !== null && $domain !== null && $encoding === null && $path === null) {
$return_lang_settings = \CoreLibs\Language\GetLocale::setLocale(
$language,
$domain
);
} elseif ($language !== null && $domain !== null && $encoding !== null && $path === null) {
$return_lang_settings = \CoreLibs\Language\GetLocale::setLocale(
$language,
$domain,
$encoding
);
} else {
$return_lang_settings = \CoreLibs\Language\GetLocale::setLocale(
$language,
$domain,
$encoding,
$path
);
}
// print "RETURN: " . print_r($return_lang_settings, true) . "\n";
foreach (
[
'locale', 'lang', 'domain', 'encoding', 'path'
] as $key
) {
$value = $expected[$key];
if (strpos($value, "/") === 0) {
// this is regex
$this->assertMatchesRegularExpression(
$value,
$return_lang_settings[$key],
'assert regex failed for ' . $key
);
} else {
// assert equal
$this->assertEquals(
$value,
$return_lang_settings[$key],
'assert equal failed for ' . $key
);
}
}
// unset all vars
$_SESSION = [];
unset($GLOBALS['OVERRIDE_LANG']);
}
}
// __END__

View File

@@ -87,44 +87,20 @@ final class CoreLibsLanguageL10nTest extends TestCase
{
return [
// 0: locale
// 1: path
// 2: domain
// 3: legacy load (Default true)
// 1: domain
// 2: encoding
// 3: path
// 4: locale expected
// 5: domain exepcted
// 6: context (null for none)
// 7: test string in
// 8: test translated
'legacy load en' => [
'en_utf8',
null,
null,
null,
//
'en_utf8',
'',
//
null,
'Original',
'Translated frontend en_US',
],
'legacy load ja' => [
'ja_utf8',
null,
null,
null,
'ja_utf8',
'',
null,
'Original',
'Translated frontend ja_JP',
],
// new style load
'gettext load en' => [
'en_US.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
false,
__DIR__ . 'includes/locale/',
//
'en_US.UTF-8',
'frontend',
null,
@@ -133,9 +109,9 @@ final class CoreLibsLanguageL10nTest extends TestCase
],
'gettext load en' => [
'en_US.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
false,
__DIR__ . 'includes/locale/',
//
'en_US.UTF-8',
'frontend',
'context',
@@ -144,32 +120,33 @@ final class CoreLibsLanguageL10nTest extends TestCase
],
'gettext load ja' => [
'ja_JP.UTF-8',
__DIR__ . 'includes/locale/',
'admin',
false,
__DIR__ . 'includes/locale/',
//
'ja_JP.UTF-8',
'admin',
null,
'Original',
'Translated admin ja_JP',
],
// null set locale legacy
'empty load legacy' => [
null,
null,
null,
null,
'',
'',
null,
'Original',
// mixed path and domain
'mixed path and domain' => [
'en_US.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
//
'en_US.UTF-8',
'frontend',
'context',
'Original',
'Original context frontend en_US',
],
// null set
'empty load new ' => [
'',
'',
'',
false,
//
'',
'',
null,
@@ -187,9 +164,8 @@ final class CoreLibsLanguageL10nTest extends TestCase
* @testdox check l10n init with Locale $locale, Path $path, Domain $domain, Legacy: $legacy with $context [$_dataName]
*
* @param string|null $locale
* @param string|null $path
* @param string|null $domain
* @param bool|null $legacy
* @param string|null $path
* @param string $locale_expected
* @param string $domain_expected
* @param ?string $context
@@ -199,9 +175,8 @@ final class CoreLibsLanguageL10nTest extends TestCase
*/
public function testL10nObject(
?string $locale,
?string $path,
?string $domain,
?bool $legacy,
?string $path,
string $locale_expected,
string $domain_expected,
?string $context,
@@ -210,14 +185,12 @@ final class CoreLibsLanguageL10nTest extends TestCase
): void {
if ($locale === null) {
$l10n = new \CoreLibs\Language\L10n();
} elseif ($path === null) {
$l10n = new \CoreLibs\Language\L10n($locale);
} elseif ($domain === null) {
$l10n = new \CoreLibs\Language\L10n($locale, $path);
} elseif ($legacy === null) {
$l10n = new \CoreLibs\Language\L10n($locale, $path, $domain);
$l10n = new \CoreLibs\Language\L10n($locale);
} elseif ($path === null) {
$l10n = new \CoreLibs\Language\L10n($locale, $domain);
} else {
$l10n = new \CoreLibs\Language\L10n($locale, $path, $domain, $legacy);
$l10n = new \CoreLibs\Language\L10n($locale, $domain, $path);
}
// print "LOC: " . $locale . ", " . $l10n->getLocale() . ", " . $locale_expected . "\n";
// print "MO: " . $l10n->getMoFile() . "\n";
@@ -259,94 +232,64 @@ final class CoreLibsLanguageL10nTest extends TestCase
{
return [
// 0: locale
// 1: path
// 2: domain
// 3: legacy flag
// 1: domain
// 2: path
// 3: load error
// 4: input string to translated
// 5: expected locale
// 6: expected domain
// 7: expected translation
// 8: change locale
// 9: change path
// 10: change domain
// 11: legacy flag
// 9: change domain
// 10: change path
// 11: change load error
// 12: expected locale
// 13: expected domain
// 14: expected translation
'legacy load and change (en->ja)' => [
// set 0-3
'en_utf8',
null,
null,
null,
// status 4
false,
// to translate 5
'Original',
// check setter 6-8
'en_utf8',
'',
'Translated frontend en_US',
// set new 9-12
'ja_utf8',
null,
null,
null,
// status new 13
false,
// check new setter 14-16
'ja_utf8',
'',
'Translated frontend ja_JP',
],
'load and change (en->ja)' => [
// set 0-3
// set 0-2
'en_US.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
__DIR__ . 'includes/locale/',
// status 3
false,
// status 4
false,
// to translate 5
// to translate 4
'Original',
// check setter 6-8
// check setter 5-7
'en_US.UTF-8',
'frontend',
'Translated frontend en_US',
// set new 9-12
// set new 8-10
'ja_JP.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
__DIR__ . 'includes/locale/',
// status new 11
false,
// status new 13
false,
// check new setter 14-16
// check new setter 12-14
'ja_JP.UTF-8',
'frontend',
'Translated frontend ja_JP',
],
'empty load and change to en' => [
// set 0-3
// set 0-2
'',
'',
'',
// status 3
false,
// status 4
false,
// to translate 5
// to translate 4
'Original',
// check setter 6-8
// check setter 5-7
'',
'',
'Original',
// set new 9-12
// set new 8-10
'en_US.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
__DIR__ . 'includes/locale/',
// status new 11
false,
// status new 13
false,
// check new setter 14-16
// check new setter 12-14
'en_US.UTF-8',
'frontend',
'Translated frontend en_US',
@@ -363,18 +306,16 @@ final class CoreLibsLanguageL10nTest extends TestCase
* @testdox change locale from $locale and domain $domain to locale $locale_new and domain $domain_new [$_dataName]
*
* @param string|null $locale
* @param string|null $path
* @param string|null $domain
* @param bool|null $legacy
* @param string|null $path
* @param bool $load_error
* @param string $original
* @param string $locale_expected_a
* @param string $domain_expected_a
* @param string $translated_a
* @param string|null $locale_new
* @param string|null $path_new
* @param string|null $domain_new
* @param bool|null $legacy_new
* @param string|null $path_new
* @param bool $load_error_new
* @param string $locale_expected_b
* @param string $domain_expected_b
@@ -382,41 +323,37 @@ final class CoreLibsLanguageL10nTest extends TestCase
* @return void
*/
public function testGetTranslator(
// 0-3
// 0-2
?string $locale,
?string $path,
?string $domain,
?bool $legacy,
// 4
?string $path,
// 3
bool $load_error,
// 5
// 4
string $original,
// 6-8
// 5-7
string $locale_expected_a,
string $domain_expected_a,
string $translated_a,
// 9-12
// 8-10
?string $locale_new,
?string $path_new,
?string $domain_new,
?bool $legacy_new,
// 13
?string $path_new,
// 11
bool $load_error_new,
// 14-16
// 12-14
string $locale_expected_b,
string $domain_expected_b,
string $translated_b,
): void {
if ($locale === null) {
$l10n = new \CoreLibs\Language\L10n();
} elseif ($path === null) {
$l10n = new \CoreLibs\Language\L10n($locale);
} elseif ($domain === null) {
$l10n = new \CoreLibs\Language\L10n($locale, $path);
} elseif ($legacy === null) {
$l10n = new \CoreLibs\Language\L10n($locale, $path, $domain);
$l10n = new \CoreLibs\Language\L10n($locale);
} elseif ($path === null) {
$l10n = new \CoreLibs\Language\L10n($locale, $domain);
} else {
$l10n = new \CoreLibs\Language\L10n($locale, $path, $domain, $legacy);
$l10n = new \CoreLibs\Language\L10n($locale, $domain, $path);
}
// print "LOC: " . $locale . ", " . $l10n->getLocale() . ", " . $locale_expected . "\n";
// status check
@@ -441,67 +378,39 @@ final class CoreLibsLanguageL10nTest extends TestCase
'Translated string init assert failed'
);
// do reload with legacy l10nReloadMOfile IF legacy is null or true
// use getTranslator if legacy is false
if ($legacy === null || $legacy === true) {
// there is no null/empty locale allowed directly
// if empty will set previous one
if ($path_new === null) {
$ret_status = $l10n->l10nReloadMOfile($locale_new);
} elseif ($domain_new === null) {
$ret_status = $l10n->l10nReloadMOfile($locale_new, $path_new);
} elseif ($legacy_new === null) {
$ret_status = $l10n->l10nReloadMOfile($locale_new, $path_new, $domain_new);
} else {
$ret_status = $l10n->l10nReloadMOfile($locale_new, $path_new, $domain_new, $legacy_new);
}
// status check
$this->assertEquals(
$load_error_new,
$l10n->getLoadError(),
'Legacy method load error change check'
);
// retun status check is inverted to load error check
$this->assertEquals(
$load_error_new ? false : true,
$ret_status,
'Legacy return load error change check'
);
// switch
if ($locale_new === null) {
$translator = $l10n->getTranslator();
} elseif ($domain_new === null) {
$translator = $l10n->getTranslator($locale_new);
} elseif ($path_new === null) {
$translator = $l10n->getTranslator($locale_new, $domain_new);
} else {
if ($locale_new === null) {
$translator = $l10n->getTranslator();
} elseif ($path_new === null) {
$translator = $l10n->getTranslator($locale_new);
} elseif ($domain_new === null) {
$translator = $l10n->getTranslator($locale_new, $path_new);
} elseif ($legacy_new === null) {
$translator = $l10n->getTranslator($locale_new, $path_new, $domain_new);
} else {
$translator = $l10n->getTranslator($locale_new, $path_new, $domain_new, $legacy_new);
}
// status check
$this->assertEquals(
$load_error_new,
$l10n->getLoadError(),
'Translate method load error change check'
);
// check that returned is class GetTextReader and object
$this->assertIsObject(
$translator,
'translater class is object assert failed'
);
$this->assertInstanceOf(
'\CoreLibs\Language\Core\GetTextReader',
$translator,
'translator class is correct instance assert failed'
);
// translator class
$this->assertEquals(
$translated_b,
$translator->gettext($original),
'Translated string change assert failed from returned class'
);
$translator = $l10n->getTranslator($locale_new, $domain_new, $path_new);
}
// status check
$this->assertEquals(
$load_error_new,
$l10n->getLoadError(),
'Translate method load error change check'
);
// check that returned is class GetTextReader and object
$this->assertIsObject(
$translator,
'translater class is object assert failed'
);
$this->assertInstanceOf(
'\CoreLibs\Language\Core\GetTextReader',
$translator,
'translator class is correct instance assert failed'
);
// translator class
$this->assertEquals(
$translated_b,
$translator->gettext($original),
'Translated string change assert failed from returned class'
);
// new set check
$this->assertEquals(
$locale_expected_b,
@@ -520,16 +429,14 @@ final class CoreLibsLanguageL10nTest extends TestCase
);
}
// TODO: test all translation types
// __/gettext
// __n/ngettext
// TODO: domain based
// ->dgettext
// ->dngettext
// ->dpgettext
// ->dpngettext
/**
* Undocumented function
* for plural and plural context
*
* @return array
*/
@@ -579,9 +486,10 @@ final class CoreLibsLanguageL10nTest extends TestCase
}
/**
* Undocumented function
* plural and plural context
*
* @covers ::__n
* @covers ::__pn
* @dataProvider ngettextProvider
* @testdox plural string test for locale $locale and domain $domain with $context [$_dataName]
*
@@ -640,21 +548,39 @@ final class CoreLibsLanguageL10nTest extends TestCase
'en',
[
'en',
]
],
[
'lang' => 'en',
'country' => null,
'charset' => null,
'modifier' => null,
],
],
'en.UTF-8' => [
'en.UTF-8',
[
'en.UTF-8',
'en',
]
],
[
'lang' => 'en',
'country' => null,
'charset' => 'UTF-8',
'modifier' => null,
],
],
'en_US' => [
'en_US',
[
'en_US',
'en',
]
],
[
'lang' => 'en',
'country' => 'US',
'charset' => null,
'modifier' => null,
],
],
'en_US.UTF-8' => [
'en_US.UTF-8',
@@ -662,7 +588,13 @@ final class CoreLibsLanguageL10nTest extends TestCase
'en_US.UTF-8',
'en_US',
'en',
]
],
[
'lang' => 'en',
'country' => 'US',
'charset' => 'UTF-8',
'modifier' => null,
],
],
'en_US@subtext' => [
'en_US@subtext',
@@ -671,7 +603,13 @@ final class CoreLibsLanguageL10nTest extends TestCase
'en@subtext',
'en_US',
'en',
]
],
[
'lang' => 'en',
'country' => 'US',
'charset' => null,
'modifier' => 'subtext',
],
],
'en_US.UTF-8@subtext' => [
'en_US.UTF-8@subtext',
@@ -682,7 +620,13 @@ final class CoreLibsLanguageL10nTest extends TestCase
'en_US.UTF-8',
'en_US',
'en',
]
],
[
'lang' => 'en',
'country' => 'US',
'charset' => 'UTF-8',
'modifier' => 'subtext',
],
]
];
}
@@ -695,16 +639,24 @@ final class CoreLibsLanguageL10nTest extends TestCase
* @testdox check $locale [$_dataName]
*
* @param string $locale
* @param array $expected
* @param array $expected_list
* @param array $expected_detail
* @return void
*/
public function testListLocales(string $locale, array $expected): void
public function testListLocales(string $locale, array $expected_list, array $expected_detail): void
{
$locale_detail = \CoreLibs\Language\L10n::parseLocale($locale);
$this->assertEquals(
$expected_detail,
$locale_detail,
'Parse local assert failed'
);
$locale_list = \CoreLibs\Language\L10n::listLocales($locale);
// print "LOCALES: " . print_r($locale_list, true) . "\n";
$this->assertEquals(
$expected,
$locale_list
$expected_list,
$locale_list,
'List locale assert failed'
);
}
@@ -971,23 +923,23 @@ final class CoreLibsLanguageL10nTest extends TestCase
{
return [
// 0: lang/locale
// 1: path
// 2: domain
// 1: domain
// 2: path
// 3: encoding
// 4: string
// 5: translated string
'standard en' => [
'en_US.UTF-8',
__DIR__ . 'includes/locale/',
'frontend',
__DIR__ . 'includes/locale/',
'UTF-8',
'Original',
'Translated frontend en_US',
],
'standard ja' => [
'ja_JP.UTF-8',
__DIR__ . 'includes/locale/',
'admin',
__DIR__ . 'includes/locale/',
'UTF-8',
'Original',
'Translated admin ja_JP',
@@ -1008,8 +960,8 @@ final class CoreLibsLanguageL10nTest extends TestCase
* @dataProvider functionsProvider
* @testdox check functions with locale $locale and domain $domain [$_dataName]
* @param string $locale
* @param string $path
* @param string $domain
* @param string $path
* @param string $encoding
* @param string $original
* @param string $translated
@@ -1017,8 +969,8 @@ final class CoreLibsLanguageL10nTest extends TestCase
*/
public function testFunctions(
string $locale,
string $path,
string $domain,
string $path,
string $encoding,
string $original,
string $translated

View File

@@ -1 +0,0 @@
../../locale/en_US/LC_MESSAGES/admin.mo

View File

@@ -1 +0,0 @@
../../locale/ja_JP/LC_MESSAGES/admin.mo

View File

@@ -1 +0,0 @@
../../locale/en_US/LC_MESSAGES/frontend.mo

View File

@@ -1 +0,0 @@
../../locale/ja_JP/LC_MESSAGES/frontend.mo

View File

@@ -43,7 +43,13 @@ $log = new CoreLibs\Debug\Logging([
]);
// db config with logger
$db = new CoreLibs\DB\IO(DB_CONFIG, $log);
$backend = new CoreLibs\Admin\Backend($db, $log);
$locale = \CoreLibs\Language\GetLocale::setLocale();
$l10n = new \CoreLibs\Language\L10n(
$locale['locale'],
$locale['domain'],
$locale['path'],
);
$backend = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale);
print "<!DOCTYPE html>";
print "<html><head><title>TEST CLASS: ADMIN BACKEND</title><head>";
@@ -51,7 +57,7 @@ print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
// set acl, from eg login acl
print "SETACL[]: " . $backend->setACL([]) . "<br>";
print "SETACL[]: " . $backend->setACL(['EMPTY' => 'EMPTY']) . "<br>";
print "ADBEDITLOG: " . $backend->adbEditLog('CLASSTEST-ADMIN', 'Some info stirng') . "<br>";
print "ADBTOPMENU(0): " . \CoreLibs\Debug\Support::printAr($backend->adbTopMenu()) . "<br>";
print "ADBMSG: " . $backend->adbMsg('info', 'Message: %1$d', [1]) . "<br>";

View File

@@ -19,8 +19,6 @@ ob_start();
// basic class test file
define('USE_DATABASE', false);
// init language
$lang = 'en_utf8';
// sample config
require 'config.php';
// set session name
@@ -37,69 +35,43 @@ print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
use CoreLibs\Language\L10n;
$string = 'INPUT TEST';
echo "<br><b>LEGACY TEST</b><br>";
$lang = 'en_utf8';
$l = new CoreLibs\Language\L10n($lang);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "LANGUAGE FILE: " . $l->getMoFile() . "<br>";
echo "LOAD ERROR: " . $l->getLoadError() . "<br>";
echo "INPUT TEST: " . $string . " => " . $l->__($string) . "<br>";
$single_string = 'single';
$multi_string = 'multi';
for ($n = 0; $n <= 3; $n++) {
echo "MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. $l->__n($single_string, $multi_string, $n) . "<br>";
}
$context = "month name";
$context_string = "May";
echo "CONTEXT TRANSLATION: " . $context_string . " => " . $l->__p($context, $context_string) . "<br>";
$single_string = 'single';
$multi_string = 'multi';
for ($n = 0; $n <= 3; $n++) {
echo "CONTEXT MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. $l->__pn($context, $single_string, $multi_string, $n) . "<br>";
}
// switch to other language
$lang = 'ja_utf8';
$l->l10nReloadMOfile($lang);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "LANGUAGE FILE: " . $l->getMoFile() . "<br>";
echo "LOAD ERROR: " . $l->getLoadError() . "<br>";
echo "INPUT TEST: " . $string . " => " . $l->__($string) . "<br>";
// switch to non existing language
$lang = 'tr_utf8';
$l->l10nReloadMOfile($lang);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "LANGUAGE FILE: " . $l->getMoFile() . "<br>";
echo "LOAD ERROR: " . $l->getLoadError() . "<br>";
echo "INPUT TEST: " . $string . " => " . $l->__($string) . "<br>";
use CoreLibs\Debug\Support;
echo "<br><b>LIST LOCALES</b><br>";
$locale = 'en_US.UTF-8';
$locales = CoreLibs\Language\L10n::listLocales($locale);
print "[" . $locale . "] LOCALES: " . CoreLibs\Debug\Support::printAr($locales) . "<br>";
print "[" . $locale . "] LOCALES: " . Support::printAr($locales) . "<br>";
$locale = 'en.UTF-8';
$locales = CoreLibs\Language\L10n::listLocales($locale);
print "[" . $locale . "] LOCALES: " . CoreLibs\Debug\Support::printAr($locales) . "<br>";
print "[" . $locale . "] LOCALES: " . Support::printAr($locales) . "<br>";
echo "<br><b>LOCALE INFO</b><br>";
$locale = 'en_US.UTF-8';
$locale_info = CoreLibs\Language\L10n::parseLocale($locale);
print "[" . $locale . "] INFO: " . Support::printAr($locale_info) . "<br>";
$locale = 'en.UTF-8';
$locale_info = CoreLibs\Language\L10n::parseLocale($locale);
print "[" . $locale . "] INFO: " . Support::printAr($locale_info) . "<br>";
echo "<br><b>AUTO DETECT</b><br>";
$get_locale = \CoreLibs\Language\GetLocale::setLocale();
print "[AUTO]: " . Support::printAr($get_locale) . "<br>";
$get_locale = \CoreLibs\Language\GetLocale::setLocale('en', 'foo', 'ISO-8895');
print "[OVERRIDE]: " . Support::printAr($get_locale) . "<br>";
// try to load non existing
echo "<br><b>NEW TYPE</b><br>";
// translate string
$string = 'INPUT TEST';
// new path test
$lang = 'ja';
$domain = 'admin';
$encoding = 'UTF-8';
$path = BASE . INCLUDES . LOCALE;
$l = new CoreLibs\Language\L10n($lang, $path, $domain, false);
// load direct
$l = new CoreLibs\Language\L10n($lang, $domain, $path);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "DOMAIN WANT/SET: " . $domain . '/' . $l->getDomain() . "<br>";
@@ -116,9 +88,45 @@ for ($n = 0; $n <= 3; $n++) {
echo "MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. $l->__n($single_string, $multi_string, $n) . "<br>";
}
$context = "month name";
$context_string = "May";
echo "CONTEXT TRANSLATION: " . $context_string . " => " . $l->__p($context, $context_string) . "<br>";
$single_string = 'single';
$multi_string = 'multi';
for ($n = 0; $n <= 3; $n++) {
echo "CONTEXT MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. $l->__pn($context, $single_string, $multi_string, $n) . "<br>";
}
// change domain
$domain = 'frontend';
$l->getTranslator('', $path, $domain);
$l->getTranslator('', $domain, $path);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "DOMAIN WANT/SET: " . $domain . '/' . $l->getDomain() . "<br>";
echo "LANGUAGE FILE: " . $l->getMoFile() . "<br>";
echo "CONTENT PATH: " . $l->getBaseContentPath() . "<br>";
echo "DOMAIN PATH: " . $l->getTextDomain($domain) . "<br>";
echo "BASE PATH: " . $l->getBaseLocalePath() . "<br>";
echo "LOAD ERROR: " . $l->getLoadError() . "<br>";
echo "INPUT TEST: " . $string . " => " . $l->__($string) . "<br>";
echo "TROUGH LOAD: " . $l->getTranslatorClass()->gettext($string) . "<br>";
// change language short type
$lang = 'en';
$domain = 'admin';
$l->getTranslator($lang, $domain, $path);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "DOMAIN WANT/SET: " . $domain . '/' . $l->getDomain() . "<br>";
echo "LANGUAGE FILE: " . $l->getMoFile() . "<br>";
echo "CONTENT PATH: " . $l->getBaseContentPath() . "<br>";
echo "DOMAIN PATH: " . $l->getTextDomain($domain) . "<br>";
echo "BASE PATH: " . $l->getBaseLocalePath() . "<br>";
echo "LOAD ERROR: " . $l->getLoadError() . "<br>";
echo "INPUT TEST: " . $string . " => " . $l->__($string) . "<br>";
echo "TROUGH LOAD: " . $l->getTranslatorClass()->gettext($string) . "<br>";
// chang to wrong language
$lang = 'tr';
$l->getTranslator($lang, $domain, $path);
echo "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "DOMAIN WANT/SET: " . $domain . '/' . $l->getDomain() . "<br>";

View File

@@ -41,7 +41,13 @@ $log = new CoreLibs\Debug\Logging([
]);
$db = new CoreLibs\DB\IO(DB_CONFIG, $log);
$login = new CoreLibs\ACL\Login($db, $log);
$backend = new CoreLibs\Admin\Backend($db, $log);
$locale = \CoreLibs\Language\GetLocale::setLocale();
$l10n = new \CoreLibs\Language\L10n(
$locale['locale'],
$locale['domain'],
$locale['path'],
);
$backend = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale);
$backend->db->dbInfo(true);
ob_end_flush();
@@ -79,9 +85,10 @@ print '<div><a href="class_test.smarty.php">Class Test: SMARTY</a></div>';
print '<div><a href="class_test.autoloader.php">Class Test: AUTOLOADER</a></div>';
print "<hr>";
print "L: " . CoreLibs\Debug\Support::printAr($locale) . "<br>";
// print all _ENV vars set
print "<div>READ _ENV ARRAY:</div>";
print "<pre>" . print_r(array_map('htmlentities', $_ENV), true) . "</pre>";
print CoreLibs\Debug\Support::printAr(array_map('htmlentities', $_ENV));
// set + check edit access id
$edit_access_id = 3;
if (is_object($login) && isset($login->acl['unit'])) {

View File

@@ -19,8 +19,6 @@ ob_start();
// basic class test file
define('USE_DATABASE', true);
// set language
$lang = 'en_utf8';
// sample config
require 'config.php';
// override ECHO ALL FALSE
@@ -43,8 +41,13 @@ $log = new CoreLibs\Debug\Logging([
'echo_all' => $ECHO_ALL ?? false,
'print_all' => $PRINT_ALL ?? false,
]);
$smarty = new CoreLibs\Template\SmartyExtend();
$l = new CoreLibs\Language\L10n($lang);
$locale = \CoreLibs\Language\GetLocale::setLocale();
$l10n = new \CoreLibs\Language\L10n(
$locale['locale'],
$locale['domain'],
$locale['path'],
);
$smarty = new CoreLibs\Template\SmartyExtend($l10n, $locale);
print "<!DOCTYPE html>";
print "<html><head><title>TEST CLASS: SMARTY</title><head>";
@@ -70,7 +73,7 @@ $smarty->setSmartyPaths();
// smarty test
$smarty->DATA['SMARTY_TEST'] = 'Test Data';
$smarty->DATA['TRANSLATE_TEST'] = $l->__('Are we translated?');
$smarty->DATA['TRANSLATE_TEST'] = $l10n->__('Are we translated?');
$smarty->DATA['TRANSLATE_TEST_SMARTY'] = $smarty->l10n->__('Are we translated?');
$smarty->DATA['replace'] = 'Replaced';
// variable variables

View File

@@ -1,77 +0,0 @@
<?php
/********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2005/07/19
* SHORT DESCRIPTION:
* preset date variables
* HISTORY:
*********************************************************************/
declare(strict_types=1);
/******
All moved to CoreLibs\Combined\DateTime
DAY_SHORT
DAY_LONG
MONTH_LONG
MONTH_SHORT
*******/
trigger_error(
'Date.VArs.php is deprecated. '
. 'Use CoreLibs\Combined\DateTime:: with upper case old variable name',
E_USER_DEPRECATED
);
$day_short = [
1 => 'Mon',
2 => 'Tue',
3 => 'Wed',
4 => 'Thu',
5 => 'Fri',
6 => 'Sat',
7 => 'Sun'
];
$day_long = [
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
];
// months
$month_long = [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
];
$month_short = [
1 => 'Jan',
2 => 'Feb',
3 => 'Mar',
4 => 'Apr',
5 => 'May',
6 => 'Jun',
7 => 'Jul',
8 => 'Aug',
9 => 'Sep',
10 => 'Oct',
11 => 'Nov',
12 => 'Dec'
];
// __END__

View File

@@ -72,10 +72,19 @@ if (
$db = new CoreLibs\DB\IO(DB_CONFIG, $log);
// login & page access check
$login = new CoreLibs\ACL\Login($db, $log);
// lang, path, domain
// pre auto detect language after login
$locale = \CoreLibs\Language\GetLocale::setLocale();
// set lang and pass to smarty/backend
$l10n = new \CoreLibs\Language\L10n(
$locale['locale'],
$locale['domain'],
$locale['path'],
);
// create smarty object
$smarty = new CoreLibs\Template\SmartyExtend();
$smarty = new CoreLibs\Template\SmartyExtend($l10n, $locale);
// create new Backend class with db and loger attached
$cms = new CoreLibs\Admin\Backend($db, $log);
$cms = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale);
// the menu show flag (what menu to show)
$cms->menu_show_flag = 'main';
// db info

View File

@@ -1,22 +0,0 @@
<?php
/********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2007/09/03
* SHORT DESCRIPTION:
* set paths & language variables
* HISTORY:
*********************************************************************/
declare(strict_types=1);
/******
NOTE THAT THIS INCLUDE IS OBSOLETE
USE THE BELOW FUNCTION CALL IN THE SCRIPT ITSELF
*******/
trigger_error('admin_set_paths.php is deprecated. Use SmartyExtended->setSmartyPaths();', E_USER_DEPRECATED);
if ($smarty) {
$smarty->setSmartyPaths();
}
// __END__

View File

@@ -1,23 +0,0 @@
<?php
/********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2005/07/12
* SHORT DESCRIPTION:
* default smarty vars, and create output template for smarty
* HISTORY:
*********************************************************************/
declare(strict_types=1);
/******
NOTE THAT THIS INCLUDE IS OBSOLETE
USE THE BELOW FUNCTION CALL IN THE SCRIPT ITSELF
*******/
trigger_error(
'admin_smarty.php is deprecated. Use SmartyExtended->setSmartyVarsAdmin(); or setSmartyVarsFrontend();',
E_USER_DEPRECATED
);
$smarty->setSmartyVarsAdmin();
// __END__

View File

@@ -61,18 +61,27 @@ $log = new CoreLibs\Debug\Logging([
$db = new CoreLibs\DB\IO(DB_CONFIG, $log);
// login page
$login = new CoreLibs\ACL\Login($db, $log);
// lang, path, domain
// pre auto detect language after login
$locale = \CoreLibs\Language\GetLocale::setLocale();
// set lang and pass to smarty/backend
$l10n = new \CoreLibs\Language\L10n(
$locale['locale'],
$locale['domain'],
$locale['path'],
);
// flush and start
ob_end_flush();
// turn off set log per class
$log->setLogPer('class', false);
// create form class
$form = new CoreLibs\Output\Form\Generate(DB_CONFIG, $log);
$form = new CoreLibs\Output\Form\Generate(DB_CONFIG, $log, $l10n, $locale);
if ($form->mobile_phone) {
echo "I am sorry, but this page cannot be viewed by a mobile phone";
exit;
}
// smarty template engine (extended Translation version)
$smarty = new CoreLibs\Template\SmartyExtend();
$smarty = new CoreLibs\Template\SmartyExtend($l10n, $locale);
// $form->log->debug('POST', $form->log->prAr($_POST));

View File

@@ -1 +0,0 @@
../../locale/en_US/LC_MESSAGES/admin.mo

View File

@@ -1 +0,0 @@
../../locale/ja/LC_MESSAGES/admin.mo

View File

@@ -1 +0,0 @@
../../locale/en_US/LC_MESSAGES/frontend.mo

View File

@@ -1 +0,0 @@
../../locale/ja/LC_MESSAGES/frontend.mo

Binary file not shown.

View File

@@ -167,13 +167,10 @@ class Login
* constructor, does ALL, opens db, works through connection checks, closes itself
* @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
*/
public function __construct(
\CoreLibs\DB\IO $db,
\CoreLibs\Debug\Logging $log,
?\CoreLibs\Language\L10n $l10n = null
\CoreLibs\Debug\Logging $log
) {
// log login data for this class only
$log->setLogPer('class', true);
@@ -215,19 +212,6 @@ class Login
// or need to pass it back
// to the continue AJAX class for output back to the user
$this->login_is_ajax_page = isset($GLOBALS['AJAX_PAGE']) && $GLOBALS['AJAX_PAGE'] ? true : false;
// set the default lang
$locale = 'en_US.UTF-8';
$lang = 'en_utf8';
if (Session::getSessionId() !== false && !empty($_SESSION['DEFAULT_LANG'])) {
$lang = $_SESSION['DEFAULT_LANG'];
$locale = $_SESSION['DEFAULT_LOCALE'];
} else {
$lang = defined('SITE_LANG') && !empty(SITE_LANG) ?
SITE_LANG : DEFAULT_LANG;
$locale = defined('SITE_LOCALE') && !empty(SITE_LOCALE) ?
SITE_LOCALE : DEFAULT_LOCALE;
}
$this->l = $l10n ?? new \CoreLibs\Language\L10n($lang);
// if we have a search path we need to set it, to use the correct DB to login
// check what schema to use. if there is a login schema use this, else check
@@ -300,6 +284,24 @@ class Login
$this->loginCheckPermissions();
// logsout user
$this->loginLogoutUser();
// ** LANGUAGE SET AFTER LOGIN **
// set the locale
if (Session::getSessionId() !== false && !empty($_SESSION['DEFAULT_LANG'])) {
$locale = $_SESSION['DEFAULT_LOCALE'] ?? '';
} else {
$locale = defined('SITE_LOCALE') && !empty(SITE_LOCALE) ?
SITE_LOCALE :
/** @phpstan-ignore-next-line DEFAULT_LOCALE could be empty */
(defined('DEFAULT_LOCALE') && !empty(DEFAULT_LOCALE) ?
DEFAULT_LOCALE : 'en.UTF-8');
}
// set domain
if (defined('CONTENT_PATH') && !empty(CONTENT_PATH)) {
$domain = str_replace('/', '', CONTENT_PATH);
} else {
$domain = 'admin';
}
$this->l = new \CoreLibs\Language\L10n($locale, $domain);
// if the password change flag is okay, run the password change method
if ($this->password_change) {
$this->loginPasswordChange();

View File

@@ -114,15 +114,16 @@ class Backend
// CONSTRUCTOR / DECONSTRUCTOR |====================================>
/**
* main class constructor
* @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
* @param \CoreLibs\DB\IO $db Database connection class
* @param \CoreLibs\Debug\Logging $log Logging class
* @param \CoreLibs\Language\L10n $l10n l10n language class
* @param array<string,string> $locale locale data read from setLocale
*/
public function __construct(
\CoreLibs\DB\IO $db,
\CoreLibs\Debug\Logging $log,
?\CoreLibs\Language\L10n $l10n = null
\CoreLibs\Language\L10n $l10n,
array $locale
) {
// set to log not per class
$log->setLogPer('class', false);
@@ -130,10 +131,15 @@ class Backend
$this->log = $log;
// attach db class
$this->db = $db;
// TODO lang create outside of class
$this->setLangEncoding();
// get the language sub class & init it
$this->l = $l10n ?? new \CoreLibs\Language\L10n($this->lang);
$this->l = $l10n;
// parse and read, legacy stuff
$this->encoding = $locale['encoding'];
$this->lang = $locale['lang'];
// get first part from lang
$this->lang_short = explode('_', $locale['lang'])[0];
$this->domain = $this->l->getDomain();
$this->lang_dir = $this->l->getBaseLocalePath();
// set the page name
$this->page_name = \CoreLibs\Get\System::getPageName();
@@ -159,29 +165,6 @@ class Backend
// NO OP
}
// INTERNAL METHODS |===============================================>
/**
* set the language encoding and language settings
* use $OVERRIDE_LANG to override all language settings
* the default charset from _SESSION login or from
* config DEFAULT ENCODING
* the lang full name for mo loading from _SESSION login
* or SITE LANG or DEFAULT LANG from config
* creates short lang (only first two chars) from the lang
* @return void
*/
private function setLangEncoding(): void
{
list (
$this->encoding,
$this->lang,
$this->lang_short,
$this->domain,
$this->lang_dir
) = \CoreLibs\Language\GetSettings::setLangEncoding();
}
// PUBLIC METHODS |=================================================>
/**

View File

@@ -0,0 +1,117 @@
<?php
/*
* Internal function for getting locale and encodig settings
* used for new locale layout
*/
declare(strict_types=1);
namespace CoreLibs\Language;
class GetLocale
{
/**
* returns locale, lang, domain, encoding, path
* from either parameter set or from sessions/config variables
*
* @param string|null $locale override auto detect
* @param string|null $domain override domain
* @param string|null $encoding override encoding
* @param string|null $path override path
* @return array<string,string> locale, domain, encoding, path
*/
public static function setLocale(
?string $locale = null,
?string $domain = null,
?string $encoding = null,
?string $path = null
): array {
// locale must match at least basic rules
if (
empty($locale) ||
!preg_match("/^[-A-Za-z0-9_.@]+$/", $locale)
) {
if (!empty($_SESSION['DEFAULT_LOCALE'])) {
// parse from session (logged in)
$locale = $_SESSION['DEFAULT_LOCALE'];
} else {
// else parse from site locale
$locale = defined('SITE_LOCALE') && !empty(SITE_LOCALE) ?
SITE_LOCALE :
// else parse from default, if not 'en'
/** @phpstan-ignore-next-line DEFAULT_LOCALE could be empty */
(defined('DEFAULT_LOCALE') && !empty(DEFAULT_LOCALE) ?
DEFAULT_LOCALE : 'en');
}
}
// if domain is set, must be alphanumeric, if not unset
if (
empty($domain) ||
!preg_match("/^\w+$/", $domain)
) {
// if no domain is set, fall back to content path
$domain = str_replace('/', '', CONTENT_PATH);
}
// check that override encoding matches locale encoding
// if locale encoding is set
preg_match('/(?:\\.(?P<charset>[-A-Za-z0-9_]+))/', $locale, $matches);
$locale_encoding = $matches['charset'] ?? null;
if (
// empty encoding
empty($encoding) ||
// not valid encoding
!preg_match("/^[-A-Za-z0-9_]+$/", $encoding) ||
// locale encoding set and not matching to encoding
(!empty($locale_encoding) && $encoding != $locale_encoding)
) {
if (!empty($locale_encoding)) {
$encoding = strtoupper($locale_encoding);
} elseif (!empty($_SESSION['DEFAULT_CHARSET'])) {
// else set from session
$encoding = $_SESSION['DEFAULT_CHARSET'];
} else {
// else set from site encoding
$encoding = defined('SITE_ENCODING') && !empty(SITE_ENCODING) ?
SITE_ENCODING :
// or default encoding, if not 'UTF-8'
/** @phpstan-ignore-next-line DEFAULT_LOCALE could be empty */
(defined('DEFAULT_ENCODING') && !empty(DEFAULT_ENCODING) ?
DEFAULT_ENCODING : 'UTF-8');
}
}
// path checks if set, if not valid path unset to default BASE path
if (
empty($path) ||
!is_dir($path)
) {
$path = BASE . INCLUDES . LOCALE;
}
// extract lang & country from locale string, else set to en
if (
preg_match(
// lang
'/^(?P<lang>[a-z]{2,3})'
// country code
. '(?:_(?P<country>[A-Z]{2}))?/',
$locale,
$matches
)
) {
$lang = ($matches['lang'] ?? 'en')
// add country only if set
. (!empty($matches['country']) ? '_' . $matches['country'] : '');
} else {
$lang = 'en';
}
return [
'locale' => $locale,
'lang' => $lang,
'domain' => $domain,
'encoding' => $encoding,
'path' => $path,
];
}
}
// __END__

View File

@@ -30,6 +30,7 @@ class GetSettings
* @param string|null $locale A valid locale name
* @param string|null $path A valid path where the mo files will be based
* @return array<int|string,string> Settings as array/dictionary
* @deprecated Use CoreLibs\Language\GetLocale::setLocale()
*/
public static function setLangEncoding(
?string $locale = null,

View File

@@ -3,34 +3,26 @@
/*********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2004/11/18
* VERSION: 1.0.0
* VERSION: 3.0.0
* RELEASED LICENSE: GNU GPL 3
* SHORT DESCRIPTION:
* init class for gettext. Original was just a function & var setting include for wordpress.
* init class for gettext. Original was just a function &
* var setting include for wordpress.
* I changed that to a class to be more portable with my style of coding
*
* PUBLIC VARIABLES
*
* PRIVATE VARIABLES
*
* VERSION 3.0 (2022/4) removes all old folder layout and uses standard gettext
* PUBLIC METHODS
* __: returns string (translated or original if not found)
* __e: echos out string (translated or original if not found)
* __n: should return plural. never tested this.
*
* PRIVATE METHODS
* __ : returns string (translated or original if not found)
* __n : plural string
* __p : string with context
* __pb: string with context and plural
*
* HISTORY:
* 2022/4/15 (cs) drop all old folder layout support, new folder base
* in locale with standard gettext layout of
* locale/LC_MESSAGES/domain.mo
* 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;
@@ -48,15 +40,20 @@ class L10n
private $domains = [];
/** @var array<string,string> bound paths for domains */
private $paths = ['' => './'];
// files
/** @var string the full path to the mo file to loaded */
private $mofile = '';
/** @var string base path to search level */
private $base_locale_path = '';
/** @var string dynamic set path to where the mo file is actually */
private $base_content_path = '';
// errors
/** @var bool if load of mo file was unsuccessful */
private $load_failure = false;
// object holders
/** @var FileReader|bool reader class for file reading, false for short circuit */
private $input = false;
/** @var GetTextReader reader class for MO data */
@@ -74,20 +71,24 @@ class L10n
*
* @param string $locale language name, default empty string
* will return self instance
* @param string $path path, if empty fallback on default internal path
* @param string $domain override CONTENT_PATH . $encoding name for mo file
* @param bool $legacy default true, if set to true, will look in the old
* folder format lang/ CONTENT_PATH / $lang . mo
* @param string $path path, if empty fallback on default internal path
*/
public function __construct(
string $locale = '',
string $path = '',
string $domain = '',
bool $legacy = true
string $path = '',
) {
// load the mo file if locale is not empty
if (!empty($locale)) {
$this->getTranslator($locale, $path, $domain, $legacy);
// auto load language only if at least locale and domain is set
if (!empty($locale) && !empty($domain)) {
// check hack if domain and path is switched
// Note this can be removed in future versions
if (strstr($domain, DIRECTORY_SEPARATOR) !== false) {
$_domain = $path;
$path = $domain;
$domain = $_domain;
}
$this->getTranslator($locale, $domain, $path);
}
}
@@ -116,43 +117,18 @@ class L10n
require_once __DIR__ . '/l10n_functions.php';
}
/**
* legacy loader name for getTranslator
* instead of returning the GetTextReader object it returns
* true or false for successful load.
* NOTE: some time down the road this will be deprecated
*
* @param string $locale
* @param string $path
* @param string $domain
* @param bool $legacy
* @return bool Returns true for successfull load, false for error
*/
public function l10nReloadMOfile(
string $locale,
string $path = '',
string $domain = '',
bool $legacy = true
): bool {
$this->getTranslator($locale, $path, $domain, $legacy);
return $this->load_failure ? false : true;
}
/**
* loads the mo file base on path, locale and domain set
*
* @param string $locale language name (optional), fallback is en
* @param string $path path, if empty fallback on default internal path
* @param string $domain override CONTENT_PATH . $encoding name for mo file
* @param bool $legacy default true, if set to true, will look in the old
* folder format lang/ CONTENT_PATH / $lang . mo
* @param string $path path, if empty fallback on default internal path
* @return GetTextReader the main gettext reader object
*/
public function getTranslator(
string $locale = '',
string $path = '',
string $domain = '',
bool $legacy = false
string $path = ''
): GetTextReader {
// set local if not from parameter
if (empty($locale)) {
@@ -169,48 +145,34 @@ class L10n
$old_base_locale_path = $this->base_locale_path;
$old_base_content_path = $this->base_content_path;
// legacy or new type
// legacy will use the old lang/content/file.mo type as default
// if path is not set, also locale is the file name
// for new type it follows the gettext spec and path is just the
// base folder where the mo files will be searched
if ($legacy === true) {
if (!is_dir($path)) {
$this->base_locale_path = BASE . INCLUDES . LANG;
$this->base_content_path = CONTENT_PATH;
$path = $this->base_locale_path . $this->base_content_path;
}
$this->mofile = $path . $locale . ".mo";
// if path is a dir
// 1) from a previous set domain
// 2) from method option as is
// 3) fallback if BASE/INCLUDES/LOCALE set
// 4) current dir
if (!empty($this->paths[$domain]) && is_dir($this->paths[$domain])) {
$this->base_locale_path = $this->paths[$domain];
} elseif (is_dir($path)) {
$this->base_locale_path = $path;
} elseif (
defined('BASE') && defined('INCLUDES') && defined('LOCALE')
) {
// set fallback base path if constant set
$this->base_locale_path = BASE . INCLUDES . LOCALE;
} else {
// if new path is a dir
// 1) from a previous set domain
// 2) from method option as is
// 3) fallback if BASE/INCLUDES/LOCALE set
// 4) current dir
if (!empty($this->paths[$domain]) && is_dir($this->paths[$domain])) {
$this->base_locale_path = $this->paths[$domain];
} elseif (is_dir($path)) {
$this->base_locale_path = $path;
} elseif (
defined('BASE') && defined('INCLUDES') && defined('LOCALE')
) {
// set fallback base path if constant set
$this->base_locale_path = BASE . INCLUDES . LOCALE;
} else {
$this->base_locale_path = './';
}
// now we loop over lang compositions to get the base path
// then we check
$locales = $this->listLocales($locale);
foreach ($locales as $_locale) {
$this->base_content_path = $_locale . DIRECTORY_SEPARATOR
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR;
$this->mofile = $this->base_locale_path
. $this->base_content_path
. $domain . '.mo';
if (file_exists($this->mofile)) {
break;
}
$this->base_locale_path = './';
}
// now we loop over lang compositions to get the base path
// then we check
$locales = $this->listLocales($locale);
foreach ($locales as $_locale) {
$this->base_content_path = $_locale . DIRECTORY_SEPARATOR
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR;
$this->mofile = $this->base_locale_path
. $this->base_content_path
. $domain . '.mo';
if (file_exists($this->mofile)) {
break;
}
}
@@ -273,6 +235,34 @@ class L10n
return $this->l10n;
}
/**
* parse the locale string for further processing
*
* @param string $locale Locale to parse
* @return array<string,string|null> array with lang, country, charset, modifier
*/
public static function parseLocale(string $locale = ''): array
{
preg_match(
// language code
'/^(?P<lang>[a-z]{2,3})'
// country code
. '(?:_(?P<country>[A-Z]{2}))?'
// charset
. '(?:\\.(?P<charset>[-A-Za-z0-9_]+))?'
// @ modifier
. '(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/',
$locale,
$matches
);
return [
'lang' => $matches['lang'] ?? null,
'country' => $matches['country'] ?? null,
'charset' => $matches['charset'] ?? null,
'modifier' => $matches['modifier'] ?? null,
];
}
/**
* original:
* vendor/phpmyadmin/motranslator/src/Loader.php
@@ -294,28 +284,16 @@ class L10n
return $locale_list;
}
// is matching regex
if (
!preg_match(
// language code
'/^(?P<lang>[a-z]{2,3})'
// country code
. '(?:_(?P<country>[A-Z]{2}))?'
// charset
. '(?:\\.(?P<charset>[-A-Za-z0-9_]+))?'
// @ modifier
. '(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/',
$locale,
$matches
)
) {
// not matching, return as is
$locale_detail = L10n::parseLocale($locale);
// all null = nothing mached, return locale as is
if ($locale_detail === array_filter($locale_detail, 'is_null')) {
return [$locale];
}
// do matching run
$lang = $matches['lang'] ?? null;
$country = $matches['country'] ?? null;
$charset = $matches['charset'] ?? null;
$modifier = $matches['modifier'] ?? null;
// write to innteral vars
$lang = $locale_detail['lang'];
$country = $locale_detail['country'];
$charset = $locale_detail['charset'];
$modifier = $locale_detail['modifier'];
// we need to add all possible cominations from not null set
// entries to the list, from longest to shortest
// %s_%s.%s@%s (lang _ country . encoding @ suffix)

View File

@@ -299,23 +299,39 @@ 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
* @param array<mixed> $db_config db config array, mandatory
* @param \CoreLibs\Debug\Logging|null $log Logging class, null auto set
* @param \CoreLibs\Language\L10n|null $l10n l10n language class, null auto set
* @param array<string,string>|null $locale locale array from ::setLocale,
* null auto set
*/
public function __construct(
array $db_config,
\CoreLibs\Debug\Logging $log = null,
?\CoreLibs\Language\L10n $l10n = null
?\CoreLibs\Debug\Logging $log = null,
?\CoreLibs\Language\L10n $l10n = null,
?array $locale = null
) {
global $table_arrays;
// replace any non valid variable names
// TODO extract only alphanumeric and _ after . to _ replacement
$this->my_page_name = str_replace(['.'], '_', System::getPageName(System::NO_EXTENSION));
$this->setLangEncoding();
// if pass on locale is null
if ($locale === null) {
$locale = \CoreLibs\Language\GetLocale::setLocale();
}
// init the language class
$this->l = $l10n ?? new \CoreLibs\Language\L10n($this->lang);
$this->l = $l10n ?? new \CoreLibs\Language\L10n(
$locale['locale'],
$locale['domain'],
$locale['path'],
);
// legacy lang vars set
$this->encoding = $locale['encoding'];
$this->lang = $locale['lang'];
// get first part from lang
$this->lang_short = explode('_', $locale['lang'])[0];
$this->domain = $this->l->getDomain();
$this->lang_dir = $this->l->getBaseLocalePath();
// load config array
// get table array definitions for current page name
@@ -443,29 +459,6 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
parent::__destruct();
}
// INTERNAL METHODS |===============================================>
/**
* ORIGINAL in \CoreLibs\Admin\Backend
* set the language encoding and language settings
* the default charset from _SESSION login or from
* config DEFAULT ENCODING
* the lang full name for mo loading from _SESSION login
* or SITE LANG or DEFAULT LANG from config
* creates short lang (only first two chars) from the lang
* @return void
*/
private function setLangEncoding(): void
{
list (
$this->encoding,
$this->lang,
$this->lang_short,
$this->domain,
$this->lang_dir
) = \CoreLibs\Language\GetSettings::setLangEncoding();
}
// PUBLIC METHODS |=================================================>
/**

View File

@@ -154,50 +154,36 @@ 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
* @param \CoreLibs\Language\L10n $l10n l10n language class
* @param array<string,string> $locale locale data read from setLocale
*/
public function __construct(?\CoreLibs\Language\L10n $l10n = null)
public function __construct(\CoreLibs\Language\L10n $l10n, array $locale)
{
// call basic smarty
// or Smarty::__construct();
parent::__construct();
// set lang vars
$this->setLangEncoding();
// iinit lang
$this->l10n = $l10n ?? new \CoreLibs\Language\L10n($this->lang);
// Smarty 3.x
// $this->registerPlugin('modifier', 'getvar', [&$this, 'get_template_vars']);
$this->l10n = $l10n;
// opt load functions so we can use legacy init for smarty run perhaps
$this->l10n->loadFunctions();
// parse and read, legacy stuff
$this->encoding = $locale['encoding'];
$this->lang = $locale['lang'];
// get first part from lang
$this->lang_short = explode('_', $locale['lang'])[0];
$this->domain = $this->l10n->getDomain();
$this->lang_dir = $this->l10n->getBaseLocalePath();
// register smarty variable
$this->registerPlugin('modifier', 'getvar', [&$this, 'getTemplateVars']);
$this->page_name = pathinfo($_SERVER["PHP_SELF"])['basename'];
$this->page_name = \CoreLibs\Get\System::getPageName();
// set internal settings
$this->CACHE_ID = defined('CACHE_ID') ? CACHE_ID : '';
$this->COMPILE_ID = defined('COMPILE_ID') ? COMPILE_ID : '';
}
/**
* ORIGINAL in \CoreLibs\Admin\Backend
* set the language encoding and language settings
* the default charset from _SESSION login or from
* config DEFAULT ENCODING
* the lang full name for mo loading from _SESSION login
* or SITE LANG or DEFAULT LANG from config
* creates short lang (only first two chars) from the lang
* @return void
*/
private function setLangEncoding(): void
{
list (
$this->encoding,
$this->lang,
$this->lang_short,
$this->domain,
$this->lang_dir
) = \CoreLibs\Language\GetSettings::setLangEncoding();
}
/**
* @return void
*/

View File

@@ -47,6 +47,7 @@ return array(
'CoreLibs\\Language\\Core\\StreamReader' => $baseDir . '/lib/CoreLibs/Language/Core/StreamReader.php',
'CoreLibs\\Language\\Core\\StringReader' => $baseDir . '/lib/CoreLibs/Language/Core/StringReader.php',
'CoreLibs\\Language\\Encoding' => $baseDir . '/lib/CoreLibs/Language/Encoding.php',
'CoreLibs\\Language\\GetLocale' => $baseDir . '/lib/CoreLibs/Language/GetLocale.php',
'CoreLibs\\Language\\GetSettings' => $baseDir . '/lib/CoreLibs/Language/GetSettings.php',
'CoreLibs\\Language\\L10n' => $baseDir . '/lib/CoreLibs/Language/L10n.php',
'CoreLibs\\Output\\Form\\Elements' => $baseDir . '/lib/CoreLibs/Output/Form/Elements.php',

View File

@@ -112,6 +112,7 @@ class ComposerStaticInit10fe8fe2ec4017b8644d2b64bcf398b9
'CoreLibs\\Language\\Core\\StreamReader' => __DIR__ . '/../..' . '/lib/CoreLibs/Language/Core/StreamReader.php',
'CoreLibs\\Language\\Core\\StringReader' => __DIR__ . '/../..' . '/lib/CoreLibs/Language/Core/StringReader.php',
'CoreLibs\\Language\\Encoding' => __DIR__ . '/../..' . '/lib/CoreLibs/Language/Encoding.php',
'CoreLibs\\Language\\GetLocale' => __DIR__ . '/../..' . '/lib/CoreLibs/Language/GetLocale.php',
'CoreLibs\\Language\\GetSettings' => __DIR__ . '/../..' . '/lib/CoreLibs/Language/GetSettings.php',
'CoreLibs\\Language\\L10n' => __DIR__ . '/../..' . '/lib/CoreLibs/Language/L10n.php',
'CoreLibs\\Output\\Form\\Elements' => __DIR__ . '/../..' . '/lib/CoreLibs/Output/Form/Elements.php',