Author: fred
Date: 2007-07-22 20:00:08 +0000 (Sun, 22 Jul 2007)
New Revision: 14261
Modified:
trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java
Log:
Exceptions & Logging
Modified: trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java 2007-07-22 19:57:01 UTC
(rev 14260)
+++ trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java 2007-07-22 20:00:08 UTC
(rev 14261)
@@ -1,10 +1,11 @@
package plugins.echo.i18n;
+import freenet.support.Logger;
+
import java.util.Properties;
-import java.io.FileInputStream; // DEBUG
+import java.util.MissingResourceException;
import java.io.IOException;
-import java.io.FileNotFoundException;
import nu.xom.Nodes;
import nu.xom.Element;
@@ -25,28 +26,43 @@
private Properties translation;
private Properties defaultTranslation = null;
- public I18n(String language) throws FileNotFoundException, IOException {
+ public I18n(String language) throws MissingResourceException {
translation = new Properties();
setLanguage(language);
-
+
}
/**
* Set the language
* @param language the ISO code of the language
*/
- public void setLanguage(String language) throws FileNotFoundException,
IOException {
+ public void setLanguage(String language) throws
MissingResourceException {
- translation.load(getClass().getResourceAsStream("/i18n/" +
PREFIX + language + SUFFIX));
+ Logger.normal("I18n", "Changing the current language to
: " + language);
+ loadTranslation(language, translation);
+
+ if(! language.equals(DEFAULT_LANGUAGE))
+ loadTranslation(DEFAULT_LANGUAGE,
defaultTranslation);
+
+ selectedLanguage = language;
+ }
+
+ /**
+ * Load a translation
+ * @param language the ISO code of the language
+ * @param props the properties to load into
+ */
+ private void loadTranslation(String language, Properties props) throws
MissingResourceException {
- if(! language.equals(DEFAULT_LANGUAGE)) {
- if(defaultTranslation == null)
- defaultTranslation = new Properties();
-
defaultTranslation.load(getClass().getResourceAsStream("/i18n/" + PREFIX +
DEFAULT_LANGUAGE + SUFFIX));
+ if(props == null)
+ props = new Properties();
+ try {
+ props.load(getClass().getResourceAsStream("/i18n/" +
PREFIX + language + SUFFIX));
+ } catch (IOException ioe) {
+ Logger.error(this, "IOException while accessing the " +
language +"file" + ioe.getMessage(), ioe);
+ throw new MissingResourceException("Unable to load the
translation file for " + language, "i18n", language);
}
- selectedLanguage = language;
-
}
/**
@@ -88,6 +104,4 @@
i18nNodes.get(i).getParent().replaceChild(i18nNodes.get(i), new
Text(translatedKey));
}
}
-
-
}
\ No newline at end of file