Compare commits

...

6 Commits

Author SHA1 Message Date
Clemens Schwaighofer
316ca106fd Composer autoload update 2022-04-15 13:57:48 +09:00
Clemens Schwaighofer
fd9b201346 Remove old GetSettings for language legacy flow 2022-04-15 13:57:26 +09:00
Clemens Schwaighofer
667dc4de8e Clean up of old _LANG config variables 2022-04-15 13:53:37 +09:00
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
Clemens Schwaighofer
c181a83b48 Rename po files to new standard <locale>-<domain>.po 2022-04-14 14:47:58 +09:00
Clemens Schwaighofer
c830a32962 Minor fix in gettext loop for max string lenght 2022-04-14 13:43:26 +09:00
50 changed files with 966 additions and 1142 deletions

View File

@@ -13,6 +13,7 @@ en_US@latin -> en_US -> en
* en * en
* en_US * en_US
* en_US.UTF-8
* en_US@latin * en_US@latin
### Domain ### Domain
@@ -32,7 +33,9 @@ if not set UTF-8 is assumed. Any other utf8 encoding is changed to UTF-8
## File name example source ## File name example source
`ja_US.admin.UTF-8.po` `ja-admin.po`
First part is LOCALE, second part is domain. Separator is `-`
## Folder layout ## Folder layout
@@ -49,4 +52,4 @@ frontend: dmain (CONTENT_PATH)
## command ## command
`msgfmt -o www/includes/locale/ja_JP/LC_MESSAGES/frontend.UTF-8.mo 4dev/lang/ja_US.admin.UTF-8.po` `msgfmt -o www/includes/locale/ja/LC_MESSAGES/frontend.mo 4dev/lang/ja.admin.po`

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,24 @@
# to craete: msgfmt -o <output.po> <input.mo>
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

@@ -1,8 +0,0 @@
# to craete: msgfmt -o <output.po> <input.mo>
msgid ""
msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
msgid "INPUT TEST"
msgstr "OUTPUT TEST FRONTEND JA"

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

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

View File

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

View File

