Wiki-Quellcode von DynamicMessageTool
Zuletzt geändert von admin am 2025/02/23 10:22
Zeige letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{groovy}} | ||
| 2 | import com.xpn.xwiki.doc.XWikiDocument; | ||
| 3 | import com.xpn.xwiki.web.Utils; | ||
| 4 | import com.xpn.xwiki.web.XWikiMessageTool; | ||
| 5 | |||
| 6 | import java.util.HashMap; | ||
| 7 | import java.util.List; | ||
| 8 | import java.util.Map; | ||
| 9 | |||
| 10 | import org.xwiki.script.service.ScriptService; | ||
| 11 | import org.xwiki.component.descriptor.DefaultComponentDescriptor; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * Extends the default message tool with the ability to add/overwrite translation keys dynamically. | ||
| 15 | * @deprecated since 4.5M1 the AppWithinMinutes wizard generates a translation bundle for each application so there's | ||
| 16 | * no need to use this hack any more. We keep it just to not break existing applications. It's enough to | ||
| 17 | * edit and save an existing application to migrate it to the new translation engine. | ||
| 18 | */ | ||
| 19 | public class XWikiDynamicMessageTool extends XWikiMessageTool | ||
| 20 | { | ||
| 21 | private XWikiMessageTool msg; | ||
| 22 | |||
| 23 | private Map<String, String> overwrites = new HashMap<String, String>(); | ||
| 24 | |||
| 25 | public XWikiDynamicMessageTool(XWikiMessageTool msg) | ||
| 26 | { | ||
| 27 | super(msg.bundle, msg.context); | ||
| 28 | this.msg = msg; | ||
| 29 | } | ||
| 30 | |||
| 31 | // @Override | ||
| 32 | public List<XWikiDocument> getDocumentBundles() | ||
| 33 | { | ||
| 34 | return this.msg.getDocumentBundles(); | ||
| 35 | } | ||
| 36 | |||
| 37 | // @Override | ||
| 38 | public String get(String key) | ||
| 39 | { | ||
| 40 | String result = super.get(key); | ||
| 41 | return result == key ? this.msg.get(key) : result; | ||
| 42 | } | ||
| 43 | |||
| 44 | // @Override | ||
| 45 | public String get(String key, Object... params) | ||
| 46 | { | ||
| 47 | String result = super.get(key, params); | ||
| 48 | return result == key ? this.msg.get(key, params) : result; | ||
| 49 | } | ||
| 50 | |||
| 51 | // @Override | ||
| 52 | protected String getTranslation(String key) | ||
| 53 | { | ||
| 54 | return this.overwrites.get(key); | ||
| 55 | } | ||
| 56 | |||
| 57 | public String put(String key, String value) | ||
| 58 | { | ||
| 59 | return this.overwrites.put(key, value); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | public class XWikiDynamicMessageToolFactory implements ScriptService | ||
| 64 | { | ||
| 65 | public XWikiDynamicMessageTool createDynamicMessageTool(XWikiMessageTool msg, Map<?, ?> overwrites) | ||
| 66 | { | ||
| 67 | XWikiDynamicMessageTool dynamicMessageTool = new XWikiDynamicMessageTool(msg); | ||
| 68 | for(Map.Entry<?,?> entry : overwrites.entrySet()) { | ||
| 69 | dynamicMessageTool.put(entry.getKey(), entry.getValue()); | ||
| 70 | } | ||
| 71 | return dynamicMessageTool; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | if (!services.component.componentManager.hasComponent(ScriptService.class, 'dynamicMessageToolFactory')) { | ||
| 76 | def descriptor = new DefaultComponentDescriptor(implementation: XWikiDynamicMessageToolFactory.class, role: ScriptService.class, roleHint: 'dynamicMessageToolFactory'); | ||
| 77 | services.component.getComponentManager("wiki:${xcontext.database}").registerComponent(descriptor); | ||
| 78 | } | ||
| 79 | {{/groovy}} |