Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Translation message placeholder support #121

Draft
wants to merge 18 commits into
base: 2.27.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 12 additions & 92 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.22.2@d768d914152dbbf3486c36398802f74e80cfde48">
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<file src="src/Exception/ExtensionNotLoadedException.php">
<UnusedClass>
<code><![CDATA[ExtensionNotLoadedException]]></code>
Expand Down Expand Up @@ -55,41 +55,18 @@
</RedundantCastGivenDocblockType>
</file>
<file src="src/Translator/Loader/Gettext.php">
<InvalidArrayAccess>
<code><![CDATA[$originalStringTable[$offsetKey]]]></code>
<code><![CDATA[$originalStringTable[$sizeKey]]]></code>
<code><![CDATA[$translationStringTable[$offsetKey]]]></code>
<code><![CDATA[$translationStringTable[$sizeKey]]]></code>
</InvalidArrayAccess>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$this->file]]></code>
<code><![CDATA[$this->file]]></code>
<code><![CDATA[$this->file]]></code>
</InvalidPropertyAssignmentValue>
<InvalidReturnStatement>
<code><![CDATA[unpack('N' . $num, fread($this->file, 4 * $num))]]></code>
<code><![CDATA[unpack('V' . $num, fread($this->file, 4 * $num))]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[int]]></code>
</InvalidReturnType>
<MissingConstructor>
<code><![CDATA[$file]]></code>
<code><![CDATA[$littleEndian]]></code>
</MissingConstructor>
<MixedArgument>
<code><![CDATA[$originalStringOffset]]></code>
<code><![CDATA[$originalStringSize]]></code>
<code><![CDATA[$textDomain['']]]></code>
<code><![CDATA[$translationStringOffset]]></code>
<code><![CDATA[$translationStringSize]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$originalStringOffset]]></code>
<code><![CDATA[$originalStringSize]]></code>
<code><![CDATA[$translationStringOffset]]></code>
<code><![CDATA[$translationStringSize]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[int]]></code>
</MixedInferredReturnType>
Expand Down Expand Up @@ -142,13 +119,6 @@
</MixedArrayAccess>
</file>
<file src="src/Translator/LoaderPluginManager.php">
<MissingReturnType>
<code><![CDATA[validatePlugin]]></code>
</MissingReturnType>
<ParamNameMismatch>
<code><![CDATA[$plugin]]></code>
<code><![CDATA[$plugin]]></code>
</ParamNameMismatch>
<PossiblyUnusedMethod>
<code><![CDATA[validatePlugin]]></code>
</PossiblyUnusedMethod>
Expand Down Expand Up @@ -177,6 +147,17 @@
<code><![CDATA[$container]]></code>
</ParamNameMismatch>
</file>
<file src="src/Translator/FormatterPluginManagerFactory.php">
<DeprecatedClass>
<code><![CDATA[new Config($config['translator_formatter'])]]></code>
</DeprecatedClass>
<MixedArgument>
<code><![CDATA[$config['translator_formatter']]]></code>
</MixedArgument>
<MoreSpecificImplementedParamType>
<code><![CDATA[$options]]></code>
</MoreSpecificImplementedParamType>
</file>
<file src="src/Translator/Plural/Parser.php">
<MixedArgument>
<code><![CDATA[$id]]></code>
Expand Down Expand Up @@ -308,10 +289,6 @@
<code><![CDATA[$second]]></code>
<code><![CDATA[$third]]></code>
</PropertyNotSetInConstructor>
<UnnecessaryVarAnnotation>
<code><![CDATA[callable]]></code>
<code><![CDATA[callable]]></code>
</UnnecessaryVarAnnotation>
</file>
<file src="src/Translator/TextDomain.php">
<PossiblyNullArgument>
Expand All @@ -330,106 +307,60 @@
</DocblockTypeContradiction>
<MissingClosureParamType>
<code><![CDATA[$r]]></code>
<code><![CDATA[$r]]></code>
</MissingClosureParamType>
<MissingConstructor>
<code><![CDATA[$events]]></code>
<code><![CDATA[$pluginManager]]></code>
</MissingConstructor>
<MixedArgument>
<code><![CDATA[$cacheId]]></code>
<code><![CDATA[$file['filename']]]></code>
<code><![CDATA[$file['filename']]]></code>
<code><![CDATA[$file['filename']]]></code>
<code><![CDATA[$file['locale'] ?? null]]></code>
<code><![CDATA[$file['text_domain'] ?? 'default']]></code>
<code><![CDATA[$file['type']]]></code>
<code><![CDATA[$file['type']]]></code>
<code><![CDATA[$loaderType]]></code>
<code><![CDATA[$options['cache']]]></code>
<code><![CDATA[$pattern['base_dir']]]></code>
<code><![CDATA[$pattern['pattern']]]></code>
<code><![CDATA[$pattern['pattern']]]></code>
<code><![CDATA[$pattern['text_domain'] ?? 'default']]></code>
<code><![CDATA[$pattern['type']]]></code>
<code><![CDATA[$pattern['type']]]></code>
<code><![CDATA[$remote['text_domain'] ?? 'default']]></code>
<code><![CDATA[$remote['type']]]></code>
<code><![CDATA[array_shift($locales)]]></code>
<code><![CDATA[array_shift($locales)]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$file['filename']]]></code>
<code><![CDATA[$file['filename']]]></code>
<code><![CDATA[$file['filename']]]></code>
<code><![CDATA[$file['locale']]]></code>
<code><![CDATA[$file['text_domain']]]></code>
<code><![CDATA[$file['type']]]></code>
<code><![CDATA[$file['type']]]></code>
<code><![CDATA[$pattern['baseDir']]]></code>
<code><![CDATA[$pattern['base_dir']]]></code>
<code><![CDATA[$pattern['pattern']]]></code>
<code><![CDATA[$pattern['pattern']]]></code>
<code><![CDATA[$pattern['text_domain']]]></code>
<code><![CDATA[$pattern['type']]]></code>
<code><![CDATA[$pattern['type']]]></code>
<code><![CDATA[$remote['text_domain']]]></code>
<code><![CDATA[$remote['type']]]></code>
<code><![CDATA[$this->files[$textDomain][$currentLocale]]]></code>
<code><![CDATA[$this->files[$textDomain][$currentLocale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale][$message]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale][$textDomain . "\x04" . $message]]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$this->files[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale]]]></code>
<code><![CDATA[$this->patterns[$textDomain][]]]></code>
<code><![CDATA[$this->remote[$textDomain][]]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code><![CDATA[$file]]></code>
<code><![CDATA[$file]]></code>
<code><![CDATA[$last]]></code>
<code><![CDATA[$last]]></code>
<code><![CDATA[$loaderType]]></code>
<code><![CDATA[$pattern]]></code>
<code><![CDATA[$pattern]]></code>
<code><![CDATA[$remote]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[string|null]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[merge]]></code>
<code><![CDATA[merge]]></code>
<code><![CDATA[merge]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$pattern['baseDir']]]></code>
</MixedOperand>
<MixedReturnStatement>
<code><![CDATA[$this->messages[$textDomain][$locale][$message]]]></code>
<code><![CDATA[$this->messages[$textDomain][$locale][$textDomain . "\x04" . $message]]]></code>
</MixedReturnStatement>
<PossiblyNullReference>
<code><![CDATA[evaluate]]></code>
</PossiblyNullReference>
<PossiblyUndefinedVariable>
<code><![CDATA[$cacheId]]></code>
</PossiblyUndefinedVariable>
<RedundantCondition>
<code><![CDATA[$translation[$index] !== null]]></code>
<code><![CDATA[isset($translation[$index]) && $translation[$index] !== '' && $translation[$index] !== null]]></code>
</RedundantCondition>
<UnsafeInstantiation>
<code><![CDATA[new static()]]></code>
</UnsafeInstantiation>
Expand All @@ -445,20 +376,9 @@
</PossiblyUnusedReturnValue>
</file>
<file src="src/Translator/TranslatorServiceFactory.php">
<DeprecatedInterface>
<code><![CDATA[TranslatorServiceFactory]]></code>
</DeprecatedInterface>
<MixedArgument>
<code><![CDATA[$container->get('TranslatorPluginManager')]]></code>
<code><![CDATA[$trConfig]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['translator']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code><![CDATA[$config]]></code>
<code><![CDATA[$trConfig]]></code>
</MixedAssignment>
</file>
<file src="src/Validator/Alnum.php">
<RedundantCastGivenDocblockType>
Expand Down
34 changes: 19 additions & 15 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public function getDependencyConfig()
Geography\CountryCodeListInterface::class => Geography\DefaultCountryCodeList::class,
],
'factories' => [
Translator\TranslatorInterface::class => Translator\TranslatorServiceFactory::class,
Translator\LoaderPluginManager::class => Translator\LoaderPluginManagerFactory::class,
Geography\DefaultCountryCodeList::class => [Geography\DefaultCountryCodeList::class, 'create'],
Translator\TranslatorInterface::class => Translator\TranslatorServiceFactory::class,
Translator\LoaderPluginManager::class => Translator\LoaderPluginManagerFactory::class,
Translator\FormatterPluginManager::class => Translator\FormatterPluginManagerFactory::class,
Translator\TranslatorFormatterDecorator::class => Translator\TranslatorFormatterDecoratorFactory::class,
Geography\DefaultCountryCodeList::class => [Geography\DefaultCountryCodeList::class, 'create'],
],
];
}
Expand Down Expand Up @@ -171,11 +173,11 @@ public function getViewHelperConfig()
'NumberFormat' => View\Helper\NumberFormat::class,
'plural' => View\Helper\Plural::class,
'Plural' => View\Helper\Plural::class,
'translate' => View\Helper\Translate::class,
'Translate' => View\Helper\Translate::class,
'translateplural' => View\Helper\TranslatePlural::class,
'translatePlural' => View\Helper\TranslatePlural::class,
'TranslatePlural' => View\Helper\TranslatePlural::class,
'translate' => View\Helper\TranslateWithParams::class,
'Translate' => View\Helper\TranslateWithParams::class,
'translateplural' => View\Helper\TranslatePluralWithParams::class,
'translatePlural' => View\Helper\TranslatePluralWithParams::class,
'TranslatePlural' => View\Helper\TranslatePluralWithParams::class,

