01: package net.xoetrope.optional.awt;
02:
03: import net.xoetrope.awt.XMetaContent;
04: import java.awt.Graphics;
05: import java.util.ResourceBundle;
06: import net.xoetrope.xui.XResourceManager;
07: import net.xoetrope.xui.XProjectManager;
08:
09: /**
10: * An extension of XMetaContent that handles translation of the content
11: * <p>Copyright (c) Xoetrope Ltd., 1998-2004</p>
12: * $Revision: 1.2 $
13: */
14: public class XLocalisedMetaContent extends XMetaContent {
15:
16: protected ResourceBundle languageResourceBundle;
17:
18: /**
19: * Create a new XLocalisedMetaContent and prepare it for translation
20: */
21: public XLocalisedMetaContent() {
22: languageResourceBundle = XResourceManager
23: .getResourceBundle(XProjectManager.getCurrentProject()
24: .getStartupParam("Language"));
25: }
26:
27: /**
28: * Render the text, first looking up the current translation of the text
29: * @param g the graphics context
30: * @param text the text to translate and display
31: */
32: protected void renderText(Graphics g, String text) {
33: int iStart = text.indexOf("${");
34: int iEnd = text.indexOf("}", iStart);
35: while (iStart > -1) {
36: String key = text.substring(iStart + 2, iEnd);
37: String temp = text.substring(0, iStart);
38: temp += translate(key);
39: temp += text.substring(iEnd + 1, text.length());
40: text = temp;
41: iStart = text.indexOf("${");
42: iEnd = text.indexOf("}", iStart);
43: }
44:
45: super .renderText(g, text);
46: }
47:
48: /**
49: * Translate the text being added to the list.
50: * @param key the key to be look up in the resourcebundle
51: * @return the translated key or the key itself if no translation found
52: */
53: private String translate(String key) {
54: String ret = key;
55:
56: try {
57: ret = languageResourceBundle.getString(key);
58: } catch (Exception ex) {
59: }
60:
61: return ret;
62: }
63: }
|