Compare commits

..

6 Commits

Author SHA1 Message Date
Clemens Schwaighofer
43b51895f0 Bug fixes in ACL\Login and DB\IO\Extended 2022-04-22 15:32:06 +09:00
Clemens Schwaighofer
d0e294ecf5 Fix Language encoding move documentation 2022-04-18 17:57:04 +09:00
Clemens Schwaighofer
3c35341e8b Bug fix in smarty block.t plugin
Equal instead of assign for plural text translate calls.

Update smarty tests with more detail checks of translations
2022-04-18 10:52:37 +09:00
Clemens Schwaighofer
daf1f9263c Change all function L10n calls to one underscore, test updates
That change is done to be compatible with the phpmyadmin translator
class so this can be a drop in replacement or other way around.

Update smarty block.t to only check for _* functions and not any pre
loaded language class
2022-04-18 10:33:45 +09:00
Clemens Schwaighofer
805c695d68 Language\L10n method name fixes, update Smarty block.t.php
__pn for context plural has now correct name __np

Update smarty plugin block.t.php to use __* named gettext methods for
all calls, for __n/__np/__/__p calls we fallback to check internal set
class on l10n object variable.

This will be removed in future calls
2022-04-15 16:53:02 +09:00
Clemens Schwaighofer
ffdd45e32a Fix phpUnit tests with constant settings and missing checks
BASE constant setting: moved all to same base folder in 4dev/tests
check all other CONSTANT settings if they are already set and skip (used
only in Language default set)

Add missing phpunit check for array merge recursive
2022-04-15 15:19:30 +09:00
19 changed files with 373 additions and 157 deletions

View File

@@ -7,6 +7,7 @@ declare(strict_types=1);
namespace tests;
use Exception;
use PHPUnit\Framework\TestCase;
/**
@@ -274,7 +275,6 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
}
/**
* TODO: create provider for n array merge
* provides array listing for the merge test
*
* @return array
@@ -282,6 +282,61 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
public function arrayMergeRecursiveProvider(): array
{
return [
// 0: expected
// 1..n: to merge arrays
// n+1: trigger for handle keys as string
'two arrays' => [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2],
['b' => 2, 'c' => 3],
],
'two arrays, string flag' => [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2],
['b' => 2, 'c' => 3],
true,
],
// non hash arrays
'non hash array merge, no string flag' => [
[3, 4, 5],
[1, 2, 3],
[3, 4, 5],
],
'non hash array merge, string flag' => [
[1, 2, 3, 3, 4, 5],
[1, 2, 3],
[3, 4, 5],
true
],
];
}
/**
* for warning checks
*
* @return array
*/
public function arrayMergeRecursiveProviderWarning(): array
{
return [
// error <2 arguments
'too view arguments' => [
'arrayMergeRecursive needs two or more array arguments',
[1]
],
// error <2 arrays
'only one array' => [
'arrayMergeRecursive needs two or more array arguments',
[1],
true,
],
// error element is not array
'non array between array' => [
'arrayMergeRecursive encountered a non array argument',
[1],
'string',
[2]
],
];
}
@@ -626,21 +681,43 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
* Undocumented function
*
* @covers ::arrayMergeRecursive
* @#dataProvider arrayMergeRecursiveProvider
* @testdox arrayMergeRecursive ... will be $expected [$_dataName]
* @dataProvider arrayMergeRecursiveProvider
* @testdox arrayMergeRecursive ... [$_dataName]
*
* @param array $input nested array set as each parameter
* @param bool $flag
* @param bool|array $expected
* @return void
* array $input, bool $flag, $expected
*
*/
public function testArrayMergeRecursive(): void
{
$this->assertTrue(true, 'Implement proper test run');
$this->markTestIncomplete(
'testArrayMergeRecursive has not been implemented yet.'
$arrays = func_get_args();
// first is expected array, always
$expected = array_shift($arrays);
$output = \CoreLibs\Combined\ArrayHandler::arrayMergeRecursive(
...$arrays
);
$this->assertEquals(
$expected,
$output
);
}
/**
* Undocumented function
*
* @covers ::arrayMergeRecursive
* @dataProvider arrayMergeRecursiveProviderWarning
* @testdox arrayMergeRecursive with E_USER_WARNING [$_dataName]
*
* @return void
*/
public function testArrayMergeRecursiveWarningA(): void
{
$arrays = func_get_args();
// first is expected warning
$warning = array_shift($arrays);
$this->expectWarning();
$this->expectWarningMessage($warning);
\CoreLibs\Combined\ArrayHandler::arrayMergeRecursive(...$arrays);
}
/**

View File

@@ -68,6 +68,7 @@ final class CoreLibsCreateSessionTest extends TestCase
*/
public function testStartSession(string $input, string $type, $expected_n, $expected_i): void
{
// NEEDS MOCKING
/* $session_id = '';
switch ($type) {
case 'p':
@@ -97,7 +98,8 @@ final class CoreLibsCreateSessionTest extends TestCase
if ($type == 'g') {
unset($GLOBALS['SET_SESSION_NAME']);
} */
$this->markTestSkipped('No implementation for Create\Session. Cannot run session_start in CLI');
$this->markTestSkipped('[CoreLibsCreateSessionTest] No implementation '
. 'for Create\Session. Cannot run session_start in CLI');
}
}

