Author: nextgens
Date: 2008-08-19 19:11:49 +0000 (Tue, 19 Aug 2008)
New Revision: 22032
Modified:
trunk/freenet/src/freenet/clients/http/PageMaker.java
trunk/freenet/src/freenet/clients/http/TranslationToadlet.java
trunk/freenet/src/freenet/l10n/L10n.java
trunk/freenet/src/freenet/node/Node.java
trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java
Log:
Use an enum in the L10n framework; needs testing.
Modified: trunk/freenet/src/freenet/clients/http/PageMaker.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/PageMaker.java 2008-08-19
18:50:57 UTC (rev 22031)
+++ trunk/freenet/src/freenet/clients/http/PageMaker.java 2008-08-19
19:11:49 UTC (rev 22032)
@@ -94,7 +94,7 @@
public HTMLNode getPageNode(String title, boolean
renderNavigationLinks, ToadletContext ctx) {
boolean fullAccess = ctx == null ? false :
ctx.isAllowedFullAccess();
HTMLNode pageNode = new HTMLNode.HTMLDoctype("html",
"-//W3C//DTD XHTML 1.1//EN");
- HTMLNode htmlNode = pageNode.addChild("html", "xml:lang",
L10n.getSelectedLanguage());
+ HTMLNode htmlNode = pageNode.addChild("html", "xml:lang",
L10n.getSelectedLanguage().getISOCode());
HTMLNode headNode = htmlNode.addChild("head");
headNode.addChild("meta", new String[] { "http-equiv",
"content" }, new String[] { "Content-Type", "text/html; charset=utf-8" });
headNode.addChild("title", title + " - Freenet");
Modified: trunk/freenet/src/freenet/clients/http/TranslationToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/TranslationToadlet.java
2008-08-19 18:50:57 UTC (rev 22031)
+++ trunk/freenet/src/freenet/clients/http/TranslationToadlet.java
2008-08-19 19:11:49 UTC (rev 22032)
@@ -154,7 +154,7 @@
HTMLNode translationNode = contentNode.addChild("div", "class",
"translation");
HTMLNode translationHeaderNode = translationNode.addChild("p");
- translationHeaderNode.addChild("#",
l10n("contributingToLabelWithLang", "lang", L10n.getSelectedLanguage()));
+ translationHeaderNode.addChild("#",
l10n("contributingToLabelWithLang", "lang",
L10n.getSelectedLanguage().getFullName()));
translationHeaderNode.addChild("a", "href",
TOADLET_URL+"?getOverrideTranlationFile").addChild("#",
l10n("downloadTranslationsFile"));
translationHeaderNode.addChild("#", " ");
if(showEverything)
Modified: trunk/freenet/src/freenet/l10n/L10n.java
===================================================================
--- trunk/freenet/src/freenet/l10n/L10n.java 2008-08-19 18:50:57 UTC (rev
22031)
+++ trunk/freenet/src/freenet/l10n/L10n.java 2008-08-19 19:11:49 UTC (rev
22032)
@@ -31,25 +31,83 @@
public static final String SUFFIX = ".properties";
public static final String OVERRIDE_SUFFIX = ".override" + SUFFIX;
- public static final String FALLBACK_DEFAULT = "en";
+ public static final LANGUAGE FALLBACK_DEFAULT = LANGUAGE.ENGLISH;
+
/** @see http://www.omniglot.com/language/names.htm */
- public static final String[][] AVAILABLE_LANGUAGES = {
- new String[] { "en", "English", "eng" },
- new String[] { "es", "Espa?ol", "spa" },
- new String[] { "da", "Dansk", "dan" },
- new String[] { "de", "Deutsch", "deu" },
- new String[] { "fi", "Suomi", "fin" },
- new String[] { "fr", "Fran?ais", "fra" },
- new String[] { "it", "Italiano", "ita" },
- new String[] { "no", "Norsk", "nor" },
- new String[] { "pl", "Polski", "pol" },
- new String[] { "se", "Svenska", "svk" },
- new String[] { "zh-cn", "??(??)", "chn" },
- new String[] { "zh-tw", "??(??)", "zh-tw" },
- new String[] { "unlisted", "unlisted", "unlisted"},
- };
- private final String selectedLanguage;
+ public enum LANGUAGE {
+ ENGLISH("en", "English", "eng"),
+ SPANISH("es", "Espa?ol", "spa"),
+ DANISH("da", "Dansk", "dan"),
+ GERMAN("de", "Deutsch", "deu"),
+ FINNISH("fi", "Suomi", "fin"),
+ FRENCH("fr", "Fran?ais", "fra"),
+ ITALIAN("it", "Italiano", "ita"),
+ NORWEGIAN("no", "Norsk", "nor"),
+ POLISH("pl", "Polski", "pol"),
+ SWEDISH("se", "Svenska", "svk"),
+ CHINESE("zh-cn", "??(??)", "chn"),
+ CHINESE_TAIWAN("zh-tw", "??(??)", "zh-tw"),
+ UNLISTED("unlisted", "unlisted", "unlisted");
+
+ private final String shortCode;
+ private final String fullName;
+ private final String isoCode;
+
+ LANGUAGE(String shortCode, String fullName, String isoCode) {
+ this.shortCode = shortCode;
+ this.fullName = fullName;
+ this.isoCode = isoCode;
+ }
+
+ LANGUAGE(LANGUAGE l) {
+ this(l.getShortCode(), l.getFullName(), l.getISOCode());
+ }
+
+ public static LANGUAGE mapToLanguage(String whatever) throws
MissingResourceException {
+ for(LANGUAGE currentLanguage : LANGUAGE.values()) {
+
if(currentLanguage.getShortCode().equalsIgnoreCase(whatever) ||
+
currentLanguage.getShortCode().equalsIgnoreCase(whatever) ||
+
currentLanguage.getISOCode().equalsIgnoreCase(whatever) ||
+
currentLanguage.toString().equalsIgnoreCase(whatever))
+ {
+ return currentLanguage;
+ }
+ }
+ return null;
+ }
+
+ public String getFullName() {
+ return fullName;
+ }
+
+ public String getShortCode() {
+ return shortCode;
+ }
+
+ public String getISOCode() {
+ return isoCode;
+ }
+
+ protected String getL10nFilename() {
+ return PREFIX.replace ('.',
'/').concat(PREFIX.concat(shortCode.concat(SUFFIX)));
+ }
+
+ protected String getL10nOverrideFilename() {
+ return L10n.PREFIX + shortCode + L10n.OVERRIDE_SUFFIX;
+ }
+
+ public static String[] valuesWithFullNames() {
+ LANGUAGE[] allValues = values();
+ String[] result = new String[allValues.length];
+ for(int i=0; i<allValues.length; i++)
+ result[i] = allValues[i].getFullName();
+
+ return result;
+ }
+ }
+ private final LANGUAGE selectedLanguage;
+
private static SimpleFieldSet currentTranslation = null;
private static SimpleFieldSet fallbackTranslation = null;
private static L10n currentClass = null;
@@ -57,11 +115,10 @@
private static SimpleFieldSet translationOverride;
private static final Object sync = new Object();
- L10n(String selected) {
- selectedLanguage = mapLanguageNameToShortCode(selected);
- File tmpFile = new File(L10n.PREFIX + selectedLanguage +
L10n.OVERRIDE_SUFFIX);
-
+ L10n(LANGUAGE selected) {
+ selectedLanguage = selected;
try {
+ File tmpFile = new
File(selected.getL10nOverrideFilename());
if(tmpFile.exists() && tmpFile.canRead() &&
tmpFile.length() > 0) {
Logger.normal(this, "Override file detected :
let's try to load it");
translationOverride =
SimpleFieldSet.readFrom(tmpFile, false, false);
@@ -94,15 +151,17 @@
* @throws MissingResourceException
*/
public static void setLanguage(String selectedLanguage) throws
MissingResourceException {
- selectedLanguage = mapLanguageNameToLongName(selectedLanguage);
synchronized (sync) {
Logger.normal(CLASS_NAME, "Changing the current
language to : " + selectedLanguage);
- currentClass = new L10n(selectedLanguage);
+ L10n oldClass = currentClass;
+ LANGUAGE lang =
LANGUAGE.mapToLanguage(selectedLanguage);
if(currentClass == null) {
- currentClass = new L10n(FALLBACK_DEFAULT);
+ currentClass = (oldClass != null ? oldClass :
new L10n(FALLBACK_DEFAULT));
Logger.error(CLASS_NAME, "The requested
translation is not available!" + selectedLanguage);
throw new MissingResourceException("The
requested translation (" + selectedLanguage + ") hasn't been found!",
CLASS_NAME, selectedLanguage);
- }
+ } else
+ currentClass = new L10n(lang);
+
}
}
@@ -133,10 +192,11 @@
private static void _saveTranslationFile() {
FileOutputStream fos = null;
- File finalFile = new File(L10n.PREFIX +
L10n.getSelectedLanguage() + L10n.OVERRIDE_SUFFIX);
+ File finalFile = new
File(getSelectedLanguage().getL10nOverrideFilename());
try {
// We don't set deleteOnExit on it : if the save
operation fails, we want a backup
+ // FIXME: REDFLAG: not symlink-race proof!
File tempFile = new File(finalFile.getParentFile(),
finalFile.getName()+".bak");
Logger.minor("L10n", "The temporary filename is : " +
tempFile);
@@ -270,7 +330,7 @@
/**
* Allows things like :
- * L10n.getString("testing.test", new String[]{ "test1", "test2" }, new
String[] { "a", "b" })
+ * L10n.getString("testing.test", new String[]{ "test1", "test2" }, "a",
"b" })
*
* @param key
* @param patterns : a list of patterns wich are matchable from the
translation
@@ -311,7 +371,7 @@
*
* @return String
*/
- public static String getSelectedLanguage() {
+ public static LANGUAGE getSelectedLanguage() {
synchronized (sync) {
if((currentClass == null) ||
(currentClass.selectedLanguage == null))
return FALLBACK_DEFAULT;
@@ -326,26 +386,18 @@
* @param name
* @return the Properties object or null if not found
*/
- public static SimpleFieldSet loadTranslation(String name) {
- String shortCountryCode = mapLanguageNameToShortCode(name);
- if(shortCountryCode == null) {
- Logger.error("L10n", "Can't map "+name+" to a country
code!");
- return null;
- } else
- name = shortCountryCode;
- name = PREFIX.replace ('.',
'/').concat(PREFIX.concat(name.concat(SUFFIX)));
-
+ public static SimpleFieldSet loadTranslation(LANGUAGE lang) {
SimpleFieldSet result = null;
InputStream in = null;
try {
ClassLoader loader = ClassLoader.getSystemClassLoader();
// Returns null on lookup failures:
- in = loader.getResourceAsStream(name);
+ in = loader.getResourceAsStream(lang.getL10nFilename());
if(in != null)
result = SimpleFieldSet.readFrom(in, false,
false);
} catch (Exception e) {
- Logger.error("L10n", "Error while loading the l10n file
from " + name + " :" + e.getMessage(), e);
+ Logger.error("L10n", "Error while loading the l10n file
from " + lang.getL10nFilename() + " :" + e.getMessage(), e);
result = null;
} finally {
Closer.close(in);
@@ -353,43 +405,7 @@
return result;
}
-
- /**
- * Map a full string language name to the corresponding country short
code
- *
- * @param The name to look for
- * @return The two letters short code OR null if not found
- */
- public static String mapLanguageNameToShortCode(String name) {
- for(int i=0; i<AVAILABLE_LANGUAGES.length; i++) {
- String currentShortCode = AVAILABLE_LANGUAGES[i][0];
- String currentLongName = AVAILABLE_LANGUAGES[i][1];
- String currentCountryCodeName =
AVAILABLE_LANGUAGES[i][2];
-
- if(currentShortCode.equalsIgnoreCase(name) ||
currentLongName.equalsIgnoreCase(name) ||
currentCountryCodeName.equalsIgnoreCase(name))
- return currentShortCode;
- }
- return null;
- }
- /**
- * Map a language identifier to its corresponding long name
- *
- * @param The name to look for
- * @return The full text language name OR null if not found
- */
- public static String mapLanguageNameToLongName(String name) {
- for(int i=0; i<AVAILABLE_LANGUAGES.length; i++) {
- String currentShortCode = AVAILABLE_LANGUAGES[i][0];
- String currentLongName = AVAILABLE_LANGUAGES[i][1];
- String currentCountryCodeName =
AVAILABLE_LANGUAGES[i][2];
-
- if(currentShortCode.equalsIgnoreCase(name) ||
currentLongName.equalsIgnoreCase(name) ||
currentCountryCodeName.equalsIgnoreCase(name))
- return currentLongName;
- }
- return null;
- }
-
public static boolean isOverridden(String key) {
synchronized(sync) {
if(translationOverride == null) return false;
Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java 2008-08-19 18:50:57 UTC (rev
22031)
+++ trunk/freenet/src/freenet/node/Node.java 2008-08-19 19:11:49 UTC (rev
22032)
@@ -207,11 +207,11 @@
private static class L10nCallback extends StringCallback implements
EnumerableOptionCallback {
public String get() {
- return
L10n.mapLanguageNameToLongName(L10n.getSelectedLanguage());
+ return L10n.getSelectedLanguage().getFullName();
}
public void set(String val) throws InvalidConfigValueException {
- if(get().equalsIgnoreCase(val)) return;
+ if(val == null || get().equalsIgnoreCase(val)) return;
try {
L10n.setLanguage(val);
} catch (MissingResourceException e) {
@@ -224,10 +224,7 @@
}
public String[] getPossibleValues() {
- String[] result = new
String[L10n.AVAILABLE_LANGUAGES.length];
- for(int i=0; i<L10n.AVAILABLE_LANGUAGES.length; i++)
- result[i] = L10n.AVAILABLE_LANGUAGES[i][1];
- return result;
+ return L10n.LANGUAGE.valuesWithFullNames();
}
}
@@ -656,7 +653,7 @@
try {
L10n.setLanguage(nodeConfig.getOption("l10n").getDefault());
} catch (MissingResourceException e1) {
- L10n.setLanguage(L10n.FALLBACK_DEFAULT);
+
L10n.setLanguage(L10n.LANGUAGE.ENGLISH.getShortCode());
}
}
Modified: trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java 2008-08-19
18:50:57 UTC (rev 22031)
+++ trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java 2008-08-19
19:11:49 UTC (rev 22032)
@@ -43,7 +43,7 @@
sfs.putSingle("Testnet", Boolean.toString(node == null ? false
: node.isTestnetEnabled()));
sfs.putSingle("CompressionCodecs",
Integer.toString(Compressor.countCompressAlgorithms()));
sfs.putSingle("ConnectionIdentifier", id);
- sfs.putSingle("NodeLanguage", L10n.getSelectedLanguage());
+ sfs.putSingle("NodeLanguage",
L10n.getSelectedLanguage().toString());
return sfs;
}