Doc: Add a way to localize generated messages 77/9277/3
Calixte DENIZET [Tue, 25 Sep 2012 20:05:38 +0000 (22:05 +0200)]
Change-Id: I4c457a4dbdeb25e93e62e16f3bd460d6f2b7fec0

scilab/modules/helptools/src/java/org/scilab/modules/helptools/CHMDocbookTagConverter.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/HTMLDocbookTagConverter.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/JavaHelpDocbookTagConverter.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/SciDocMain.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/TemplateHandler.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/TemplateLocalization.java [new file with mode: 0644]

index d216c50..f8bcdcb 100644 (file)
@@ -33,7 +33,6 @@ public class CHMDocbookTagConverter extends HTMLDocbookTagConverter {
     private StringBuilder buffer = new StringBuilder(8192);
     private String outName;
     private List<String> filesList = new ArrayList<String>();
-    private String language;
     private String docWebsite;
 
     /**
@@ -50,9 +49,8 @@ public class CHMDocbookTagConverter extends HTMLDocbookTagConverter {
      * @param language the language to use ('en_US', 'fr_FR', ...)
      */
     public CHMDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, String docWebsite, boolean isToolbox, String urlBase, String language) throws IOException, SAXException {
-        super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, HTMLDocbookTagConverter.GenerationType.CHM);
+        super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.CHM);
         this.outName = new File(outName).getCanonicalPath() + File.separator;
-        this.language = language;
         this.docWebsite = docWebsite;
     }
 
index ba48800..cfbc58a 100644 (file)
@@ -88,6 +88,7 @@ public class HTMLDocbookTagConverter extends DocbookTagConverter implements Temp
     protected String prependToProgramListing;
     protected String currentId;
     protected String indexFilename = "index" /*UUID.randomUUID().toString()*/ + ".html";
+    protected String language;
 
     protected boolean isToolbox;
     protected final GenerationType type;
@@ -104,7 +105,7 @@ public class HTMLDocbookTagConverter extends DocbookTagConverter implements Temp
      * @param isToolbox is true when compile a toolbox' help
      * @param urlBase the base url for external link
      */
-    public HTMLDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, boolean isToolbox, String urlBase, GenerationType type) throws IOException, SAXException {
+    public HTMLDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, boolean isToolbox, String urlBase, String language, GenerationType type) throws IOException, SAXException {
         super(inName);
 
         this.version = version;
@@ -120,37 +121,38 @@ public class HTMLDocbookTagConverter extends DocbookTagConverter implements Temp
         this.urlBase = urlBase;
         this.linkToTheWeb = urlBase != null && !urlBase.equals("scilab://");
         this.isToolbox = isToolbox;
+        this.language = language;
         this.type = type;
         if (isToolbox) {// we generate a toolbox's help
             HTMLScilabCodeHandler.setLinkWriter(new AbstractScilabCodeHandler.LinkWriter() {
-                    public String getLink(String id) {
-                        if (id.length() > 0 && id.charAt(0) == '%') {
-                            id = id.replace("%", "percent");
-                        }
-                        String link = mapId.get(id);
-                        if (link == null) {
-                            return HTMLDocbookTagConverter.this.urlBase + id;
-                        } else {
-                            return link;
-                        }
+                public String getLink(String id) {
+                    if (id.length() > 0 && id.charAt(0) == '%') {
+                        id = id.replace("%", "percent");
                     }
-                });
+                    String link = mapId.get(id);
+                    if (link == null) {
+                        return HTMLDocbookTagConverter.this.urlBase + id;
+                    } else {
+                        return link;
+                    }
+                }
+            });
         } else {// we generate Scilab's help
             HTMLScilabCodeHandler.setLinkWriter(new AbstractScilabCodeHandler.LinkWriter() {
-                    public String getLink(String id) {
-                        if (id.length() > 0 && id.charAt(0) == '%') {
-                            id = id.replace("%", "percent");
-                        }
-                        return mapId.get(id);
+                public String getLink(String id) {
+                    if (id.length() > 0 && id.charAt(0) == '%') {
+                        id = id.replace("%", "percent");
                     }
-                });
+                    return mapId.get(id);
+                }
+            });
         }
 
         xmlLexer = new XMLLexer();
         cLexer = new CLexer();
         javaLexer = new JavaLexer();
         File tpl = new File(template);
