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 | 8 | */ |
|
30 | const CACHE_TIMESTAMP_KEY = 'js-localization-messages-last-modified'; |
||
31 | 8 | ||
32 | 8 | ||
33 | public function __construct() |
||
37 | |||
38 | /** |
||
39 | * Returns the messages (already JSON encoded), fresh if wanted. |
||
40 | 3 | * Creates the necessary cache item if necessary. |
|
41 | * |
||
42 | 3 | * @return string JSON encoded messages object. |
|
43 | */ |
||
44 | public function getMessagesJson($noCache = false) |
||
52 | 7 | ||
53 | /** |
||
54 | 7 | * Refreshes the cache item containing the JSON encoded |
|
55 | * messages object. |
||
56 | 7 | * Fires the 'JsLocalization.registerMessages' event. |
|
57 | 7 | * |
|
58 | 7 | * @return void |
|
59 | */ |
||
60 | 7 | public function refreshCache() |
|
67 | |||
68 | 7 | /** |
|
69 | * @return string |
||
70 | 7 | */ |
|
71 | protected function createMessagesJson() |
||
79 | |||
80 | 7 | /** |
|
81 | * Returns the locales we need to consider. |
||
82 | 7 | * |
|
83 | * @return array Locales. |
||
84 | 7 | */ |
|
85 | 7 | protected function getLocales() |
|
89 | 7 | ||
90 | 7 | /** |
|
91 | * Returns the translated messages for the given keys and locales. |
||
92 | 7 | * |
|
93 | * @param array $messageKeys |
||
94 | * @param array $locales |
||
95 | * @return array The translated messages as [<locale> => [ <message id> => <translation>, ... ], ...] |
||
96 | */ |
||
97 | protected function getTranslatedMessagesForLocales(array $messageKeys, array $locales) |
||
111 | 6 | ||
112 | 6 | /** |
|
113 | 6 | * Returns the translated messages for the given keys. |
|
114 | * |
||
115 | 7 | * @param array $messageKeys |
|
116 | * @param $locale |
||
117 | 7 | * @return array The translated messages as [ <message id> => <translation>, ... ] |
|
118 | */ |
||
119 | protected function getTranslatedMessages(array $messageKeys, $locale) |
||
136 | 6 | ||
137 | /** |
||
138 | 6 | * Transforms an array of nested translation messages into a "flat" (not nested) array. |
|
139 | * |
||
140 | 6 | * @param array $nestedMessages |
|
141 | * @param string $keyPrefix |
||
142 | * @return array Flattened translations array. |
||
143 | */ |
||
144 | protected function flattenTranslations(array $nestedMessages, $keyPrefix='') |
||
159 | |||
160 | /** |
||
161 | * Returns the message keys of all messages |
||
162 | 1 | * that are supposed to be sent to the browser. |
|
163 | * |
||
164 | * @return array Array of message keys. |
||
165 | */ |
||
166 | protected function getMessageKeys() |
||
177 | } |
||
178 |