From 805c695d681577c73219f720188147857b174df5 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 15 Apr 2022 16:53:02 +0900 Subject: [PATCH] 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 --- 4dev/tests/CoreLibsLanguageL10nTest.php | 4 +- www/admin/class_test.lang.php | 2 +- www/admin/class_test.smarty.php | 1 + www/includes/templates/admin/smarty_test.tpl | 3 +- www/includes/templates/admin/smarty_test4.tpl | 37 ---------- www/lib/CoreLibs/Language/L10n.php | 2 +- www/lib/CoreLibs/Template/SmartyExtend.php | 9 ++- www/lib/smarty-4.1.0/libs/plugins/block.t.php | 69 ++++++++++++++++++- 8 files changed, 81 insertions(+), 46 deletions(-) delete mode 100644 www/includes/templates/admin/smarty_test4.tpl diff --git a/4dev/tests/CoreLibsLanguageL10nTest.php b/4dev/tests/CoreLibsLanguageL10nTest.php index 88d618e2..1b77751f 100644 --- a/4dev/tests/CoreLibsLanguageL10nTest.php +++ b/4dev/tests/CoreLibsLanguageL10nTest.php @@ -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 diff --git a/www/admin/class_test.lang.php b/www/admin/class_test.lang.php index 5e9ee78c..9aec8c54 100644 --- a/www/admin/class_test.lang.php +++ b/www/admin/class_test.lang.php @@ -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) . "
"; + . $l->__np($context, $single_string, $multi_string, $n) . "
"; } // change domain $domain = 'frontend'; diff --git a/www/admin/class_test.smarty.php b/www/admin/class_test.smarty.php index 95fd065f..f390d37e 100644 --- a/www/admin/class_test.smarty.php +++ b/www/admin/class_test.smarty.php @@ -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 diff --git a/www/includes/templates/admin/smarty_test.tpl b/www/includes/templates/admin/smarty_test.tpl index 17a6a7f0..2b83a97c 100644 --- a/www/includes/templates/admin/smarty_test.tpl +++ b/www/includes/templates/admin/smarty_test.tpl @@ -7,7 +7,8 @@
Outside translation test
TRANSLATION CLASS (OUT): {$TRANSLATE_TEST}
- TRANSLATION CLASS (SMARTY): {$TRANSLATE_TEST_SMARTY} + TRANSLATION CLASS (OUT FUNCTION): {$TRANSLATE_TEST_FUNCTION}
+ TRANSLATION CLASS (SMARTY): {$TRANSLATE_TEST_SMARTY}
Translate Test with replace:
diff --git a/www/includes/templates/admin/smarty_test4.tpl b/www/includes/templates/admin/smarty_test4.tpl deleted file mode 100644 index f9300036..00000000 --- a/www/includes/templates/admin/smarty_test4.tpl +++ /dev/null @@ -1,37 +0,0 @@ -
- {$SMARTY_TEST} -
-
- TRANSLATION CLASS (OUT): {$TRANSLATE_TEST} -
-
- TRANSLATION CLASS (SMARTY): {$TRANSLATE_TEST_SMARTY} -
-
- -
-
- -
-
-
LOOP TEST
-{section name=page_list start=1 loop=$loop_start+1} -
LOOP OUTPUT: {$smarty.section.page_list.index}
-{/section} -
-{* progresss indicator *} -
-{* the action confirm box *} -
-{* The Overlay box *} -
diff --git a/www/lib/CoreLibs/Language/L10n.php b/www/lib/CoreLibs/Language/L10n.php index f3d48a1e..a86065bc 100644 --- a/www/lib/CoreLibs/Language/L10n.php +++ b/www/lib/CoreLibs/Language/L10n.php @@ -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; diff --git a/www/lib/CoreLibs/Template/SmartyExtend.php b/www/lib/CoreLibs/Template/SmartyExtend.php index b692791a..8497b005 100644 --- a/www/lib/CoreLibs/Template/SmartyExtend.php +++ b/www/lib/CoreLibs/Template/SmartyExtend.php @@ -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']); diff --git a/www/lib/smarty-4.1.0/libs/plugins/block.t.php b/www/lib/smarty-4.1.0/libs/plugins/block.t.php index 9f6973c9..8cba787c 100644 --- a/www/lib/smarty-4.1.0/libs/plugins/block.t.php +++ b/www/lib/smarty-4.1.0/libs/plugins/block.t.php @@ -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