Compare commits

...

1 Commits

Author SHA1 Message Date
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
8 changed files with 81 additions and 46 deletions

View File

@@ -545,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
);
}
@@ -967,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

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';

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,7 +7,8 @@
<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>

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

@@ -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

@@ -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
$this->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

@@ -101,11 +101,76 @@ 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 (isset($domain)) {
if (is_callable('__dngettext')) {
$text = __dngettext($domain, $text, $plural, $count);
}
} elseif (isset($context)) {
if (is_callable('__npgettext')) {
$text == __npgettext($context, $text, $plural, $count);
} elseif (
$template->l10n instanceof \CoreLibs\Language\L10n &&
method_exists($template->l10n, '__pn')
) {
$text = $template->l10n->__pn($text, $plural, $count);
}
} elseif (
$template->l10n instanceof \CoreLibs\Language\L10n &&
method_exists($template->l10n, '__n')
) {
$text = $template->l10n->__n($text, $plural, $count);
// $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 (isset($domain)) {
if (is_callable('__dgettext')) {
$text = __dgettext($domain, $text);
}
} elseif (isset($context)) {
if (is_callable('__pgettext')) {
$text = __pgettext($context, $text);
} elseif (
$template->l10n instanceof \CoreLibs\Language\L10n &&
method_exists($template->l10n, '__p')
) {
$text = $template->l10n->__p($context, $text);
}
} else {
if (is_callable('__gettext')) {
$text = __gettext($text);
} elseif (
$template->l10n instanceof \CoreLibs\Language\L10n &&
method_exists($template->l10n, '__')
) {
$text = $template->l10n->__($text);
}
}
}
// run strarg if there are parameters