// Legacy Zend Framework aliases
'Zend\I18n\View\Helper\CurrencyFormat' => View\Helper\CurrencyFormat::class,
Expand All @@ -186,13 +188,15 @@ public function getViewHelperConfig()
'Zend\I18n\View\Helper\TranslatePlural' => View\Helper\TranslatePlural::class,
],
'factories' => [
View\Helper\CountryCodeDataList::class => View\Helper\Container\CountryCodeDataListFactory::class,
View\Helper\CurrencyFormat::class => InvokableFactory::class,
View\Helper\DateFormat::class => InvokableFactory::class,
View\Helper\NumberFormat::class => InvokableFactory::class,
View\Helper\Plural::class => InvokableFactory::class,
View\Helper\Translate::class => InvokableFactory::class,
View\Helper\TranslatePlural::class => InvokableFactory::class,
View\Helper\CountryCodeDataList::class => View\Helper\Container\CountryCodeDataListFactory::class,
View\Helper\CurrencyFormat::class => InvokableFactory::class,
View\Helper\DateFormat::class => InvokableFactory::class,
View\Helper\NumberFormat::class => InvokableFactory::class,
View\Helper\Plural::class => InvokableFactory::class,
View\Helper\Translate::class => InvokableFactory::class,
View\Helper\TranslatePlural::class => InvokableFactory::class,
View\Helper\TranslateWithParams::class => InvokableFactory::class,
View\Helper\TranslatePluralWithParams::class => InvokableFactory::class,
Comment on lines +198 to +199
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will provide the delegator to add the decorator for the new view helpers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I was going to ask regarding that. That would be great.
Thanks for all your time on this!

],
];
}
Expand Down
13 changes: 13 additions & 0 deletions src/Translator/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Laminas\I18n\Translator\Formatter;