View File

@@ -2709,7 +2709,8 @@ final class CoreLibsDBIOTest extends TestCase
// comarep all, except timestamp that is a regex
foreach ($expected_history as $key => $value) {
// check if starts with / because this is regex (timestamp)
if (strpos($value, "/") === 0) {
// if (substr($expected_2, 0, 1) == '/) {
if (strpos($value, '/') === 0) {
// this is regex
$this->assertMatchesRegularExpression(
$value,

View File

@@ -54,14 +54,16 @@ final class CoreLibsDebugLoggingTest extends TestCase
'no options set, constant set' => [
null,
[
'log_folder' => '/tmp/',
'log_folder' => str_replace('/configs', '', __DIR__)
. DIRECTORY_SEPARATOR . 'log/',
'debug_all' => false,
'print_all' => false,
],
[
'constant' => [
'BASE' => '/tmp',
'LOG' => '/'
'BASE' => str_replace('/configs', '', __DIR__)
. DIRECTORY_SEPARATOR,
'LOG' => 'log/'
]
]
],

View File

@@ -90,11 +90,16 @@ final class CoreLibsGetSystemTest extends TestCase
public function getPageNameProvider(): array
{
return [
// 0: input
// 1: expected default/WITH_EXTENSION
// 2: expected NO_EXTENSION
// 3: expected FULL_PATH, if first and last character are / use regex
'original set' => [
0 => null, // input
0 => null,
1 => 'phpunit',
2 => 'phpunit',
3 => 'www/vendor/bin/phpunit', // NOTE: this can change
// NOTE: this can change, so it is a regex check
3 => "/^(\/?.*\/?)?www\/vendor\/bin\/phpunit$/",
],
'some path with extension' => [
0 => '/some/path/to/file.txt',
@@ -147,11 +152,13 @@ final class CoreLibsGetSystemTest extends TestCase
list ($host, $port) = \CoreLibs\Get\System::getHostName();
$this->assertEquals(
$expected_host,
$host
$host,
'failed expected host assert'
);
$this->assertEquals(
$expected_port,
$port
$port,
'faile expected port assert'
);
}
@@ -176,20 +183,38 @@ final class CoreLibsGetSystemTest extends TestCase
// default 0,
$this->assertEquals(
$expected_0,
\CoreLibs\Get\System::getPageName()
\CoreLibs\Get\System::getPageName(),
'failed default assert'
);
$this->assertEquals(
$expected_0,
\CoreLibs\Get\System::getPageName(\CoreLibs\Get\System::WITH_EXTENSION)
\CoreLibs\Get\System::getPageName(\CoreLibs\Get\System::WITH_EXTENSION),
'failed WITH_EXTESION assert'
);
$this->assertEquals(
$expected_1,
\CoreLibs\Get\System::getPageName(\CoreLibs\Get\System::NO_EXTENSION)
);
$this->assertEquals(
$expected_2,
\CoreLibs\Get\System::getPageName(\CoreLibs\Get\System::FULL_PATH)
\CoreLibs\Get\System::getPageName(\CoreLibs\Get\System::NO_EXTENSION),
'failed NO_EXTENSION assert'
);
// FULL PATH check can be equals or regex
$page_name_full_path = \CoreLibs\Get\System::getPageName(\CoreLibs\Get\System::FULL_PATH);
if (
substr($expected_2, 0, 1) == '/' &&
substr($expected_2, -1, 1) == '/'
) {
// this is regex
$this->assertMatchesRegularExpression(
$expected_2,
$page_name_full_path,
'failed FULL_PATH assert regex'
);
} else {
$this->assertEquals(
$expected_2,
$page_name_full_path,
'failed FULL_PATH assert equals'
);
}
}
}

View File

@@ -22,17 +22,36 @@ final class CoreLibsLanguageGetLocaleTest extends TestCase
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');
if (!defined('DEFAULT_ENCODING')) {
define('DEFAULT_ENCODING', 'UTF-8');
}
if (!defined('DEFAULT_LOCALE')) {
// default lang + encoding
define('DEFAULT_LOCALE', 'en_US.UTF-8');
}
// site
define('SITE_ENCODING', DEFAULT_ENCODING);
define('SITE_LOCALE', DEFAULT_LOCALE);
if (!defined('SITE_ENCODING')) {
define('SITE_ENCODING', DEFAULT_ENCODING);
}
if (!defined('SITE_LOCALE')) {
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);
if (!defined('BASE')) {
define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR);
}
if (!defined('INCLUDES')) {
define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR);
}
if (!defined('LANG')) {
define('LANG', 'lang' . DIRECTORY_SEPARATOR);
}
if (!defined('LOCALE')) {
define('LOCALE', 'locale' . DIRECTORY_SEPARATOR);
}
if (!defined('CONTENT_PATH')) {
define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR);
}
// array session
$_SESSION = [];
global $_SESSION;

View File

@@ -22,19 +22,37 @@ final class CoreLibsLanguageL10nTest extends TestCase
*/
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');
if (!defined('DEFAULT_ENCODING')) {
define('DEFAULT_ENCODING', 'UTF-8');
}
if (!defined('DEFAULT_LOCALE')) {
// default lang + encoding
define('DEFAULT_LOCALE', 'en_US.UTF-8');
}
// site
define('SITE_LANG', DEFAULT_LANG);
if (!defined('SITE_ENCODING')) {
define('SITE_ENCODING', DEFAULT_ENCODING);
}
if (!defined('SITE_LOCALE')) {
define('SITE_LOCALE', DEFAULT_LOCALE);
}
// just set
define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR);
define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR);
define('LANG', 'lang' . DIRECTORY_SEPARATOR);
define('LOCALE', 'locale' . DIRECTORY_SEPARATOR);
define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR);
if (!defined('BASE')) {
define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR);
}
if (!defined('INCLUDES')) {
define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR);
}
if (!defined('LANG')) {
define('LANG', 'lang' . DIRECTORY_SEPARATOR);
}
if (!defined('LOCALE')) {
define('LOCALE', 'locale' . DIRECTORY_SEPARATOR);
}
if (!defined('CONTENT_PATH')) {
define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR);
}
}
/**
@@ -527,7 +545,7 @@ final class CoreLibsLanguageL10nTest extends TestCase
} else {
$this->assertEquals(
$expected,
$l10n->__pn($context, $original_single, $original_plural, $n),
$l10n->__np($context, $original_single, $original_plural, $n),
'assert failed for plural: ' . $n . ' in context: ' . $context
);
}
@@ -949,7 +967,7 @@ final class CoreLibsLanguageL10nTest extends TestCase
/**
* fuctions check
* TODO: others d/dn/dp/dpn gettext functions
* TODO: others d/dn/dp/dnp gettext functions
*
* @covers __setlocale
* @covers __bindtextdomain
@@ -976,10 +994,10 @@ final class CoreLibsLanguageL10nTest extends TestCase
string $translated
): void {
\CoreLibs\Language\L10n::loadFunctions();
__setlocale(LC_MESSAGES, $locale);
__textdomain($domain);
__bindtextdomain($domain, $path);
__bind_textdomain_codeset($domain, $encoding);
_setlocale(LC_MESSAGES, $locale);
_textdomain($domain);
_bindtextdomain($domain, $path);
_bind_textdomain_codeset($domain, $encoding);
$this->assertEquals(
$translated,
__($original),
@@ -987,7 +1005,7 @@ final class CoreLibsLanguageL10nTest extends TestCase
);
$this->assertEquals(
$translated,
__gettext($original),
_gettext($original),
'function gettext assert failed'
);
}

View File

@@ -95,7 +95,7 @@ $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>";
. $l->__np($context, $single_string, $multi_string, $n) . "<br>";
}
// change domain
$domain = 'frontend';
@@ -138,6 +138,7 @@ echo "LOAD ERROR: " . $l->getLoadError() . "<br>";
echo "INPUT TEST: " . $string . " => " . $l->__($string) . "<br>";
echo "TROUGH LOAD: " . $l->getTranslatorClass()->gettext($string) . "<br>";
$lang = 'en';
$domain = 'admin';
echo "<br><b>STATIC TYPE TEST</b><br>";
// static tests from l10n_load
@@ -154,18 +155,30 @@ echo "<br><b>FUNCTIONS</b><br>";
// real statisc test
L10n::loadFunctions();
$locale = 'ja';
__setlocale(LC_MESSAGES, $locale);
__textdomain($domain);
__bindtextdomain($domain, $path);
__bind_textdomain_codeset($domain, $encoding);
_setlocale(LC_MESSAGES, $locale);
_textdomain($domain);
_bindtextdomain($domain, $path);
_bind_textdomain_codeset($domain, $encoding);
echo "INPUT TEST $locale: " . $string . " => " . __($string) . "<br>";
$single_string = 'single';
$multi_string = 'multi';
for ($n = 0; $n <= 3; $n++) {
echo "MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. _ngettext($single_string, $multi_string, $n) . "<br>";
}
$locale = 'en_US.UTF-8';
__setlocale(LC_MESSAGES, $locale);
__textdomain($domain);
__bindtextdomain($domain, $path);
__bind_textdomain_codeset($domain, $encoding);
_setlocale(LC_MESSAGES, $locale);
_textdomain($domain);
_bindtextdomain($domain, $path);
_bind_textdomain_codeset($domain, $encoding);
echo "INPUT TEST $locale: " . $string . " => " . __($string) . "<br>";
$single_string = 'single';
$multi_string = 'multi';
for ($n = 0; $n <= 3; $n++) {
echo "MULTI TEST $n: " . $single_string . "/" . $multi_string . " => "
. _ngettext($single_string, $multi_string, $n) . "<br>";
}
print "</body></html>";

View File

@@ -74,6 +74,7 @@ $smarty->setSmartyPaths();
// smarty test
$smarty->DATA['SMARTY_TEST'] = 'Test Data';
$smarty->DATA['TRANSLATE_TEST'] = $l10n->__('Are we translated?');
$smarty->DATA['TRANSLATE_TEST_FUNCTION'] = _gettext('Are we translated?');
$smarty->DATA['TRANSLATE_TEST_SMARTY'] = $smarty->l10n->__('Are we translated?');
$smarty->DATA['replace'] = 'Replaced';
// variable variables

View File

@@ -7,13 +7,19 @@
<div>
<b>Outside translation test</b><br>
TRANSLATION CLASS (OUT): {$TRANSLATE_TEST}<br>
TRANSLATION CLASS (SMARTY): {$TRANSLATE_TEST_SMARTY}
TRANSLATION CLASS (OUT FUNCTION): {$TRANSLATE_TEST_FUNCTION}<br>
TRANSLATION CLASS (SMARTY): {$TRANSLATE_TEST_SMARTY}<br>
</div>
<div>
<b>Translate Test with replace:</b><br>
ORIGINAL: Original with string: %1 ({$replace})<br>
TRANSLATED: {t 1=$replace}Original with string: %1{/t}<br>
TRANSLATED (escape): {t escape=on 1=$replace}Original with string: %1{/t}
TRANSLATED (escape): {t escape=on 1=$replace}Original with string: %1{/t}<br>
{capture assign="extra_title"}{t}INPUT TEST{/t}{/capture}
Capture test: {$extra_title}<br>
{section name=plural_test start=0 loop=3}
Plural test {$smarty.section.plural_test.index}: {t count=$smarty.section.plural_test.index plural="multi"}single{/t}<br>
{/section}
</div>
<div>
<b>Variable variables:</b><br>

View File

@@ -1,37 +0,0 @@
<div>
{$SMARTY_TEST}
</div>
<div>
TRANSLATION CLASS (OUT): {$TRANSLATE_TEST}
</div>
<div>
TRANSLATION CLASS (SMARTY): {$TRANSLATE_TEST_SMARTY}
</div>
<div>
<select id="drop_down_test" name="drop_down_test">
{html_options options=$drop_down_test selected=$drop_down_test_selected}
</select>
</div>
<div class="jq-container">
<div id="jq-test" class="jp-test">
<div id="test-div" class="test-div">
Some content here or asdfasdfasf
</div>
<div id="translate-div">
{* TRANSLATION SMARTY: {t}I should be translated{/t} *}
TRANSLATION SMARTY: I should be translated
</div>
</div>
</div>
<div class="loop-test">
<div>LOOP TEST</div>
{section name=page_list start=1 loop=$loop_start+1}
<div>LOOP OUTPUT: {$smarty.section.page_list.index}</div>
{/section}
</div>
{* progresss indicator *}
<div id="indicator"></div>
{* the action confirm box *}
<div id="actionBox" class="actionBoxElement"></div>
{* The Overlay box *}
<div id="overlayBox" class="overlayBoxElement"></div>

View File

@@ -885,13 +885,13 @@ class Login
$this->acl['show_ea_extra'] = false;
}
// set the default edit access
$this->acl['default_edit_access'] = $_SESSION['UNIT_DEFAULT'];
$this->acl['default_edit_access'] = $_SESSION['UNIT_DEFAULT'] ?? null;
// integrate the type acl list, but only for the keyword -> level
foreach ($this->default_acl_list as $level => $data) {
$this->acl['min'][$data['type']] = $level;
}
// set the full acl list too
$this->acl['acl_list'] = $_SESSION['DEFAULT_ACL_LIST'];
$this->acl['acl_list'] = $_SESSION['DEFAULT_ACL_LIST'] ?? [];
// debug
// $this->debug('ACL', $this->print_ar($this->acl));
}
@@ -1453,7 +1453,8 @@ EOM;
* checks that the given edit access id is valid for this user
* @param int|null $edit_access_id edit access id to check
* @return int|null same edit access id if ok
* or the default edit access id if given one is not valid
* or the default edit access id
* if given one is not valid
*/
public function loginCheckEditAccessId(?int $edit_access_id): ?int
{
@@ -1463,7 +1464,7 @@ EOM;
is_array($_SESSION['UNIT']) &&
!array_key_exists($edit_access_id, $_SESSION['UNIT'])
) {
return (int)$_SESSION['UNIT_DEFAULT'];
return $_SESSION['UNIT_DEFAULT'] ?? null;
} else {
return $edit_access_id;
}

