Index: lutinutil/src/java/org/codelutin/i18n/Language.java diff -u lutinutil/src/java/org/codelutin/i18n/Language.java:1.12 lutinutil/src/java/org/codelutin/i18n/Language.java:1.13 --- lutinutil/src/java/org/codelutin/i18n/Language.java:1.12 Sat Mar 22 23:26:31 2008 +++ lutinutil/src/java/org/codelutin/i18n/Language.java Sun Mar 23 06:02:24 2008 @@ -32,18 +32,17 @@ import java.io.FileOutputStream; import java.io.IOException; import java.net.URLClassLoader; -import java.net.URL; import java.util.Enumeration; import java.util.Locale; import java.util.MissingResourceException; import java.util.Properties; /** - * This class is used byte the i18n class. It encapsulates the translation - * resource for one language. + * This class is used by the i18n class. It encapsulates the translation + * resource for a given language and a given encoding. *
* The class offers a public static factory method : - * {@link #newLanguage(Locale, String,URL[])}. + * {@link #newLanguage(Locale, String)}. * * Note : You SHOULD always use the factory method, since constructor does * not load i18n bundle resources. @@ -62,12 +61,11 @@ * * @param l la locale choisie * @param toEncoding l'encoding choisi - * @param extraUrl extra urls where to detect bundles * @return l'instance du language correspondant, initialisé */ - public static Language newLanguage(Locale l, String toEncoding, URL... extraUrl) { + public static Language newLanguage(Locale l, String toEncoding) { Language language = new Language(l, toEncoding); - language.init(extraUrl); + language.init(); return language; } @@ -93,24 +91,16 @@ this.encoding = toEncoding; } - protected Language(Locale l) { - this(l, I18n.UTF_8_ENCONDING); - } - /** * charge les traductions de la langue. * * recherche dans un premier temps, les urls des bundles puis les charge. - * - * @param extraUrl extra urls where to detect bundles */ - protected void init(URL... extraUrl) { + protected void init() { // make sure the bundlemanager is init - I18nBundleManager.init(extraUrl); - + I18nBundleManager.init(); // load resources from bundles resource = new Properties(); - // get bundles for this locale I18nBundleManager.getInstance().load(this, resource); } @@ -172,8 +162,9 @@ * @return untranslated sentence */ public String untranslate(String sentence) { - if (resource == null) + if (resource == null) { return sentence; + } try { Enumeration e = resource.propertyNames(); // Look for the given sentence through all translations @@ -181,8 +172,9 @@ String key = (String) e.nextElement(); String translation = resource.getProperty(key); // If found returns the corresponding key - if (sentence.equals(translation)) + if (sentence.equals(translation)) { return key; + } } } catch (MissingResourceException eee) { // Well, this can't happen... @@ -203,6 +195,20 @@ return resource == null ? 0 : resource.size(); } + public void close() { + if (resource != null) { + log.info(this); + resource.clear(); + resource = null; + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + close(); + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -222,6 +228,6 @@ @Override public String toString() { - return "Languageinit(...) methods
+ */
+ protected static URL[] extraURL;
+
+ /** Initialise la librairie avec encoing par defaut et locale par defaut */
+ public static void init() {
+ init(null, null, DEFAULT_ENCODING);
}
- //===========================================================
- //== With default encoding init
- //===========================================================
-
/**
* Initialize the library for given localeISO-8859-1 enconding
*
- * @param locale language to use
- * @param extraUrl some extra urls where to find bundles
+ * @param locale language to use
*/
- public static void init(Locale locale, URL... extraUrl) {
- init(locale, DEFAULT_ENCODING, extraUrl);
+ public static void init(Locale locale) {
+ init(locale, DEFAULT_ENCODING);
}
/**
@@ -117,133 +105,30 @@
*
* @param language une chaine representant la langue à utiliser fr, en, ...
* @param country une chaine representant le pays à utiliser FR, GB, ...
- * @param extraUrl some extra urls where to find bundles
*/
- public static void init(String language, String country, URL... extraUrl) {
- init(language, country, DEFAULT_ENCODING, extraUrl);
+ public static void init(String language, String country) {
+ init(language, country, DEFAULT_ENCODING);
}
/**
* Initialise la librairie.
*
* @param language une chaine representant la langue à utiliser fr, en, ...
- * @param extraUrl some extra urls where to find bundles
* @deprecated
*/
- public static void init(String language, URL... extraUrl) {
- init(language, null, DEFAULT_ENCODING, extraUrl);
+ public static void init(String language) {
+ init(language, null, DEFAULT_ENCODING);
}
-// //===========================================================
-// //== With ISO-8859-1 encoding init
-// //===========================================================
-//
-// /**
-// * Initialize the library with default locale {@link #DEFAULT_LOCALE}
-// * with ISO-8859-1 enconding
-// *
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initISO88591(URL... extraUrl) {
-// init((Locale) null, ISO_8859_1_ENCONDING, extraUrl);
-// }
-//
-// /**
-// * Initialize the library for given localeISO-8859-1 enconding
-// *
-// * @param locale language to use
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initISO88591(Locale locale, URL... extraUrl) {
-// init(locale, ISO_8859_1_ENCONDING, extraUrl);
-// }
-//
-// /**
-// * Initialize the library for given language and given
-// * country with ISO-8859-1 enconding
-// *
-// * @param language language to use
-// * @param country country to use
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initISO88591(String language, String country, URL... extraUrl) {
-// init(language, country, ISO_8859_1_ENCONDING, extraUrl);
-// }
-//
-// /**
-// * Initialize the library for given language with
-// * ISO-8859-1 enconding
-// *
-// * @param language language to use
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initISO88591(String language, URL... extraUrl) {
-// init(language,null, ISO_8859_1_ENCONDING, extraUrl);
-// }
-//
-// //===========================================================
-// //== With UTF-8 encoding init
-// //===========================================================
-//
-// /**
-// * Initialize the library with default locale {@link #DEFAULT_LOCALE}
-// * with UTF-8 enconding
-// *
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initUTF8(URL... extraUrl) {
-// init((String) null, UTF_8_ENCONDING, extraUrl);
-// }
-//
-// /**
-// * Initialize the library for given localeUTF-8 enconding
-// *
-// * @param locale language to use
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initUTF8(Locale locale, URL... extraUrl) {
-// init(locale, UTF_8_ENCONDING, extraUrl);
-// }
-//
-// /**
-// * Initialize the library for given language and
-// * given country with UTF-81 enconding
-// *
-// * @param language language to use
-// * @param country country to use
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initUTF8(String language, String country, URL... extraUrl) {
-// init(language, country, UTF_8_ENCONDING, extraUrl);
-// }
-//
-// /**
-// * Initialize the library for given language with
-// * UTF-8 enconding
-// *
-// * @param language language to use
-// * @param extraUrl some extra urls where to find bundles
-// */
-// public static void initUTF8(String language, URL... extraUrl) {
-// init(language, null, UTF_8_ENCONDING, extraUrl);
-// }
-
- //===========================================================
- //== With given encoding init
- //===========================================================
-
/**
* Initialise la librairie
*
* @param language une chaine representant la langue à utiliser fr, en, ...
* @param country une chaine representant le pays à utiliser FR, GB, ...
* @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise
- * @param extraUrl some extra urls where to find bundles
*/
- public static void init(String language, String country, String encoding, URL... extraUrl) {
- getLoader(encoding).init(language, country, extraUrl);
+ public static void init(String language, String country, String encoding) {
+ getLoader(encoding).init(language, country);
}
/**
@@ -251,13 +136,11 @@
*
* @param newLocale la locale a charger
* @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise
- * @param extraUrl some extra urls where to find bundles
*/
- public static void init(Locale newLocale, String encoding, URL... extraUrl) {
- getLoader(encoding).init(newLocale, extraUrl);
+ public static void init(Locale newLocale, String encoding) {
+ getLoader(encoding).init(newLocale);
}
-
/**
* Retourne la chaine traduite si possible.
*
@@ -266,12 +149,13 @@
* sinon.
*/
public static String _(String message) {
- if (loader.getLanguage() == null) {
+ if (loader == null || loader.getLanguage() == null) {
return applyFilter(message);
}
return applyFilter(loader.getLanguage().translate(message));
}
+
/**
* Retourne la chaine traduite si possible.
*
@@ -282,7 +166,7 @@
*/
public static String _(String message, Object... args) {
String result = message;
- Language language = loader.getLanguage();
+ Language language = loader == null ? null : loader.getLanguage();
if (language != null) {
result = language.translate(message);
}
@@ -327,6 +211,45 @@
}
}
+ public static String getRecordFilePath() {
+ return recordFilePath;
+ }
+
+ /**
+ * Change le filtre des chaines traduites
+ *
+ * @param filter l'objet filtre a utiliser
+ */
+ public static void setFilter(I18nFilter filter) {
+ I18n.filter = filter;
+ }
+
+ public static void setRecordFilePath(String recordFilePath) {
+ I18n.recordFilePath = recordFilePath;
+ }
+
+ /**
+ * Change extra urls to use in bundle discovering
+ *
+ * Note: This method will close the i18n system and reset cache of url in {@link I18nBundleManager}
+ *
+ * @param extraURL new extra urls to use
+ */
+ public static void setExtraURL(URL[] extraURL) {
+ I18n.extraURL = extraURL;
+ // must reset bundlemanager urls
+ I18nBundleManager.resetURL();
+ // and close system
+ close();
+ }
+
+ public static void close() {
+ if (loader != null) {
+ loader.close();
+ loader = null;
+ }
+ }
+
/**
* Applique le filtre s'il y en a un
*
@@ -345,34 +268,25 @@
return filter;
}
- public static String getRecordFilePath() {
- return recordFilePath;
+ public static URL[] getExtraURL() {
+ return extraURL == null ? new URL[0] : extraURL;
}
- /*protected static Language getLanguage() {
- return LanguageManager.getLanguage();
- }*/
-
- /*protected static void setLanguage(Locale newLocale, String toEncoding, URL... extraUrl) {
- // delegate to LanguageManager
- LanguageManager.setLanguage(newLocale, toEncoding, extraUrl);
- }*/
-
- /**
- * Change le filtre des chaines traduites
- *
- * @param filter l'objet filtre a utiliser
- */
- public static void setFilter(I18nFilter filter) {
- I18n.filter = filter;
- }
-
- public static void setRecordFilePath(String recordFilePath) {
- I18n.recordFilePath = recordFilePath;
+ protected static synchronized I18nLoader getLoader(String encoding) {
+ if (encoding == null) {
+ encoding = DEFAULT_ENCODING;
+ }
+ if (loader == null) {
+ loader = new I18nLoader(encoding, DEFAULT_LOCALE);
+ } else {
+ if (!loader.getEncoding().equals(encoding)) {
+ // close previous loader
+ loader.close();
+ // open a new loader
+ loader = new I18nLoader(encoding, DEFAULT_LOCALE);
+ }
+ }
+ return loader;
}
- /*protected static Locale newLocale(String str) {
- return ConverterUtil.convert(Locale.class, str);
- } */
-
} //I18n
\ No newline at end of file
Index: lutinutil/src/java/org/codelutin/i18n/I18nBundleBridge.java
diff -u lutinutil/src/java/org/codelutin/i18n/I18nBundleBridge.java:1.1 lutinutil/src/java/org/codelutin/i18n/I18nBundleBridge.java:1.2
--- lutinutil/src/java/org/codelutin/i18n/I18nBundleBridge.java:1.1 Wed Oct 4 14:51:55 2006
+++ lutinutil/src/java/org/codelutin/i18n/I18nBundleBridge.java Sun Mar 23 06:02:24 2008
@@ -24,22 +24,18 @@
* Created: 6 sept. 06
*
* @author Arnaud Thimel