|
22 | 22 |
|
23 | 23 | namespace FormatPHP\Intl; |
24 | 24 |
|
25 | | -use FormatPHP\ConfigInterface; |
26 | | -use FormatPHP\DescriptorInterface; |
27 | 25 | use FormatPHP\Exception\InvalidArgumentException; |
28 | | -use FormatPHP\Exception\MessageNotFoundException; |
29 | | -use FormatPHP\Exception\UnableToGenerateMessageIdException; |
30 | | -use FormatPHP\Extractor\IdInterpolator; |
| 26 | +use FormatPHP\Exception\UnableToFormatMessageException; |
| 27 | +use IntlException as PhpIntlException; |
| 28 | +use Locale as PhpLocale; |
31 | 29 | use MessageFormatter as PhpMessageFormatter; |
32 | 30 |
|
33 | | -use function preg_replace; |
34 | 31 | use function sprintf; |
35 | | -use function trim; |
36 | 32 |
|
37 | 33 | /** |
38 | | - * Formats a message using {@link https://unicode-org.github.io/icu/userguide/format_parse/messages/ ICU Message syntax} |
| 34 | + * Formats an ICU message format pattern |
39 | 35 | */ |
40 | | -class MessageFormat |
| 36 | +class MessageFormat implements MessageFormatInterface |
41 | 37 | { |
42 | | - private ConfigInterface $config; |
43 | | - |
44 | | - public function __construct(ConfigInterface $config) |
45 | | - { |
46 | | - $this->config = $config; |
47 | | - } |
| 38 | + private LocaleInterface $locale; |
48 | 39 |
|
49 | 40 | /** |
50 | | - * Returns a translated string for the given descriptor ID |
51 | | - * |
52 | | - * If the descriptor does not have an ID, we will use a combination of the |
53 | | - * defaultMessage and description to create an ID. |
54 | | - * |
55 | | - * If we cannot find the given ID in the configured messages, we will use |
56 | | - * the descriptor's defaultMessage, if provided. |
57 | | - * |
58 | | - * @param array<array-key, int | float | string> $values |
59 | | - * |
60 | 41 | * @throws InvalidArgumentException |
61 | 42 | */ |
62 | | - public function format(DescriptorInterface $descriptor, array $values = []): string |
| 43 | + public function __construct(?LocaleInterface $locale = null) |
63 | 44 | { |
64 | | - return (string) PhpMessageFormatter::formatMessage( |
65 | | - (string) $this->config->getLocale()->baseName(), |
66 | | - $this->getMessage($descriptor), |
67 | | - $values, |
68 | | - ); |
| 45 | + $this->locale = $locale ?? new Locale(PhpLocale::getDefault()); |
69 | 46 | } |
70 | 47 |
|
71 | 48 | /** |
72 | | - * @throws InvalidArgumentException |
| 49 | + * @inheritdoc |
73 | 50 | */ |
74 | | - private function buildMessageId(DescriptorInterface $descriptor): string |
| 51 | + public function format(string $pattern, array $values = []): string |
75 | 52 | { |
76 | 53 | try { |
77 | | - $messageId = (new IdInterpolator())->generateId( |
78 | | - $descriptor, |
79 | | - $this->config->getIdInterpolatorPattern(), |
| 54 | + $formatter = new PhpMessageFormatter((string) $this->locale->baseName(), $pattern); |
| 55 | + |
| 56 | + return (string) $formatter->format($values); |
| 57 | + } catch (PhpIntlException $exception) { |
| 58 | + throw new UnableToFormatMessageException( |
| 59 | + sprintf( |
| 60 | + 'Unable to format message with pattern "%s" for locale "%s"', |
| 61 | + $pattern, |
| 62 | + (string) $this->locale->baseName(), |
| 63 | + ), |
| 64 | + (int) $exception->getCode(), |
| 65 | + $exception, |
80 | 66 | ); |
81 | | - } catch (UnableToGenerateMessageIdException $exception) { |
82 | | - $messageId = ''; |
83 | | - } |
84 | | - |
85 | | - return $messageId; |
86 | | - } |
87 | | - |
88 | | - /** |
89 | | - * @throws InvalidArgumentException |
90 | | - */ |
91 | | - private function getMessage(DescriptorInterface $descriptor): string |
92 | | - { |
93 | | - $messageId = $this->buildMessageId($descriptor); |
94 | | - |
95 | | - try { |
96 | | - return $this->lookupMessage($messageId); |
97 | | - } catch (MessageNotFoundException $exception) { |
98 | | - if ($descriptor->getDefaultMessage() !== null) { |
99 | | - return trim((string) preg_replace('/\n\s*/', ' ', (string) $descriptor->getDefaultMessage())); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - return $messageId; |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * @throws MessageNotFoundException |
108 | | - */ |
109 | | - private function lookupMessage(string $messageId): string |
110 | | - { |
111 | | - $config = $this->config; |
112 | | - |
113 | | - try { |
114 | | - return $config->getMessages()->getMessage($messageId, $config->getLocale()); |
115 | | - } catch (MessageNotFoundException $exception) { |
116 | | - try { |
117 | | - // Try falling back to a locale made up of just the language. |
118 | | - return $config->getMessages()->getMessage( |
119 | | - $messageId, |
120 | | - new Locale((string) $config->getLocale()->language()), |
121 | | - ); |
122 | | - } catch (MessageNotFoundException $exception) { |
123 | | - $defaultLocale = $config->getDefaultLocale(); |
124 | | - if ($defaultLocale !== null) { |
125 | | - return $config->getMessages()->getMessage($messageId, $defaultLocale); |
126 | | - } |
127 | | - } |
128 | 67 | } |
129 | | - |
130 | | - throw new MessageNotFoundException(sprintf('Unable to look up message with ID "%s".', $messageId)); |
131 | 68 | } |
132 | 69 | } |
0 commit comments