001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump;
033:
034: import java.text.MessageFormat;
035: import java.util.Hashtable;
036: import java.util.ResourceBundle;
037: import java.util.Locale;
038:
039: import com.vividsolutions.jump.workbench.JUMPWorkbench;
040:
041: import org.apache.log4j.Logger;
042:
043: /**
044: * Singleton for the Internationalization (I18N)
045: * <pre>
046: * [1] HOWTO TRANSLATE JUMP IN MY OWN LANGUAGE
047: * Copy theses files and add the locales extension for your language and country instead of the *.
048: * - resources/jump_*.properties
049: * - com/vividsolutions/jump/workbench/ui/plugin/KeyboardPlugIn_*.html
050: *
051: * [2] HOWTO TRANSLATE MY PLUGIN AND GIVE THE ABILITY TO TRANSLATE IT
052: * Use theses methods to use your own *.properties files :
053: * [Michael Michaud 2007-03-23] the 3 following methods have been deactivated
054: * com.vividsolutions.jump.I18N#setPlugInRessource(String, String)
055: * com.vividsolutions.jump.I18N#get(String, String)
056: * com.vividsolutions.jump.I18N#getMessage(String, String, Object[])
057: * you still can use plugInsResourceBundle (as in Pirol's plugin)
058: *
059: * And use jump standard menus
060: * </pre>
061: *
062: * Code example : [Michael Michaud 2007-03-23 : the following code example
063: * is no more valid and has to be changed]
064: *
065: * <pre>
066: * public class PrintPlugIn extends AbstractPlugIn
067: * {
068: * private String name = "print";
069: *
070: * public void initialize(PlugInContext context) throws Exception
071: * {
072: * I18N.setPlugInRessource(name, "org.agil.core.jump.plugin.print");
073: * context.getFeatureInstaller().addMainMenuItem(this,
074: * new String[]
075: * {MenuNames.TOOLS,I18N.get(name, "print")},
076: * I18N.get(name, "print"), false, null, null);
077: * }
078: * ...
079: * </pre>
080: *
081: * <pre>
082: * TODO :I18N (1) Improve translations
083: * TODO :I18N (2) Separate config (customization) and I18N
084: * TODO :I18N (3) Explore and discuss about I18N integration and Jakarta Common Ressources
085: * (using it as a ressource interface)
086: * </pre>
087: *
088: * @author Basile Chandesris - <chandesris@pt-consulting.lu>
089: *
090: * @see com.vividsolutions.jump.workbench.ui.MenuNames
091: * @see com.vividsolutions.jump.workbench.ui.VTextIcon text rotation
092: */
093: public final class I18N {
094:
095: private static final Logger LOG = Logger.getLogger(I18N.class);
096:
097: // [Michael Michaud 2007-03-23] removed SingletonHolder internal class
098: // 1 - getInstance is enough to guarantee I18N instance unicity
099: // 2 - I18N should not be instanciated as the class has only static methods
100: private static final I18N instance = new I18N();
101:
102: // use 'jump<locale>.properties' i18n mapping file
103: // STanner changed the place where are stored bundles. Now are in /language
104: // public static ResourceBundle rb = ResourceBundle.getBundle("com.vividsolutions.jump.jump");
105: public static ResourceBundle rb = ResourceBundle
106: .getBundle("language/jump");
107:
108: // [Michael Michaud 2007-03-23] plugInsResourceBundle is deactivated because all the methods
109: // using it have been deactivated.
110: // [sstein] activated again since Pirol does use it
111: public static Hashtable plugInsResourceBundle = new Hashtable();
112:
113: private I18N() {
114: super ();
115: }
116:
117: public static I18N getInstance() {
118: //[Michael Michaud 2007-03-04] guarantee I18N instance unicity without
119: // creating a SingletonHolder inner class instance
120: return (instance == null) ? new I18N() : instance;
121: // return SingletonHolder._singleton;
122: }
123:
124: /**
125: * Load file specified in command line (-i18n lang_country)
126: * (lang_country :language 2 letters + "_" + country 2 letters)
127: * Tries first to extract lang and country, and if only lang
128: * is specified, loads the corresponding resource bundle.
129: * @param langcountry
130: */
131: public static void loadFile(final String langcountry) {
132: // [Michael Michaud 2007-03-04] handle the case where lang is the only
133: // variable instead of catching an ArrayIndexOutOfBoundsException
134: String[] lc = langcountry.split("_");
135: Locale locale = Locale.getDefault();
136: if (lc.length > 1) {
137: LOG.debug("lang:" + lc[0] + " " + "country:" + lc[1]);
138: locale = new Locale(lc[0], lc[1]);
139: } else if (lc.length > 0) {
140: LOG.debug("lang:" + lc[0]);
141: locale = new Locale(lc[0]);
142: } else {
143: LOG
144: .debug(langcountry
145: + " is an illegal argument to define lang [and country]");
146: }
147: rb = ResourceBundle.getBundle("language/jump", locale);
148: }
149:
150: /**
151: * Process text with the locale 'jump_<locale>.properties' file
152: * @param label
153: * @return i18n label
154: * [Michael Michaud 2007-03-23] If no resourcebundle is found, returns a default string
155: * which is the last part of the label
156: */
157: public static String get(final String label) {
158: try {
159: return rb.getString(label);
160: } catch (java.util.MissingResourceException e) {
161: String[] labelpath = label.split("\\.");
162: LOG
163: .debug("No resource bundle or no translation found for the key : "
164: + label);
165: return labelpath[labelpath.length - 1];
166: }
167: }
168:
169: /**
170: * Get the short signature for locale
171: * (letters extension :language 2 letters + "_" + country 2 letters)
172: * @return string signature for locale
173: */
174: public static String getLocale() {
175: return rb.getLocale().getLanguage() + "_"
176: + rb.getLocale().getCountry();
177: }
178:
179: /**
180: * Get the short signature for language
181: * (letters extension :language 2 letters)
182: * @return string signature for language
183: */
184: public static String getLanguage() {
185: if (JUMPWorkbench.I18N_SETLOCALE == "") {
186: // No locale has been specified at startup: choose default locale
187: return rb.getLocale().getLanguage();
188: } else {
189: return JUMPWorkbench.I18N_SETLOCALE.split("_")[0];
190: }
191: }
192:
193: /**
194: * Process text with the locale 'jump_<locale>.properties' file
195: * If no resourcebundle is found, returns default string contained
196: * inside com.vividsolutions.jump.jump
197: * @param label with argument insertion : {0}
198: * @param objects
199: * @return i18n label
200: */
201: public static String getMessage(final String label,
202: final Object[] objects) {
203: try {
204: final MessageFormat mformat = new MessageFormat(rb
205: .getString(label));
206: return mformat.format(objects);
207: } catch (java.util.MissingResourceException e) {
208: final String[] labelpath = label.split("\\.");
209: LOG.warn(e.getMessage()
210: + " no default value, the resource key is used: "
211: + labelpath[labelpath.length - 1]);
212: final MessageFormat mformat = new MessageFormat(
213: labelpath[labelpath.length - 1]);
214: return mformat.format(objects);
215: }
216: }
217:
218: /**
219: * Process text with the locale 'pluginName_<locale>.properties' file
220: *
221: * @param pluginName (path + name)
222: * @param label
223: * @return i18n label
224: */
225: /*
226: public static String get(String pluginName, String label)
227: {
228: if (LOG.isDebugEnabled()){
229: LOG.debug(I18N.plugInsResourceBundle.get(pluginName)+" "+label
230: + ((ResourceBundle)I18N.plugInsResourceBundle
231: .get(pluginName))
232: .getString(label));
233: }
234: return ((ResourceBundle)I18N.plugInsResourceBundle
235: .get(pluginName))
236: .getString(label);
237: }
238: */
239:
240: /**
241: * Process text with the locale 'pluginName_<locale>.properties' file
242: *
243: * @param pluginName (path + name)
244: * @param label with argument insertion : {0}
245: * @param objects
246: * @return i18n label
247: */
248: /*
249: public static String getMessage(String pluginName, String label, Object[] objects)
250: {
251: MessageFormat mf = new MessageFormat(((ResourceBundle)I18N.plugInsResourceBundle
252: .get(pluginName))
253: .getString(label));
254: return mf.format(objects);
255: }
256: */
257:
258: }
|