diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1d031182..c0e89ab0 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -147,10 +147,16 @@ - + + + + - + + + + diff --git a/src/Translator/Formatter/FormatterInterface.php b/src/Translator/Formatter/FormatterInterface.php index f7b62b66..2f5dabc9 100644 --- a/src/Translator/Formatter/FormatterInterface.php +++ b/src/Translator/Formatter/FormatterInterface.php @@ -7,7 +7,7 @@ interface FormatterInterface { /** - * @param iterable $placeholders + * @param iterable $params */ - public function format(string $locale, string $message, iterable $placeholders = []): string; + public function format(string $locale, string $message, iterable $params = []): string; } diff --git a/src/Translator/Formatter/HandlebarFormatter.php b/src/Translator/Formatter/HandlebarFormatter.php index 8fe33e00..e89aa8d5 100644 --- a/src/Translator/Formatter/HandlebarFormatter.php +++ b/src/Translator/Formatter/HandlebarFormatter.php @@ -8,10 +8,10 @@ class HandlebarFormatter implements FormatterInterface { - public function format(string $locale, string $message, iterable $placeholders = []): string + public function format(string $locale, string $message, iterable $params = []): string { $compiled = $message; - foreach ($placeholders as $key => $value) { + foreach ($params as $key => $value) { $compiled = str_replace("{{{$key}}}", $value, $compiled); } diff --git a/src/Translator/Formatter/IcuFormatter.php b/src/Translator/Formatter/IcuFormatter.php index f45e1d25..b92f1de3 100644 --- a/src/Translator/Formatter/IcuFormatter.php +++ b/src/Translator/Formatter/IcuFormatter.php @@ -11,12 +11,12 @@ class IcuFormatter implements FormatterInterface { - public function format(string $locale, string $message, iterable $placeholders = []): string + public function format(string $locale, string $message, iterable $params = []): string { - if ($placeholders instanceof Traversable) { - $placeholders = iterator_to_array($placeholders); + if ($params instanceof Traversable) { + $params = iterator_to_array($params); } - return MessageFormatter::formatMessage($locale, $message, $placeholders); + return MessageFormatter::formatMessage($locale, $message, $params); } } diff --git a/src/Translator/Formatter/PrintfFormatter.php b/src/Translator/Formatter/PrintfFormatter.php index 95058234..a8d415af 100644 --- a/src/Translator/Formatter/PrintfFormatter.php +++ b/src/Translator/Formatter/PrintfFormatter.php @@ -12,14 +12,14 @@ class PrintfFormatter implements FormatterInterface { - public function format(string $locale, string $message, iterable $placeholders = []): string + public function format(string $locale, string $message, iterable $params = []): string { - if ($placeholders instanceof Traversable) { - $placeholders = iterator_to_array($placeholders); + if ($params instanceof Traversable) { + $params = iterator_to_array($params); } /** @var string|false $compiled */ - $compiled = call_user_func_array('vsprintf', [$message, $placeholders]); + $compiled = call_user_func_array('vsprintf', [$message, $params]); if ($compiled === false) { throw new ParseException( 'Error occurred while processing sprintf placeholders for message "' . $message . '"' diff --git a/src/Translator/Formatter/SegmentFormatter.php b/src/Translator/Formatter/SegmentFormatter.php index f8138e78..1129c898 100644 --- a/src/Translator/Formatter/SegmentFormatter.php +++ b/src/Translator/Formatter/SegmentFormatter.php @@ -19,17 +19,17 @@ class SegmentFormatter implements FormatterInterface { - public function format(string $locale, string $message, iterable $placeholders = []): string + public function format(string $locale, string $message, iterable $params = []): string { - if ($placeholders instanceof Traversable) { - $placeholders = iterator_to_array($placeholders); + if ($params instanceof Traversable) { + $params = iterator_to_array($params); } - if (empty($placeholders)) { + if (empty($params)) { return $message; } - if (! ArrayUtils::hasStringKeys($placeholders)) { + if (! ArrayUtils::hasStringKeys($params)) { throw new InvalidArgumentException( 'SegmentPlaceholder expects an associative array of placeholder names and values' ); @@ -38,12 +38,12 @@ public function format(string $locale, string $message, iterable $placeholders = try { // Sorting the array by key length to replace placeholders with longer names first // to avoid replacing placeholders with shorter names that are part of longer names - uksort($placeholders, static function (string|int $a, string|int $b) { + uksort($params, static function (string|int $a, string|int $b) { return strlen((string) $a) <=> strlen((string) $b); }); $compiled = $message; - foreach ($placeholders as $key => $value) { + foreach ($params as $key => $value) { $key = (string) $key; $compiled = str_replace([':' . $key, ':' . strtoupper($key), ':' . ucfirst($key)], [ $value, diff --git a/src/Translator/FormatterPluginManager.php b/src/Translator/FormatterPluginManager.php index 3484be16..e48c0eb3 100644 --- a/src/Translator/FormatterPluginManager.php +++ b/src/Translator/FormatterPluginManager.php @@ -13,11 +13,11 @@ use function sprintf; /** - * Plugin manager implementation for translation placeholder compilers. + * Plugin manager implementation for translation message formatters. * * Enforces that placeholder compilers retrieved are either instances of - * Placeholder\PlaceholderInterface. Additionally, it registers a number - * of default placeholder compilers. + * Formatter\FormatterInterface. Additionally, it registers a number + * of default message formatters. * * @template InstanceType of Formatter\FormatterInterface * @extends AbstractPluginManager @@ -49,8 +49,7 @@ class FormatterPluginManager extends AbstractPluginManager /** * Validate the plugin. * - * Checks that the filter loaded is an instance of - * Loader\FileLoaderInterface or Loader\RemoteLoaderInterface. + * Checks that the filter loaded is an instance of Formatter\FormatterInterface * * @throws Exception\RuntimeException If invalid. * @psalm-assert RemoteLoaderInterface $instance diff --git a/src/Translator/FormatterPluginManagerFactory.php b/src/Translator/FormatterPluginManagerFactory.php index 98629a14..d7e4c2b5 100644 --- a/src/Translator/FormatterPluginManagerFactory.php +++ b/src/Translator/FormatterPluginManagerFactory.php @@ -16,10 +16,9 @@ final class FormatterPluginManagerFactory implements FactoryInterface { /** * @param string $requestedName - * @param array|null $options + * @psalm-param ServiceManagerConfiguration|null $options * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface - * @psalm-suppress DeprecatedClass */ public function __invoke( ContainerInterface $container, diff --git a/src/Translator/TranslatorFormatterDecorator.php b/src/Translator/TranslatorFormatterDecorator.php index 3663fc25..70358904 100644 --- a/src/Translator/TranslatorFormatterDecorator.php +++ b/src/Translator/TranslatorFormatterDecorator.php @@ -3,6 +3,10 @@ namespace Laminas\I18n\Translator; use Laminas\I18n\Translator\Formatter\FormatterInterface; +use Locale; + +use function is_string; +use function method_exists; final class TranslatorFormatterDecorator implements TranslatorInterface { @@ -24,7 +28,9 @@ public function translate( $locale = null, iterable $params = [] ): string { - $locale ??= $this->translator->getLocale(); + if ($locale === null) { + $locale = $this->getLocale(); + } return $this->formatMessage($this->translator->translate($message, $textDomain, $locale), $params, $locale); } @@ -33,8 +39,8 @@ public function translate( * @param string $singular * @param string $plural * @param int $number - * @param string $textDomain - * @param string $locale + * @param string|null $textDomain + * @param string|null $locale * @param iterable $params */ public function translatePlural( @@ -45,7 +51,9 @@ public function translatePlural( $locale = null, iterable $params = [] ): string { - $locale ??= $this->translator->getLocale(); + if ($locale === null) { + $locale = $this->getLocale(); + } return $this->formatMessage( $this->translatePlural($singular, $plural, $number, $textDomain, $locale), @@ -55,10 +63,24 @@ public function translatePlural( } /** - * @param iterable $placeholders + * @param iterable $params */ - protected function formatMessage(string $message, iterable $placeholders, string $locale): string + protected function formatMessage(string $message, iterable $params, string $locale): string { - return $message !== '' ? $this->formatter->format($locale, $message, $placeholders) : $message; + return $message !== '' ? $this->formatter->format($locale, $message, $params) : $message; + } + + protected function getLocale(): string + { + $locale = null; + if (method_exists($this->translator, 'getLocale')) { + /** @var string|null $translatorLocale */ + $translatorLocale = $this->translator->getLocale(); + if (is_string($translatorLocale)) { + $locale = $translatorLocale; + } + } + + return $locale ?? Locale::getDefault(); } } diff --git a/src/View/Helper/TranslatePluralWithParams.php b/src/View/Helper/TranslatePluralWithParams.php index 88cf5e20..6f67a331 100644 --- a/src/View/Helper/TranslatePluralWithParams.php +++ b/src/View/Helper/TranslatePluralWithParams.php @@ -4,7 +4,6 @@ use Laminas\I18n\Exception; use Laminas\I18n\Translator\TranslatorFormatterDecorator; -use Laminas\I18n\Translator\TranslatorWithParamsInterface; /** * View helper for translating messages with placeholders. diff --git a/src/View/Helper/TranslateWithParams.php b/src/View/Helper/TranslateWithParams.php index c7a18d80..5643c7d6 100644 --- a/src/View/Helper/TranslateWithParams.php +++ b/src/View/Helper/TranslateWithParams.php @@ -4,7 +4,6 @@ use Laminas\I18n\Exception; use Laminas\I18n\Translator\TranslatorFormatterDecorator; -use Laminas\I18n\Translator\TranslatorWithParamsInterface; /** * View helper for translating messages with placeholders.