Skip to content

Commit

Permalink
laminas#7: Move compilation to translate methods
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Hamilton <[email protected]>
  • Loading branch information
TotalWipeOut committed May 16, 2024
1 parent 3f9c7c9 commit feac441
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ public function getPluginManager()
*/
public function translate($message, $textDomain = 'default', $locale = null)
{
$locale ??= $this->getLocale();
$locale ??= $this->getLocale();
$placeholders = [];
if (is_array($textDomain)) {
$placeholders = $textDomain;
$textDomain = $placeholders['_textDomain'] ?? 'default';
}

$translation = $this->getTranslatedMessage($message, $locale, $textDomain);

if ($translation !== null && $translation !== '') {
Expand All @@ -362,7 +368,7 @@ public function translate($message, $textDomain = 'default', $locale = null)
return $this->translate($message, $textDomain, $fallbackLocale);
}

return $this->compileMessage($message, is_array($textDomain) ? $textDomain : []);
return $this->compileMessage($message, $placeholders, $locale);
}

/**
Expand All @@ -383,7 +389,12 @@ public function translatePlural(
$textDomain = 'default',
$locale = null
) {
$locale = $locale ?? $this->getLocale();
$locale ??= $this->getLocale();
$placeholders = [];
if (is_array($textDomain)) {
$placeholders = $textDomain;
$textDomain = $placeholders['_textDomain'] ?? 'default';
}
$translation = $this->getTranslatedMessage($singular, $locale, $textDomain);

if (is_string($translation)) {
Expand All @@ -398,23 +409,27 @@ public function translatePlural(
}

if (isset($translation[$index]) && $translation[$index] !== '' && $translation[$index] !== null) {
return $translation[$index];
return $this->compileMessage($translation[$index], $placeholders, $locale);
}

if (
null !== ($fallbackLocale = $this->getFallbackLocale())
&& $locale !== $fallbackLocale
) {
return $this->translatePlural(
$singular,
$plural,
$number,
$textDomain,
$fallbackLocale
return $this->compileMessage(
$this->translatePlural(
$singular,
$plural,
$number,
$textDomain,
$fallbackLocale
),
$placeholders,
$locale
);
}

return $index === 0 ? $singular : $plural;
return $this->compileMessage($index === 0 ? $singular : $plural, $placeholders, $locale);
}

/**
Expand All @@ -424,7 +439,7 @@ public function translatePlural(
* @param string $message
* @param string $locale
* @param string|string[] $textDomain or placeholders
* @return string|null
* @return string|array|null
*/
protected function getTranslatedMessage(
$message,
Expand All @@ -435,22 +450,12 @@ protected function getTranslatedMessage(
return '';
}

$placeholders = [];
if (is_array($textDomain)) {
$placeholders = $textDomain;
$textDomain = $placeholders['_textDomain'] ?? 'default';
}

if (! isset($this->messages[$textDomain][$locale])) {
$this->loadMessages($textDomain, $locale);
}

if (isset($this->messages[$textDomain][$locale][$message])) {
return $this->compileMessage(
$this->messages[$textDomain][$locale][$message],
$placeholders,
$locale
);
return $this->messages[$textDomain][$locale][$message];
}

/**
Expand All @@ -467,11 +472,7 @@ protected function getTranslatedMessage(
* ]
*/
if (isset($this->messages[$textDomain][$locale][$textDomain . "\x04" . $message])) {
return $this->compileMessage(
$this->messages[$textDomain][$locale][$textDomain . "\x04" . $message],
$placeholders,
$locale
);
return $this->messages[$textDomain][$locale][$textDomain . "\x04" . $message];
}

if ($this->isEventManagerEnabled()) {
Expand Down Expand Up @@ -846,7 +847,10 @@ public function setPlaceholder(PlaceholderInterface $placeholder)
$this->placeholder = $placeholder;
}

protected function compileMessage(string $message, array $placeholders, string $locale): string
/**
* @param iterable<string|int, string> $placeholders
*/
protected function compileMessage(string $message, iterable $placeholders, string $locale): string
{
return $this->placeholder ?
$this->placeholder->compile(
Expand Down

0 comments on commit feac441

Please sign in to comment.