-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
940d440
19e8e69
c2b3dc0
b43fe21
a325a44
2aa7181
597a15d
f88153f
b1aa004
fb30c77
2a90931
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,9 +54,9 @@ class Translator implements TranslatorInterface, TranslatorBagInterface | |
private $resources = array(); | ||
|
||
/** | ||
* @var MessageFormatterInterface | ||
* @var MessageFormatterInterface[] | ||
*/ | ||
private $formatter; | ||
private $formatters; | ||
|
||
/** | ||
* @var string | ||
|
@@ -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; | ||
} | ||
|
@@ -137,6 +137,15 @@ public function addResource($format, $resource, $locale, $domain = null) | |
} | ||
} | ||
|
||
/** | ||
* @param string $domain | ||
* @param MessageFormatterInterface $formatter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be removed :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I added |
||
*/ | ||
public function addFormatter(string $domain, MessageFormatterInterface $formatter) | ||
{ | ||
$this->formatters[$domain] = $formatter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -455,4 +469,17 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface | |
|
||
return $this->configCacheFactory; | ||
} | ||
|
||
/** | ||
* @param string $domain | ||
* @return MessageFormatterInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be removed (and a real return type be used instead) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you |
||
*/ | ||
private function getFormatter(string $domain) | ||
{ | ||
if (isset($this->formatters[$domain])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be reduced to: return $this->formatters[$domain] ?? $this->formatters['_default']; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']; | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 thetransChoice
method which is right since we should only use thetrans
method for intl-formated message.