General Actions:
Log-in
Register
Wiki:
Main wiki
▼
:
Document Index
»
Space:
AppWithinMinutes
▼
:
Document Index
»
Page:
DynamicMessageTool
Search
default
Page Actions:
Export
▼
:
Export as PDF
Export as RTF
Export as HTML
More actions
▼
:
Print preview
View Source
Main wiki
»
App Within Minutes
»
LiveTableGenerator
»
DynamicMessageTool
Wiki source code of
DynamicMessageTool
Last modified by
Administrator
on 2012/03/21 17:41
Content
·
Comments
(0)
·
Annotations
(0)
·
Attachments
(0)
·
History
·
Information
Hide line numbers
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: */ 16: public class XWikiDynamicMessageTool extends XWikiMessageTool 17: { 18: private XWikiMessageTool msg; 19: 20: private Map<String, String> overwrites = new HashMap<String, String>(); 21: 22: public XWikiDynamicMessageTool(XWikiMessageTool msg) 23: { 24: super(msg.bundle, msg.context); 25: this.msg = msg; 26: } 27: 28: public List<XWikiDocument> getDocumentBundles() 29: { 30: return this.msg.getDocumentBundles(); 31: } 32: 33: protected String getTranslation(String key) 34: { 35: if (key == null) { 36: return null; 37: } 38: String value = this.overwrites.get(key); 39: return value != null ? value : this.msg.getTranslation(key); 40: } 41: 42: public String put(String key, String value) 43: { 44: return this.overwrites.put(key, value); 45: } 46: } 47: 48: public class XWikiDynamicMessageToolFactory implements ScriptService 49: { 50: public XWikiDynamicMessageTool createDynamicMessageTool(XWikiMessageTool msg, Map<?, ?> overwrites) 51: { 52: XWikiDynamicMessageTool dynamicMessageTool = new XWikiDynamicMessageTool(msg); 53: for(Map.Entry<?,?> entry : overwrites.entrySet()) { 54: dynamicMessageTool.put(entry.getKey(), entry.getValue()); 55: } 56: return dynamicMessageTool; 57: } 58: } 59: 60: if (!Utils.getComponentManager().hasComponent(ScriptService.class, 'dynamicMessageToolFactory')) { 61: Utils.getComponentManager().registerComponent(new DefaultComponentDescriptor(implementation: XWikiDynamicMessageToolFactory.class, role: ScriptService.class, roleHint: 'dynamicMessageToolFactory')); 62: } 63: {{/groovy}}