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 12 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
98 changes: 6 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,11 @@
<code><![CDATA[$container]]></code>
</ParamNameMismatch>
</file>
<file src="src/Translator/PlaceholderPluginManagerFactory.php">
<MixedArgument>
<code><![CDATA[$config['translator_placeholders']]]></code>
</MixedArgument>
</file>
<file src="src/Translator/Plural/Parser.php">
<MixedArgument>
<code><![CDATA[$id]]></code>
Expand Down Expand Up @@ -308,10 +283,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 +301,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 +370,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
59 changes: 32 additions & 27 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ 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\PlaceholderPluginManager::class => Translator\PlaceholderPluginManagerFactory::class,
Geography\DefaultCountryCodeList::class => [Geography\DefaultCountryCodeList::class, 'create'],
],
];
}
Expand Down Expand Up @@ -159,23 +160,25 @@ public function getViewHelperConfig()
{
return [
'aliases' => [
'countryCodeDataList' => View\Helper\CountryCodeDataList::class,
'currencyformat' => View\Helper\CurrencyFormat::class,
'currencyFormat' => View\Helper\CurrencyFormat::class,
'CurrencyFormat' => View\Helper\CurrencyFormat::class,
'dateformat' => View\Helper\DateFormat::class,
'dateFormat' => View\Helper\DateFormat::class,
'DateFormat' => View\Helper\DateFormat::class,
'numberformat' => View\Helper\NumberFormat::class,
'numberFormat' => View\Helper\NumberFormat::class,
'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,
'countryCodeDataList' => View\Helper\CountryCodeDataList::class,
'currencyformat' => View\Helper\CurrencyFormat::class,
'currencyFormat' => View\Helper\CurrencyFormat::class,
'CurrencyFormat' => View\Helper\CurrencyFormat::class,
'dateformat' => View\Helper\DateFormat::class,
'dateFormat' => View\Helper\DateFormat::class,
'DateFormat' => View\Helper\DateFormat::class,
'numberformat' => View\Helper\NumberFormat::class,
'numberFormat' => View\Helper\NumberFormat::class,
'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,
'translateWithParams' => View\Helper\TranslateWithParams::class,
'translatePluralWithParams' => View\Helper\TranslatePluralWithParams::class,
TotalWipeOut marked this conversation as resolved.
Show resolved Hide resolved

// Legacy Zend Framework aliases
'Zend\I18n\View\Helper\CurrencyFormat' => View\Helper\CurrencyFormat::class,
Expand All @@ -186,13 +189,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
11 changes: 5 additions & 6 deletions src/Translator/Loader/Gettext.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,14 @@ protected function readInteger()
/**
* Read an integer from the current file.
*
* @param int $num
* @return int
* @param int $num
* @return array<array-key, int>
*/
protected function readIntegerList($num)
{
if ($this->littleEndian) {
return unpack('V' . $num, fread($this->file, 4 * $num));
}
/** @var array<array-key, int>|false $integerList */
$integerList = unpack(($this->littleEndian ? 'V' : 'N') . $num, fread($this->file, 4 * $num));

return unpack('N' . $num, fread($this->file, 4 * $num));
return $integerList === false ? [] : $integerList;
}
}
21 changes: 11 additions & 10 deletions src/Translator/LoaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,21 @@ class LoaderPluginManager extends AbstractPluginManager
* Checks that the filter loaded is an instance of
* Loader\FileLoaderInterface or Loader\RemoteLoaderInterface.
*
* @param mixed $plugin
* @param mixed $instance
* @return void
* @throws Exception\RuntimeException If invalid.
* @psalm-assert InstanceType $plugin
* @psalm-assert InstanceType $instance
*/
public function validate($plugin)
public function validate($instance)
{
if ($plugin instanceof FileLoaderInterface || $plugin instanceof RemoteLoaderInterface) {
if ($instance instanceof FileLoaderInterface || $instance instanceof RemoteLoaderInterface) {
// we're okay
return;
}

throw new InvalidServiceException(sprintf(
'Plugin of type %s is invalid; must implement %s or %s',
is_object($plugin) ? $plugin::class : gettype($plugin),
is_object($instance) ? $instance::class : gettype($instance),
FileLoaderInterface::class,
RemoteLoaderInterface::class
));
Expand All @@ -126,18 +126,19 @@ public function validate($plugin)
* @deprecated Since 2.16.0 - This component is no longer compatible with Service Manager v2.
* This method will be removed in version 3.0
*
* @param mixed $plugin
* @param mixed $instance
* @return void
* @throws Exception\RuntimeException
* @psalm-assert InstanceType $plugin
* @psalm-assert InstanceType $instance
*/
public function validatePlugin($plugin)
public function validatePlugin($instance)
{
try {
$this->validate($plugin);
$this->validate($instance);
} catch (InvalidServiceException $e) {
throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s or %s',
is_object($plugin) ? $plugin::class : gettype($plugin),
is_object($instance) ? $instance::class : gettype($instance),
FileLoaderInterface::class,
RemoteLoaderInterface::class
));
Expand Down
20 changes: 20 additions & 0 deletions src/Translator/Placeholder/HandlebarPlaceholder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Laminas\I18n\Translator\Placeholder;

use function str_replace;

class HandlebarPlaceholder implements PlaceholderInterface
Copy link
Member

Choose a reason for hiding this comment

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

The naming is still wrong because it is an formatter which formats a message. A placeholder is in the message: "Hello {name}!" – here {name}.

Copy link
Author

@TotalWipeOut TotalWipeOut May 22, 2024

Choose a reason for hiding this comment

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

@froschdesign so then from this, shall i rename things like this:

  • namespace to be Laminas\I18n\Translator\Formatter
  • PlaceholderPluginManager to FormatterPluginManager, same with factory
  • PlaceholderInterface to FormatterInterface
  • and all class suffixes to *Formatter

or, if you have something else in mind, please advise

{
public function compile(string $locale, string $message, iterable $placeholders = []): string
{
$compiled = $message;
foreach ($placeholders as $key => $value) {
$compiled = str_replace("{{{$key}}}", $value, $compiled);
}

return $compiled;
}
}
Loading