-        templateHandler = new TemplateHandler(this, tpl);
+        templateHandler = new TemplateHandler(this, tpl, language);
         ImageConverter.registerExternalImageConverter(LaTeXImageConverter.getInstance(type));
         ImageConverter.registerExternalImageConverter(MathMLImageConverter.getInstance(type));
         ImageConverter.registerExternalImageConverter(SVGImageConverter.getInstance(type));
index ae45c65..09c118e 100644 (file)
@@ -42,8 +42,8 @@ public class JavaHelpDocbookTagConverter extends HTMLDocbookTagConverter {
      * @param isToolbox is true when compile a toolbox' help
      * @param urlBase the base url for external link
      */
-    public JavaHelpDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, boolean isToolbox, String urlBase) throws IOException, SAXException {
-        super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, HTMLDocbookTagConverter.GenerationType.JAVAHELP);
+    public JavaHelpDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, boolean isToolbox, String urlBase, String language) throws IOException, SAXException {
+        super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.JAVAHELP);
         prependToProgramListing = "<table border=\"0\" width=\"100%\"><tr><td width=\"98%\">";
         appendToProgramListing = "</td><td valign=\"top\"><a href=\"scilab://scilab.execexample/\"><img src=\"ScilabExecute.png\" border=\"0\"/></a></td><td valign=\"top\"><a href=\"scilab://scilab.editexample/\"><img src=\"ScilabEdit.png\" border=\"0\"/></a></td><td></td></tr></table>";
         appendForExecToProgramListing = "</td><td valign=\"top\"><a href=\"scilab://scilab.execexample/\"><img src=\"ScilabExecute.png\" border=\"0\"/></a></td><td></td></tr></table>";
index e896765..87aea2a 100644 (file)
@@ -109,15 +109,15 @@ public final class SciDocMain {
             String urlBase = null;
 
             if (format.equalsIgnoreCase("javahelp")) {
-                converter = new JavaHelpDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, "scilab://");
+                converter = new JavaHelpDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, "scilab://", language);
             } else {
                 if (isToolbox) {
                     urlBase = conf.getWebSiteURL() + language + "/";
                 }
                 if (format.equalsIgnoreCase("html")) {
-                    converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase, HTMLDocbookTagConverter.GenerationType.HTML);
+                    converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.HTML);
                 } else if (format.equalsIgnoreCase("web")) {
-                    converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase, HTMLDocbookTagConverter.GenerationType.WEB);
+                    converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.WEB);
                 } else if (format.equalsIgnoreCase("chm")) {
                     converter = new CHMDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, conf.getWebSiteURL(), isToolbox, urlBase, language);
                 }
index a73bbc4..873edec 100644 (file)
@@ -29,9 +29,11 @@ public class TemplateHandler {
 
     private String[] array;
     private TemplateFiller filler;
+    private String language;
 
-    public TemplateHandler(TemplateFiller filler, File f) {
+    public TemplateHandler(TemplateFiller filler, File f, String language) {
         this.filler = filler;
+        this.language = language;
         parseFile(f);
     }
 
@@ -68,6 +70,9 @@ public class TemplateHandler {
                         str = filler.makeVersion(id);;
                     } else if (array[i + 1].equals("start")) {
                         str = filler.makeStart(id);
+                    } else if (array[i + 1].startsWith("translate=")) {
+                        String toTranslate = array[i + 1].substring("translate=".length());
+                        str = TemplateLocalization.getLocalized(language, toTranslate);
                     }
                     writer.append(str);
                 }
diff --git a/scilab/modules/helptools/src/java/org/scilab/modules/helptools/TemplateLocalization.java b/scilab/modules/helptools/src/java/org/scilab/modules/helptools/TemplateLocalization.java
new file mode 100644 (file)
index 0000000..466ee1b
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+package org.scilab.modules.helptools;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Class to handle a template localization
+ * @author Calixte DENIZET
+ */
+public final class TemplateLocalization {
+
+    private static final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
+
+    static {
+        Map<String, String> report_an_issue = new HashMap<String, String>();
+        report_an_issue.put("fr_FR", "Reporter un bug");
+        report_an_issue.put("pt_BR", "Reportero a bugo");
+        report_an_issue.put("es_ES", "Raportare el bugo");
+
+        map.put("Report an issue", report_an_issue);
+    }
+
+    public static final String getLocalized(final String language, final String str) {
+        if (language.equals("en_US")) {
+            return str;
+        }
+
+        Map<String, String> m = map.get(str);
+        if (m == null) {
+            return str;
+        }
+
+        String s = m.get(language);
+        if (s == null) {
+            return str;
+        }
+
+        return s;
+    }
+}
+