@@ -7,7 +7,10 @@ if [ ! -f "locale/en_US/LC_MESSAGES/admin.mo" ]; then
exit; exit;
fi; fi;
msgfmt -o locale/en_US/LC_MESSAGES/admin.mo locale/en_US.admin.UTF-8.po for file in $(ls -1 locale/*.po); do
msgfmt -o locale/en_US/LC_MESSAGES/frontend.mo locale/en_US.frontend.UTF-8.po echo $file;
msgfmt -o locale/ja_JP/LC_MESSAGES/admin.mo locale/ja_JP.admin.UTF-8.po file=$(basename $file .po);
msgfmt -o locale/ja_JP/LC_MESSAGES/frontend.mo locale/ja_JP.frontend.UTF-8.po locale=$(echo "${file}" | cut -d "-" -f 1);
domain=$(echo "${file}" | cut -d "-" -f 2);
msgfmt -o locale/${locale}/LC_MESSAGES/${domain}.mo locale/${locale}-${domain}.po;
done;

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

@@ -44,9 +44,24 @@ $db = new CoreLibs\DB\IO(DB_CONFIG, $log);
// login & page access check // login & page access check
$login = new CoreLibs\ACL\Login($db, $log); $login = new CoreLibs\ACL\Login($db, $log);
``` ```
* update language class
```php
// 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'],
);
```
* smarty needs language
```php
$smarty = new CoreLibs\Template\SmartyExtend($l10n, $locale);
```
* admin backend also needs logger * admin backend also needs logger
```php ```php
$cms = new CoreLibs\Admin\Backend($db, $log); $cms = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale);
``` ```
* update and `$cms` or similar calls so db is in `$cms->db->...` and log are in `$cms->log->...` * update and `$cms` or similar calls so db is in `$cms->db->...` and log are in `$cms->log->...`
* update all `config.*.php` files where needed * update all `config.*.php` files where needed
@@ -60,6 +75,12 @@ require BASE . LIB . 'autoloader.php';
``` ```
**UPDATE:** **UPDATE:**
```php ```php
// po langs [DEPRECAED: use LOCALE]
define('LANG', 'lang' . DIRECTORY_SEPARATOR);
// po locale file
define('LOCALE', 'locale' . DIRECTORY_SEPARATOR);
```
```php
// SSL host name // SSL host name
// define('SSL_HOST', $_ENV['SSL_HOST'] ?? ''); // define('SSL_HOST', $_ENV['SSL_HOST'] ?? '');
``` ```
@@ -85,6 +106,13 @@ define('CONTENT_WIDTH', '100%');
define('BASE_NAME', preg_replace('/[^A-Za-z0-9]/', '', $_ENV['BASE_NAME'] ?? '')); define('BASE_NAME', preg_replace('/[^A-Za-z0-9]/', '', $_ENV['BASE_NAME'] ?? ''));
``` ```
```php ```php
/************* LANGUAGE / ENCODING *******/
// default lang + encoding
define('DEFAULT_LOCALE', 'en_US.UTF-8');
// default web page encoding setting
define('DEFAULT_ENCODING', 'UTF-8');
```
```php
// BAIL ON MISSING DB CONFIG: // BAIL ON MISSING DB CONFIG:
// we have either no db selction for this host but have db config entries // we have either no db selction for this host but have db config entries
// or we have a db selection but no db config as array or empty // or we have a db selection but no db config as array or empty
@@ -104,6 +132,10 @@ if (
} }
``` ```
```php ```php
// remove SITE_LANG
define('SITE_LOCALE', $SITE_CONFIG[HOST_NAME]['site_locale'] ?? DEFAULT_LOCALE);
define('SITE_ENCODING', $SITE_CONFIG[HOST_NAME]['site_encoding'] ?? DEFAULT_ENCODING);
```
```php ```php
/************* GENERAL PAGE TITLE ********/ /************* GENERAL PAGE TITLE ********/
define('G_TITLE', $_ENV['G_TITLE'] ?? ''); define('G_TITLE', $_ENV['G_TITLE'] ?? '');
@@ -118,6 +150,15 @@ In the config then
```php ```php
'db_name' => $_ENV['DB_NAME.TEST'] ?? '', 'db_name' => $_ENV['DB_NAME.TEST'] ?? '',
``` ```
* config.host.php update
must add site_locale (site_lang + site_encoding)
remove site_lang
```php
// lang + encoding
'site_locale' => 'en_US.UTF-8',
// site language
'site_encoding' => 'UTF-8',
```
* copy `layout/admin/javascript/edit.jq.js` * copy `layout/admin/javascript/edit.jq.js`
* check other javacsript files if needed (`edit.jq.js`) * check other javacsript files if needed (`edit.jq.js`)

View File

@@ -43,7 +43,13 @@ $log = new CoreLibs\Debug\Logging([
]); ]);
// db config with logger // db config with logger
$db = new CoreLibs\DB\IO(DB_CONFIG, $log); $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 "<!DOCTYPE html>";
print "<html><head><title>TEST CLASS: ADMIN BACKEND</title><head>"; 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>'; print '<div><a href="class_test.php">Class Test Master</a></div>';
// set acl, from eg login acl // 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 "ADBEDITLOG: " . $backend->adbEditLog('CLASSTEST-ADMIN', 'Some info stirng') . "<br>";
print "ADBTOPMENU(0): " . \CoreLibs\Debug\Support::printAr($backend->adbTopMenu()) . "<br>"; print "ADBTOPMENU(0): " . \CoreLibs\Debug\Support::printAr($backend->adbTopMenu()) . "<br>";
print "ADBMSG: " . $backend->adbMsg('info', 'Message: %1$d', [1]) . "<br>"; print "ADBMSG: " . $backend->adbMsg('info', 'Message: %1$d', [1]) . "<br>";

View File

@@ -19,8 +19,6 @@ ob_start();
// basic class test file // basic class test file
define('USE_DATABASE', false); define('USE_DATABASE', false);
// init language
$lang = 'en_utf8';
// sample config // sample config
require 'config.php'; require 'config.php';
// set session name // set session name
@@ -37,69 +35,43 @@ print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>'; print '<div><a href="class_test.php">Class Test Master</a></div>';
use CoreLibs\Language\L10n; use CoreLibs\Language\L10n;
use CoreLibs\Debug\Support;
$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>";
echo "<br><b>LIST LOCALES</b><br>"; echo "<br><b>LIST LOCALES</b><br>";
$locale = 'en_US.UTF-8'; $locale = 'en_US.UTF-8';
$locales = CoreLibs\Language\L10n::listLocales($locale); $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'; $locale = 'en.UTF-8';
$locales = CoreLibs\Language\L10n::listLocales($locale); $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 // try to load non existing
echo "<br><b>NEW TYPE</b><br>"; echo "<br><b>NEW TYPE</b><br>";
// translate string
$string = 'INPUT TEST';
// new path test // new path test
$lang = 'ja'; $lang = 'ja';
$domain = 'admin'; $domain = 'admin';
$encoding = 'UTF-8'; $encoding = 'UTF-8';
$path = BASE . INCLUDES . LOCALE; $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 "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>"; echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "DOMAIN WANT/SET: " . $domain . '/' . $l->getDomain() . "<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 . " => " echo "MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. $l->__n($single_string, $multi_string, $n) . "<br>"; . $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'; $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 "*<br>";
echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>"; echo "LANGUAGE WANT/SET: " . $lang . '/' . $l->getLocale() . "<br>";
echo "DOMAIN WANT/SET: " . $domain . '/' . $l->getDomain() . "<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); $db = new CoreLibs\DB\IO(DB_CONFIG, $log);
$login = new CoreLibs\ACL\Login($db, $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); $backend->db->dbInfo(true);
ob_end_flush(); 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 '<div><a href="class_test.autoloader.php">Class Test: AUTOLOADER</a></div>';
print "<hr>"; print "<hr>";
print "L: " . CoreLibs\Debug\Support::printAr($locale) . "<br>";
// print all _ENV vars set // print all _ENV vars set
print "<div>READ _ENV ARRAY:</div>"; 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 // set + check edit access id
$edit_access_id = 3; $edit_access_id = 3;
if (is_object($login) && isset($login->acl['unit'])) { if (is_object($login) && isset($login->acl['unit'])) {

View File

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

View File

@@ -27,9 +27,8 @@ $__LOCAL_CONFIG = [
'db_host' => 'test', 'db_host' => 'test',
'location' => 'test', 'location' => 'test',
'debug_flag' => true, 'debug_flag' => true,
'site_lang' => 'en_US',
'site_encoding' => 'UTF-8',
'site_locale' => 'en_US.UTF-8', 'site_locale' => 'en_US.UTF-8',
'site_encoding' => 'UTF-8',
'login_enabled' => true 'login_enabled' => true
]; ];
@@ -46,11 +45,10 @@ $SITE_CONFIG = [
'location' => 'test', 'location' => 'test',
// show DEBUG override // show DEBUG override
'debug_flag' => true, 'debug_flag' => true,
// site language // site locale
'site_lang' => 'en_US',
'site_encoding' => 'UTF-8',
// lang + encoding
'site_locale' => 'en_US.UTF-8', 'site_locale' => 'en_US.UTF-8',
// site encoding
'site_encoding' => 'UTF-8',
// enable/disable login override // enable/disable login override
'login_enabled' => true 'login_enabled' => true
], ],

View File

@@ -65,8 +65,6 @@ define('JS', 'javascript' . DIRECTORY_SEPARATOR);
define('TABLE_ARRAYS', 'table_arrays' . DIRECTORY_SEPARATOR); define('TABLE_ARRAYS', 'table_arrays' . DIRECTORY_SEPARATOR);
// smarty libs path // smarty libs path
define('SMARTY', 'Smarty' . DIRECTORY_SEPARATOR); define('SMARTY', 'Smarty' . DIRECTORY_SEPARATOR);
// po langs [DEPRECAED: use LOCALE]
define('LANG', 'lang' . DIRECTORY_SEPARATOR);
// po locale file // po locale file
define('LOCALE', 'locale' . DIRECTORY_SEPARATOR); define('LOCALE', 'locale' . DIRECTORY_SEPARATOR);
// cache path // cache path
@@ -160,11 +158,10 @@ define('CACHE_ID', 'CACHE_' . BASE_NAME . '_' . SERVER_NAME_HASH);
define('COMPILE_ID', 'COMPILE_' . BASE_NAME . '_' . SERVER_NAME_HASH); define('COMPILE_ID', 'COMPILE_' . BASE_NAME . '_' . SERVER_NAME_HASH);
/************* LANGUAGE / ENCODING *******/ /************* LANGUAGE / ENCODING *******/
define('DEFAULT_LANG', 'en_US');
// default web page encoding setting
define('DEFAULT_ENCODING', 'UTF-8');
// default lang + encoding // default lang + encoding
define('DEFAULT_LOCALE', 'en_US.UTF-8'); define('DEFAULT_LOCALE', 'en_US.UTF-8');
// default web page encoding setting
define('DEFAULT_ENCODING', 'UTF-8');
/************* LOGGING *******************/ /************* LOGGING *******************/
// below two can be defined here, but they should be // below two can be defined here, but they should be
@@ -262,9 +259,8 @@ $GLOBALS['DB_CONFIG'] = DB_CONFIG;
// debug settings, site lang, etc // debug settings, site lang, etc
define('TARGET', $SITE_CONFIG[HOST_NAME]['location'] ?? 'test'); define('TARGET', $SITE_CONFIG[HOST_NAME]['location'] ?? 'test');
define('DEBUG', $SITE_CONFIG[HOST_NAME]['debug_flag'] ?? false); define('DEBUG', $SITE_CONFIG[HOST_NAME]['debug_flag'] ?? false);
define('SITE_LANG', $SITE_CONFIG[HOST_NAME]['site_lang'] ?? DEFAULT_LANG);
define('SITE_ENCODING', $SITE_CONFIG[HOST_NAME]['site_encoding'] ?? DEFAULT_ENCODING);
define('SITE_LOCALE', $SITE_CONFIG[HOST_NAME]['site_locale'] ?? DEFAULT_LOCALE); define('SITE_LOCALE', $SITE_CONFIG[HOST_NAME]['site_locale'] ?? DEFAULT_LOCALE);
define('SITE_ENCODING', $SITE_CONFIG[HOST_NAME]['site_encoding'] ?? DEFAULT_ENCODING);
define('LOGIN_ENABLED', $SITE_CONFIG[HOST_NAME]['login_enabled'] ?? false); define('LOGIN_ENABLED', $SITE_CONFIG[HOST_NAME]['login_enabled'] ?? false);
define('AUTH', $SITE_CONFIG[HOST_NAME]['auth'] ?? false); define('AUTH', $SITE_CONFIG[HOST_NAME]['auth'] ?? false);
// paths // paths

View File

@@ -53,9 +53,14 @@ if (!defined('DIR')) {
// find trigger name "admin/" or "frontend/" in the getcwd() folder // find trigger name "admin/" or "frontend/" in the getcwd() folder
foreach (['admin', 'frontend'] as $folder) { foreach (['admin', 'frontend'] as $folder) {
if (strstr(getcwd() ?: '', DIRECTORY_SEPARATOR . $folder)) { if (strstr(getcwd() ?: '', DIRECTORY_SEPARATOR . $folder)) {
define('CONTENT_PATH', $folder . DIRECTORY_SEPARATOR);
break; break;
} }
} }
// if content path is empty, fallback is default
/** @phpstan-ignore-next-line can be empty */
if (empty($folder)) {
$folder = 'default';
}
define('CONTENT_PATH', $folder . DIRECTORY_SEPARATOR);
// __END__ // __END__

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); $db = new CoreLibs\DB\IO(DB_CONFIG, $log);
// login & page access check // login & page access check
$login = new CoreLibs\ACL\Login($db, $log); $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 // create smarty object
$smarty = new CoreLibs\Template\SmartyExtend(); $smarty = new CoreLibs\Template\SmartyExtend($l10n, $locale);
// create new Backend class with db and loger attached // 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) // the menu show flag (what menu to show)
$cms->menu_show_flag = 'main'; $cms->menu_show_flag = 'main';
// db info // 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); $db = new CoreLibs\DB\IO(DB_CONFIG, $log);
// login page // login page
$login = new CoreLibs\ACL\Login($db, $log); $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 // flush and start
ob_end_flush(); ob_end_flush();
// turn off set log per class // turn off set log per class
$log->setLogPer('class', false); $log->setLogPer('class', false);
// create form class // 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) { if ($form->mobile_phone) {
echo "I am sorry, but this page cannot be viewed by a mobile phone"; echo "I am sorry, but this page cannot be viewed by a mobile phone";
exit; exit;
} }
// smarty template engine (extended Translation version) // 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)); // $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 * constructor, does ALL, opens db, works through connection checks, closes itself
* @param \CoreLibs\DB\IO $db Database connection class * @param \CoreLibs\DB\IO $db Database connection class
* @param \CoreLibs\Debug\Logging $log Logging class * @param \CoreLibs\Debug\Logging $log Logging class
* @param \CoreLibs\Language\L10n|null $l10n l10n language class
* if null, auto set
*/ */
public function __construct( public function __construct(
\CoreLibs\DB\IO $db, \CoreLibs\DB\IO $db,
\CoreLibs\Debug\Logging $log, \CoreLibs\Debug\Logging $log
?\CoreLibs\Language\L10n $l10n = null
) { ) {
// log login data for this class only // log login data for this class only
$log->setLogPer('class', true); $log->setLogPer('class', true);
@@ -215,19 +212,6 @@ class Login
// or need to pass it back // or need to pass it back
// to the continue AJAX class for output back to the user // 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; $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 // 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 // check what schema to use. if there is a login schema use this, else check
@@ -300,6 +284,24 @@ class Login
$this->loginCheckPermissions(); $this->loginCheckPermissions();
// logsout user // logsout user
$this->loginLogoutUser(); $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 the password change flag is okay, run the password change method
if ($this->password_change) { if ($this->password_change) {
$this->loginPasswordChange(); $this->loginPasswordChange();

View File

@@ -114,15 +114,16 @@ class Backend
// CONSTRUCTOR / DECONSTRUCTOR |====================================> // CONSTRUCTOR / DECONSTRUCTOR |====================================>
/** /**
* main class constructor * main class constructor
* @param \CoreLibs\DB\IO $db Database connection class * @param \CoreLibs\DB\IO $db Database connection class
* @param \CoreLibs\Debug\Logging $log Logging class * @param \CoreLibs\Debug\Logging $log Logging class
* @param \CoreLibs\Language\L10n|null $l10n l10n language class * @param \CoreLibs\Language\L10n $l10n l10n language class
* if null, auto set * @param array<string,string> $locale locale data read from setLocale
*/ */
public function __construct( public function __construct(
\CoreLibs\DB\IO $db, \CoreLibs\DB\IO $db,
\CoreLibs\Debug\Logging $log, \CoreLibs\Debug\Logging $log,
?\CoreLibs\Language\L10n $l10n = null \CoreLibs\Language\L10n $l10n,
array $locale
) { ) {
// set to log not per class // set to log not per class
$log->setLogPer('class', false); $log->setLogPer('class', false);
@@ -130,10 +131,15 @@ class Backend
$this->log = $log; $this->log = $log;
// attach db class // attach db class
$this->db = $db; $this->db = $db;
// TODO lang create outside of class
$this->setLangEncoding();
// get the language sub class & init it // 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 // set the page name
$this->page_name = \CoreLibs\Get\System::getPageName(); $this->page_name = \CoreLibs\Get\System::getPageName();
@@ -159,29 +165,6 @@ class Backend
// NO OP // 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 |=================================================> // PUBLIC METHODS |=================================================>
/** /**

View File

@@ -353,7 +353,8 @@ class GetTextReader
$expr .= ';'; $expr .= ';';
$res = ''; $res = '';
$p = 0; $p = 0;
for ($i = 0; $i < strlen($expr); $i++) { $expr_len = strlen($expr);
for ($i = 0; $i < $expr_len; $i++) {
$ch = $expr[$i]; $ch = $expr[$i];
switch ($ch) { switch ($ch) {
case '?': case '?':
@@ -433,6 +434,7 @@ class GetTextReader
$total = 0; $total = 0;
$plural = 0; $plural = 0;
// FIXME use Symfony\Component\ExpressionLanguage\ExpressionLanguage or similar
eval("$string"); eval("$string");
/** @phpstan-ignore-next-line 0 >= 0 is always true*/ /** @phpstan-ignore-next-line 0 >= 0 is always true*/
if ($plural >= $total) { if ($plural >= $total) {

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

@@ -1,140 +0,0 @@
<?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 locale is set it must be in the format of:
* <lang>.<encoding>
* <lang>_<country>.<encoding>
* <lang>_<country>.<encoding>@<subset>
* If no encoding is set in the is, UTF-8 is assumed
*
* Returned is an array with array indes and dictionary index
* 0~4 array are
* encoding: 0
* lang: 1
* lang_short: 2
* domain: 3
* path: 4
*
* @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
*/
public static function setLangEncoding(
?string $locale = null,
?string $domain = null,
?string $path = null
): array {
$lang = '';
$lang_short = '';
$encoding = '';
// if is is set, extract
if (!empty($locale)) {
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
);
// lang short part
$lang_short = $matches['lang'] ?? '';
$lang = $lang_short;
// lang + country if country is set
if (!empty($matches['country'])) {
$lang = sprintf('%s_%s', $lang_short, $matches['country']);
}
// encoding if set
$encoding = strtoupper($matches['charset'] ?? 'UTF-8');
}
// 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,
];
}
}

View File

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

View File

@@ -299,23 +299,39 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
/** /**
* construct form generator * construct form generator
* @param array<mixed> $db_config db config array * @param array<mixed> $db_config db config array, mandatory
* @param \CoreLibs\Debug\Logging|null $log Logging class * @param \CoreLibs\Debug\Logging|null $log Logging class, null auto set
* @param \CoreLibs\Language\L10n|null $l10n l10n language class * @param \CoreLibs\Language\L10n|null $l10n l10n language class, null auto set
* if null, auto set * @param array<string,string>|null $locale locale array from ::setLocale,
* null auto set
*/ */
public function __construct( public function __construct(
array $db_config, array $db_config,
\CoreLibs\Debug\Logging $log = null, ?\CoreLibs\Debug\Logging $log = null,
?\CoreLibs\Language\L10n $l10n = null ?\CoreLibs\Language\L10n $l10n = null,
?array $locale = null
) { ) {
global $table_arrays; global $table_arrays;
// replace any non valid variable names // replace any non valid variable names
// TODO extract only alphanumeric and _ after . to _ replacement // TODO extract only alphanumeric and _ after . to _ replacement
$this->my_page_name = str_replace(['.'], '_', System::getPageName(System::NO_EXTENSION)); $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 // 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 // load config array
// get table array definitions for current page name // get table array definitions for current page name
@@ -443,29 +459,6 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
parent::__destruct(); 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 |=================================================> // PUBLIC METHODS |=================================================>
/** /**

View File

@@ -154,50 +154,36 @@ class SmartyExtend extends \Smarty
* constructor class, just sets the language stuff * constructor class, just sets the language stuff
* calls L10 for pass on internaly in smarty * calls L10 for pass on internaly in smarty
* also registers the getvar caller plugin * also registers the getvar caller plugin
* @param \CoreLibs\Language\L10n|null $l10n l10n language class * @param \CoreLibs\Language\L10n $l10n l10n language class
* if null, auto set * @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 // call basic smarty
// or Smarty::__construct(); // or Smarty::__construct();
parent::__construct(); parent::__construct();
// set lang vars
$this->setLangEncoding();
// iinit lang // iinit lang
$this->l10n = $l10n ?? new \CoreLibs\Language\L10n($this->lang); $this->l10n = $l10n;
// Smarty 3.x // opt load functions so we can use legacy init for smarty run perhaps
// $this->registerPlugin('modifier', 'getvar', [&$this, 'get_template_vars']); $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->registerPlugin('modifier', 'getvar', [&$this, 'getTemplateVars']);
$this->page_name = pathinfo($_SERVER["PHP_SELF"])['basename']; $this->page_name = \CoreLibs\Get\System::getPageName();
// set internal settings // set internal settings
$this->CACHE_ID = defined('CACHE_ID') ? CACHE_ID : ''; $this->CACHE_ID = defined('CACHE_ID') ? CACHE_ID : '';
$this->COMPILE_ID = defined('COMPILE_ID') ? COMPILE_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 * @return void
*/ */

View File

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

View File

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