View File

@@ -218,7 +218,8 @@ class ArrayHandler
continue;
}
foreach ($array as $key => $value) {
// if string or if key is assumed to be string do key match else add new entry
// if string or if key is assumed to be string do key match
// else add new entry
if (is_string($key) || $key_is_string === false) {
if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key])) {
// $merged[$key] = call_user_func(__METHOD__, $merged[$key], $value, $key_is_string);

View File

@@ -503,7 +503,7 @@ class ArrayIO extends \CoreLibs\DB\IO
// max id, falls INSERT
$q = 'SELECT MAX(' . $this->pk_name . ') + 1 AS pk_id FROM ' . $this->table_name;
if (is_array($res = $this->dbReturnRow($q))) {
$pk_id = $res['pkd_id'];
$pk_id = $res['pk_id'];
} else {
$pk_id = 1;
}

View File

@@ -5,7 +5,7 @@
* Language\Encoding::__mbMimeEncode -> Convert\MimeEncode::__mbMimeEncode
* Langauge\Encoding::checkConvertEncoding -> Check\Encoding::checkConvertEncoding
* Langauge\Encoding::setErrorChar -> Check\Encoding::setErrorChar
* Langauge\Encoding::getErrorChar -> Encoding::getErrorChar
* Langauge\Encoding::getErrorChar -> Check\Encoding::getErrorChar
* Langauge\Encoding::convertEncoding -> Convert\Encoding::convertEncoding
*/

View File

@@ -14,7 +14,7 @@
* __ : returns string (translated or original if not found)
* __n : plural string
* __p : string with context
* __pb: string with context and plural
* __np: string with context and plural
*
* HISTORY:
* 2022/4/15 (cs) drop all old folder layout support, new folder base
@@ -601,7 +601,7 @@ class L10n
* @param int $number number value
* @return string
*/
public function __pn(string $context, string $single, string $plural, int $number): string
public function __np(string $context, string $single, string $plural, int $number): string
{
if ($this->l10n === null) {
return $number > 1 ? $plural : $single;

View File

@@ -1,8 +1,16 @@
<?php
/*********************************************************************
* Original: https://github.com/phpmyadmin/motranslator
* Has the same function names, but uses a different base system
* setlocale -> setLocale
* bindtextdomain -> setTextDomain
* textdomain -> setDomain
*********************************************************************/
declare(strict_types=1);
use CoreLibs\Language\L10n;
use CoreLibs\Language\L10n as Loader;
/**
* Sets a requested locale.
@@ -12,9 +20,9 @@ use CoreLibs\Language\L10n;
*
* @return string Set or current locale
*/
function __setlocale(int $category, string $locale): string
function _setlocale(int $category, string $locale): string
{
return L10n::getInstance()->setLocale($locale);
return Loader::getInstance()->setLocale($locale);
}
/**
@@ -23,9 +31,9 @@ function __setlocale(int $category, string $locale): string
* @param string $domain Domain name
* @param string $path Path where to find locales
*/
function __bindtextdomain(string $domain, string $path): void
function _bindtextdomain(string $domain, string $path): void
{
L10n::getInstance()->setTextDomain($domain, $path);
Loader::getInstance()->setTextDomain($domain, $path);
}
/**
@@ -39,7 +47,7 @@ function __bindtextdomain(string $domain, string $path): void
* @param string $domain Domain where to set character set
* @param string $codeset Character set to set
*/
function __bind_textdomain_codeset(string $domain, string $codeset): void
function _bind_textdomain_codeset(string $domain, string $codeset): void
{
}
@@ -48,9 +56,9 @@ function __bind_textdomain_codeset(string $domain, string $codeset): void
*
* @param string $domain Domain name
*/
function __textdomain(string $domain): void
function _textdomain(string $domain): void
{
L10n::getInstance()->setDomain($domain);
Loader::getInstance()->setDomain($domain);
}
/**
@@ -60,9 +68,9 @@ function __textdomain(string $domain): void
*
* @return string translated string (or original, if not found)
*/
function __gettext(string $msgid): string
function _gettext(string $msgid): string
{
return L10n::getInstance()->getTranslator()->gettext(
return Loader::getInstance()->getTranslator()->gettext(
$msgid
);
}
@@ -76,7 +84,7 @@ function __gettext(string $msgid): string
*/
function __(string $msgid): string
{
return L10n::getInstance()->getTranslator()->gettext(
return Loader::getInstance()->getTranslator()->gettext(
$msgid
);
}
@@ -90,9 +98,9 @@ function __(string $msgid): string
*
* @return string translated plural form
*/
function __ngettext(string $msgid, string $msgidPlural, int $number): string
function _ngettext(string $msgid, string $msgidPlural, int $number): string
{
return L10n::getInstance()->getTranslator()->ngettext(
return Loader::getInstance()->getTranslator()->ngettext(
$msgid,
$msgidPlural,
$number
@@ -107,9 +115,9 @@ function __ngettext(string $msgid, string $msgidPlural, int $number): string
*
* @return string translated plural form
*/
function __pgettext(string $msgctxt, string $msgid): string
function _pgettext(string $msgctxt, string $msgid): string
{
return L10n::getInstance()->getTranslator()->pgettext(
return Loader::getInstance()->getTranslator()->pgettext(
$msgctxt,
$msgid
);
@@ -125,9 +133,9 @@ function __pgettext(string $msgctxt, string $msgid): string
*
* @return string translated plural form
*/
function __npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number): string
function _npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
return L10n::getInstance()->getTranslator()->npgettext(
return Loader::getInstance()->getTranslator()->npgettext(
$msgctxt,
$msgid,
$msgidPlural,
@@ -143,9 +151,9 @@ function __npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $n
*
* @return string translated string (or original, if not found)
*/
function __dgettext(string $domain, string $msgid): string
function _dgettext(string $domain, string $msgid): string
{
return L10n::getInstance()->getTranslator('', '', $domain)->gettext(
return Loader::getInstance()->getTranslator('', '', $domain)->gettext(
$msgid
);
}
@@ -160,9 +168,9 @@ function __dgettext(string $domain, string $msgid): string
*
* @return string translated plural form
*/
function __dngettext(string $domain, string $msgid, string $msgidPlural, int $number): string
function _dngettext(string $domain, string $msgid, string $msgidPlural, int $number): string
{
return L10n::getInstance()->getTranslator('', '', $domain)->ngettext(
return Loader::getInstance()->getTranslator('', '', $domain)->ngettext(
$msgid,
$msgidPlural,
$number
@@ -178,9 +186,9 @@ function __dngettext(string $domain, string $msgid, string $msgidPlural, int $nu
*
* @return string translated plural form
*/
function __dpgettext(string $domain, string $msgctxt, string $msgid): string
function _dpgettext(string $domain, string $msgctxt, string $msgid): string
{
return L10n::getInstance()->getTranslator('', '', $domain)->pgettext(
return Loader::getInstance()->getTranslator('', '', $domain)->pgettext(
$msgctxt,
$msgid
);
@@ -197,9 +205,9 @@ function __dpgettext(string $domain, string $msgctxt, string $msgid): string
*
* @return string translated plural form
*/
function __dnpgettext(string $domain, string $msgctxt, string $msgid, string $msgidPlural, int $number): string
function _dnpgettext(string $domain, string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
return L10n::getInstance()->getTranslator('', '', $domain)->npgettext(
return Loader::getInstance()->getTranslator('', '', $domain)->npgettext(
$msgctxt,
$msgid,
$msgidPlural,

View File

@@ -164,8 +164,6 @@ class SmartyExtend extends \Smarty
parent::__construct();
// iinit lang
$this->l10n = $l10n;
// opt load functions so we can use legacy init for smarty run perhaps
$this->l10n->loadFunctions();
// parse and read, legacy stuff
$this->encoding = $locale['encoding'];
$this->lang = $locale['lang'];
@@ -174,6 +172,13 @@ class SmartyExtend extends \Smarty
$this->domain = $this->l10n->getDomain();
$this->lang_dir = $this->l10n->getBaseLocalePath();
// opt load functions so we can use legacy init for smarty run perhaps
\CoreLibs\Language\L10n::loadFunctions();
_setlocale(LC_MESSAGES, $locale['locale']);
_textdomain($this->domain);
_bindtextdomain($this->domain, $this->lang_dir);
_bind_textdomain_codeset($this->domain, $this->encoding);
// register smarty variable
$this->registerPlugin('modifier', 'getvar', [&$this, 'getTemplateVars']);

View File

@@ -3,30 +3,19 @@
/**
* smarty-gettext.php - Gettext support for smarty
*
* ------------------------------------------------------------------------- *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* ------------------------------------------------------------------------- *
*
* To register as a smarty block function named 't', use:
* $smarty->register_block('t', 'smarty_translate');
*
* NOTE: native php support for conext sensitive does not exist
* Those jumps are disabled
*
* @package smarty-gettext
* @version $Id: block.t.php 4738 2022-05-06 01:28:48Z clemens $
* @link http://smarty-gettext.sf.net/
* @author Sagi Bashari <sagi@boom.org.il>
* @copyright 2004 Sagi Bashari
* @copyright Elan Ruusamäe
* @copyright Clemens Schwaighofer
*/
/**
@@ -39,7 +28,7 @@
*/
function smarty_gettext_strarg($str/*, $varargs... */)
{
$tr = array();
$tr = [];
$p = 0;
$nargs = func_num_args();
@@ -68,14 +57,18 @@ function smarty_gettext_strarg($str/*, $varargs... */)
* - escape - sets escape mode:
* - 'html' for HTML escaping, this is the default.
* - 'js' for javascript escaping.
* - 'url' for url escaping.
* - 'no'/'off'/0 - turns off escaping
* - plural - The plural version of the text (2nd parameter of ngettext())
* - count - The item count for plural mode (3rd parameter of ngettext())
* - domain - Textdomain to be used, default if skipped (dgettext() instead of gettext())
* - context - gettext context. reserved for future use.
*
*/
// cs modified: __ calls instead of direct gettext calls
function smarty_block_t($params, $text, $template, &$repeat)
function smarty_block_t($params, $text)
{
if (!isset($text)) {
return $text;
@@ -101,11 +94,75 @@ function smarty_block_t($params, $text, $template, &$repeat)
}
}
// get domain param
if (isset($params['domain'])) {
$domain = $params['domain'];
unset($params['domain']);
} else {
$domain = null;
}
// get context param
if (isset($params['context'])) {
$context = $params['context'];
unset($params['context']);
} else {
$context = null;
}
// use plural if required parameters are set
if (isset($count) && isset($plural)) {
$text = $template->l10n->__ngettext($text, $plural, $count);
if (isset($domain) && isset($context)) {
if (is_callable('_dnpgettext')) {
$text = _dnpgettext($domain, $context, $text, $plural, $count);
}/* elseif (is_callable('dnpgettext')) {
$text = dnpgettext($domain, $context, $text, $plural, $count);
} */
} elseif (isset($domain)) {
if (is_callable('_dngettext')) {
$text = _dngettext($domain, $text, $plural, $count);
} elseif (is_callable('dngettext')) {
$text = dngettext($domain, $text, $plural, $count);
}
} elseif (isset($context)) {
if (is_callable('_npgettext')) {
$text = _npgettext($context, $text, $plural, $count);
}/* elseif (is_callable('npgettext')) {
$text = npgettext($context, $text, $plural, $count);
} */
} else {
if (is_callable('_ngettext')) {
$text = _ngettext($text, $plural, $count);
} elseif (is_callable('ngettext')) {
$text = ngettext($text, $plural, $count);
}
}
} else { // use normal
$text = $template->l10n->__($text);
if (isset($domain) && isset($context)) {
if (is_callable('_dpgettext')) {
$text = _dpgettext($domain, $context, $text);
}/* elseif (is_callable('dpgettext')) {
$text = dpgettext($domain, $context, $text);
} */
} elseif (isset($domain)) {
if (is_callable('_dgettext')) {
$text = _dgettext($domain, $text);
} elseif (is_callable('dpgettext')) {
$text = dgettext($domain, $text);
}
} elseif (isset($context)) {
if (is_callable('_pgettext')) {
$text = _pgettext($context, $text);
}/* elseif (is_callable('pgettext')) {
$text = pgettext($context, $text);
} */
} else {
if (is_callable('_gettext')) {
$text = _gettext($text);
} elseif (is_callable('gettext')) {
$text = gettext($text);
}
}
}
// run strarg if there are parameters
@@ -115,6 +172,7 @@ function smarty_block_t($params, $text, $template, &$repeat)
switch ($escape) {
case 'html':
// default
$text = nl2br(htmlspecialchars($text));
break;
case 'javascript':
@@ -122,13 +180,29 @@ function smarty_block_t($params, $text, $template, &$repeat)
// javascript escape
$text = strtr(
$text,
array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/')
[
'\\' => '\\\\',
"'" => "\\'",
'"' => '\\"',
"\r" => '\\r',
"\n" => '\\n',
'</' => '<\/'
]
);
break;
case 'url':
// url escape
$text = urlencode($text);
break;
// below is a list for explicit OFF
case 'no':
case 'off':
case 'false':
case '0':
case 0:
// explicit OFF
default:
break;
}
return $text;