Wiki-Quellcode von Translator
Zuletzt geändert von admin am 2025/02/23 19:33
Zeige letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity output="false"}} | ||
| 2 | #macro (getTranslations) | ||
| 3 | #set ($keys = $request.getParameterValues('key')) | ||
| 4 | #set ($prefix = $request.prefix) | ||
| 5 | #if ("$!prefix" != '' && !$prefix.endsWith('.')) | ||
| 6 | #set ($prefix = $prefix + '.') | ||
| 7 | #end | ||
| 8 | #set ($translations = {}) | ||
| 9 | #foreach ($key in $keys) | ||
| 10 | #set ($translation = $services.localization.get("$!prefix$key")) | ||
| 11 | #if ($translation) | ||
| 12 | #set ($discard = $translations.put($key, $translation.rawSource)) | ||
| 13 | #else | ||
| 14 | #set ($discard = $translations.put($key, $key)) | ||
| 15 | #end | ||
| 16 | #end | ||
| 17 | #end | ||
| 18 | |||
| 19 | #macro (outputTranslations) | ||
| 20 | #getTranslations | ||
| 21 | #if ($xcontext.action == 'get') | ||
| 22 | #set ($discard = $response.setContentType('application/json')) | ||
| 23 | #set ($discard = $response.setHeader('Cache-Control', 'public')) | ||
| 24 | ## Cache for one month. | ||
| 25 | #set ($cacheDuration = 30 * 24 * 3600 * 1000) | ||
| 26 | #set ($discard = $response.setDateHeader('Expires', $mathtool.add($datetool.date.time, $cacheDuration))) | ||
| 27 | ## Even if the response is cached, most browsers are still sending a request if the user reloads the page using F5. | ||
| 28 | ## We send back the "Last-Modified" header in the response so that the browser will send us an "If-Modified-Since" | ||
| 29 | ## request for any subsequent calls for this resource. When this happens we return a 304 to tell the browser to use | ||
| 30 | ## its cached version. | ||
| 31 | #set ($discard = $response.setDateHeader('Last-Modified', $datetool.date.time)) | ||
| 32 | #end | ||
| 33 | $jsontool.serialize($translations) | ||
| 34 | #end | ||
| 35 | {{/velocity}} | ||
| 36 | |||
| 37 | {{velocity wiki="false"}} | ||
| 38 | #if ($request.getHeader('If-Modified-Since')) | ||
| 39 | ## Return 304 Not Modified. | ||
| 40 | #set ($discard = $response.setStatus(304)) | ||
| 41 | #else | ||
| 42 | #outputTranslations | ||
| 43 | #end | ||
| 44 | {{/velocity}} |