Author: nextgens
Date: 2007-04-20 21:33:20 +0000 (Fri, 20 Apr 2007)
New Revision: 12831

Modified:
   trunk/plugins/TranslationHelper/TranslationHelper.java
Log:
TranslationHelper: it doesn't work but it *should* work, I guess there is a bug 
in the implementation of the SFS iterator :/

Modified: trunk/plugins/TranslationHelper/TranslationHelper.java
===================================================================
--- trunk/plugins/TranslationHelper/TranslationHelper.java      2007-04-20 
21:03:59 UTC (rev 12830)
+++ trunk/plugins/TranslationHelper/TranslationHelper.java      2007-04-20 
21:33:20 UTC (rev 12831)
@@ -4,9 +4,7 @@
 package plugins.TranslationHelper;

 import java.io.IOException;
-import java.io.StringWriter;
-import java.util.Enumeration;
-import java.util.Properties;
+import java.util.Iterator;

 import freenet.l10n.L10n;
 import freenet.pluginmanager.DownloadPluginHTTPException;
@@ -17,6 +15,7 @@
 import freenet.pluginmanager.PluginRespirator;
 import freenet.pluginmanager.RedirectPluginHTTPException;
 import freenet.support.HTMLNode;
+import freenet.support.SimpleFieldSet;
 import freenet.support.api.HTTPRequest;

 /**
@@ -28,20 +27,17 @@
        public static String PLUGIN_NAME = "TranslationHelper";
        public static String PLUGIN_BASE_URL = "/plugins/plugins." + 
PLUGIN_NAME + '.' + PLUGIN_NAME + '/';
        private PluginRespirator pr;
-       private Properties editedProperties;
+       private SimpleFieldSet editedProperties;
        private String editedLang;

        public void runPlugin(PluginRespirator pr) {
                this.pr = pr;
                editedLang = L10n.availableLanguages[0];
-               editedProperties = L10n.loadProperties(editedLang);
+               editedProperties = L10n.loadTranslation(editedLang);
        }

-       public void terminate() {
-               // TODO Auto-generated method stub
+       public void terminate() {}

-       }
-
        public String handleHTTPGet(HTTPRequest request) throws 
PluginHTTPException {
                if(request.isParameterSet("reload")) {
                        editedLang = request.getParam("reload");
@@ -76,15 +72,14 @@
                        HTMLNode updateForm =  pr.addFormChild(translationNode, 
PLUGIN_BASE_URL, "update");
                        HTMLNode legendTable = updateForm.addChild("table", 
"class", "translation");

-                       HTMLNode legendRow = legendTable.addChild("tr");
+                       HTMLNode legendRow = 
legendTable.addChild("tr").addChild("b");
                        legendRow.addChild("td", "class", "translation-key", 
"Translation key");
                        legendRow.addChild("td", "class", "translation-key", 
"Original translation");
                        legendRow.addChild("td", "class", "translation-key", 
"Current translation");

-                       final Enumeration keys = L10n.getKeys();
-       
-                       while(keys.hasMoreElements()){
-                               String key = (String)keys.nextElement();
+                       final Iterator it = editedProperties.keyIterator();
+                       while(it.hasNext()) {
+                               String key = (String) it.next();
                                HTMLNode contentRow = 
legendTable.addChild("tr");
                                contentRow.addChild("td", "class", 
"translation-key",
                                                key
@@ -92,12 +87,12 @@
                                contentRow.addChild("td", "class", 
"translation-orig",
                                                L10n.getDefaultString(key)
                                );
-                               String newValue = 
editedProperties.getProperty(key);
+                               String newValue = L10n.getString(key, true);

                                contentRow.addChild("td", "class", 
"translation-new").addChild(
                                                "input",
                                                new String[] { "type", "name", 
"value" },
-                                               new String[] { "text", key,  
newValue
+                                               new String[] { "text", key,  
(newValue == null ? "" : newValue)
                                });
                        }
                        HTMLNode contentRow = legendTable.addChild("tr");
@@ -116,14 +111,10 @@
                        contentRow.addChild("a", "href", 
PLUGIN_BASE_URL).addChild("#", "Return to the plugin's main page");

                        return pageNode.generate();
-                       // It doesn't help: we need to decode unicode 
characters!
-                       //throw new PluginHTTPException(200, "text/html; 
charset=ISO-8859-1", "Found", pageNode.generate());
                } else if(request.isParameterSet("getTranlationFile")) {
                        byte[] data = null;
                        try {
-                               StringWriter sw = new StringWriter();
-                               editedProperties.store(sw, "Translation file 
for "+editedLang);
-                               data = 
sw.getBuffer().toString().getBytes("UTF-8");
+                               data = 
editedProperties.toOrderedString().getBytes("UTF-8");
                        } catch (IOException e) {
                                throw new PluginHTTPException("Should not 
happen !", PLUGIN_BASE_URL);
                        }
@@ -151,30 +142,23 @@
                }

                availableLanguagesInfoboxContent.addChild("a", "href", 
PLUGIN_BASE_URL+"?getTranlationFile").addChild("#", "Download the translation 
file");
-//             availableLanguagesInfoboxContent.addChild(
-//                             "#",
-//                             "Or create ").addChild(
-//                                             "a",
-//                                             "href",
-//                                             PLUGIN_BASE_URL + 
"?new").addChild(
-//                                                             "#",
-//                                                             "a new one"
-//             );

                return pageNode.generate();
        }

        public String handleHTTPPost(HTTPRequest request) throws 
PluginHTTPException {
                if(request.isPartSet("update")){                        
-                       Enumeration keys = editedProperties.keys();
-                       while(keys.hasMoreElements()) {
-                               String key = (String)keys.nextElement();
-                               editedProperties.setProperty(key, 
request.getPartAsString(key, 256));
+                       Iterator it = editedProperties.keyIterator();
+                       while(it.hasNext()) {
+                               String key = (String) it.next();
+                               editedProperties.putOverwrite(key, 
request.getPartAsString(key, 256));
                        }
+                       L10n.setLanguage(editedProperties);

                        throw new RedirectPluginHTTPException("Redirect", 
PLUGIN_BASE_URL, PLUGIN_BASE_URL+"?lang="+editedLang);
                }else if(request.isPartSet("reload")) {
-                       editedProperties = L10n.loadProperties(editedLang);
+                       editedProperties = L10n.loadTranslation(editedLang);
+                       L10n.setLanguage(editedProperties);

                        throw new RedirectPluginHTTPException("Redirect", 
PLUGIN_BASE_URL, PLUGIN_BASE_URL+"?lang="+editedLang);
                }


Reply via email to