interface FormatterInterface
{
/**
* @param iterable<array-key, string> $params
*/
public function format(string $locale, string $message, iterable $params = []): string;
}
20 changes: 20 additions & 0 deletions src/Translator/Formatter/HandlebarFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Laminas\I18n\Translator\Formatter;

use function str_replace;

class HandlebarFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $params = []): string
{
$compiled = $message;
foreach ($params as $key => $value) {
$compiled = str_replace("{{{$key}}}", $value, $compiled);
}

return $compiled;
}
}
22 changes: 22 additions & 0 deletions src/Translator/Formatter/IcuFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Laminas\I18n\Translator\Formatter;

use MessageFormatter;
use Traversable;

use function iterator_to_array;

class IcuFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $params = []): string
{
if ($params instanceof Traversable) {
$params = iterator_to_array($params);
}

return MessageFormatter::formatMessage($locale, $message, $params);
}
}
31 changes: 31 additions & 0 deletions src/Translator/Formatter/PrintfFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Laminas\I18n\Translator\Formatter;

use Laminas\I18n\Exception\ParseException;
use Traversable;

use function call_user_func_array;
use function iterator_to_array;

class PrintfFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $params = []): string
{
if ($params instanceof Traversable) {
$params = iterator_to_array($params);
}

/** @var string|false $compiled */
$compiled = call_user_func_array('vsprintf', [$message, $params]);
if ($compiled === false) {
throw new ParseException(
'Error occurred while processing sprintf placeholders for message "' . $message . '"'
);
}

return $compiled;
}
}
Loading