Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Translation] Added intl message formatter. #27399

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

Merged
merged 11 commits into from
Sep 4, 2018
Prev Previous commit
Next Next commit
Add support for multiple formatters
  • Loading branch information
Nyholm committed Sep 3, 2018
commit b43fe21997e5ff1e18391915b3502ad5286e7682
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class IntlMessageFormatter implements MessageFormatterInterface
class IntlMessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
{
/**
* {@inheritdoc}
Expand All @@ -34,4 +34,12 @@ public function format($message, $locale, array $parameters = array())

return $message;
}

/**
* {@inheritdoc}
*/
public function choiceFormat($message, $number, $locale, array $parameters = array())
{
return $this->format($message, $locale, $parameters);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this kind of implementation right? Argument number is unused.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't implement the ChoiceMessageFormatterInterface instead, it will disallow using the transChoice method which is right since we should only use the trans method for intl-formated message.

}
}
37 changes: 32 additions & 5 deletions 37 src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
private $resources = array();

/**
* @var MessageFormatterInterface
* @var MessageFormatterInterface[]
*/
private $formatter;
private $formatters;

/**
* @var string
Expand Down Expand Up @@ -89,7 +89,7 @@ public function __construct(?string $locale, MessageFormatterInterface $formatte
$formatter = new MessageFormatter();
}

$this->formatter = $formatter;
$this->formatters['default'] = $formatter;
$this->cacheDir = $cacheDir;
$this->debug = $debug;
}
Expand Down Expand Up @@ -137,6 +137,15 @@ public function addResource($format, $resource, $locale, $domain = null)
}
}

/**
* @param string $domain
* @param MessageFormatterInterface $formatter
Copy link
Member

Choose a reason for hiding this comment

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

to be removed :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you. I added : void as return type.

*/
public function addFormatter(string $domain, MessageFormatterInterface $formatter)
{
$this->formatters[$domain] = $formatter;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -192,7 +201,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
$domain = 'messages';
}

return $this->formatter->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters);
return $this->getFormatter($domain)->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters);
}

/**
Expand All @@ -208,6 +217,11 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
$domain = 'messages';
}

$formatter = $this->getFormatter($domain);
if (!$formatter instanceof ChoiceMessageFormatterInterface) {
throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', get_class($formatter)));
}

$id = (string) $id;
$catalogue = $this->getCatalogue($locale);
$locale = $catalogue->getLocale();
Expand All @@ -220,7 +234,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
}
}

return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
return $formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
}

/**
Expand Down Expand Up @@ -455,4 +469,17 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface

return $this->configCacheFactory;
}

/**
* @param string $domain
* @return MessageFormatterInterface
Copy link
Member

Choose a reason for hiding this comment

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

to be removed (and a real return type be used instead)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you

*/
private function getFormatter(string $domain)
{
if (isset($this->formatters[$domain])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this could be reduced to:

return $this->formatters[$domain] ?? $this->formatters['_default'];

Copy link
Contributor

Choose a reason for hiding this comment

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

And we can declare return type also

return $this->formatters[$domain];
}

return $this->formatters['default'];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.