1 | <?php |
||
14 | class MessageCachingService extends AbstractCachingService |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * The key used to cache the JSON encoded messages. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | const CACHE_KEY = 'js-localization-messages-json'; |
||
23 | |||
24 | /** |
||
25 | * The key used to cache the timestamp of the last |
||
26 | * refreshCache() call. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | const CACHE_TIMESTAMP_KEY = 'js-localization-messages-last-modified'; |
||
31 | |||
32 | |||
33 | 8 | public function __construct() |
|
37 | |||
38 | /** |
||
39 | * Returns the messages (already JSON encoded). |
||
40 | * Refreshes the cache if necessary. |
||
41 | * |
||
42 | * @param bool $noCache (optional) Defines if cache should be ignored. |
||
43 | * @return string JSON encoded messages object. |
||
44 | */ |
||
45 | 3 | public function getMessagesJson($noCache = false) |
|
53 | |||
54 | /** |
||
55 | * Refreshes the cache item containing the JSON encoded |
||
56 | * messages object. |
||
57 | * Fires the 'JsLocalization.registerMessages' event. |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | 7 | public function refreshCache() |
|
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | 7 | protected function createMessagesJson() |
|
80 | |||
81 | /** |
||
82 | * Returns the locales we need to consider. |
||
83 | * |
||
84 | * @return array Locales. |
||
85 | */ |
||
86 | 7 | protected function getLocales() |
|
90 | |||
91 | /** |
||
92 | * Returns the translated messages for the given keys and locales. |
||
93 | * |
||
94 | * @param array $messageKeys |
||
95 | * @param array $locales |
||
96 | * @return array The translated messages as [<locale> => [ <message id> => <translation>, ... ], ...] |
||
97 | */ |
||
98 | 7 | protected function getTranslatedMessagesForLocales(array $messageKeys, array $locales) |
|
112 | |||
113 | /** |
||
114 | * Returns the translated messages for the given keys. |
||
115 | * |
||
116 | * @param array $messageKeys |
||
117 | * @param $locale |
||
118 | * @return array The translated messages as [ <message id> => <translation>, ... ] |
||
119 | */ |
||
120 | 7 | protected function getTranslatedMessages(array $messageKeys, $locale) |
|
137 | |||
138 | /** |
||
139 | * Transforms an array of nested translation messages into a "flat" (not nested) array. |
||
140 | * |
||
141 | * @param array $nestedMessages |
||
142 | * @param string $keyPrefix |
||
143 | * @return array Flattened translations array. |
||
144 | */ |
||
145 | 6 | protected function flattenTranslations(array $nestedMessages, $keyPrefix='') |
|
160 | |||
161 | /** |
||
162 | * Returns the message keys of all messages |
||
163 | * that are supposed to be sent to the browser. |
||
164 | * |
||
165 | * @return array Array of message keys. |
||
166 | */ |
||
167 | 7 | protected function getMessageKeys() |
|
178 | } |
||
179 |