multi-language support
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Sun, 29 Oct 2017 21:53:26 +0000 (22:53 +0100)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Sun, 29 Oct 2017 21:53:26 +0000 (22:53 +0100)
war/src/main/java/pnews/Category.java
war/src/main/java/pnews/servlet/HTML.java
war/src/main/java/pnews/servlet/Pnews.java
war/src/main/resources/feeds.json

index ab63a05..ac59c59 100644 (file)
@@ -29,6 +29,10 @@ public class Category {
                 return id;
         }
         
+        public String getLanguage() {
+                return language;
+        }
+        
         @Override
         public String toString() {
                 return getLabel();
index 321c5a7..a9ad838 100644 (file)
@@ -8,6 +8,7 @@ import java.util.logging.Logger;
 
 import pnews.Article;
 import pnews.Category;
+import pnews.Language;
 
 public class HTML {
         private static final String CLASS_NAME= HTML.class.getName();
@@ -71,13 +72,16 @@ public class HTML {
                buf.append("</div>\n");         
        }
        
-       private static void appendMenu(StringBuffer buf, Category catActive, Category[] cats) {
+       private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
                String cl;
                
                buf.append("<nav>\n");
                buf.append("<ul>\n");
 
-               for (Category cat: cats) {
+               for (Category cat: cfg.getCategories()) {
+                       if (!cat.getLanguage().equals(catActive.getLanguage()))
+                               continue;
+                       
                        buf.append("<li>");
                        
                        if (cat.equals(catActive))
@@ -88,16 +92,19 @@ public class HTML {
                        appendA(buf, cat.getLabel(), cat.getURL(), cl);
                        buf.append("</li>");
                }
-                buf.append("<li><a href='/en'>EN</a></li>");
-                buf.append("<li><a href='/fr'>FR</a></li>");           
+               
+               for (Language l: cfg.getLanguages())
+                       buf.append("<li><a href='" + l.toURL() + "'>" + l.getLabel() + "</a></li>");
+                
                buf.append("</ul>\n");
                
                buf.append("</nav>\n");
        }
        
-       public static String toHTML(List<Article> articles, Category catActive, Category[] cats) {
+       public static String toHTML(List<Article> articles, Category catActive, Config cfg) {
                StringBuffer buf;
                int i;
+               Category[] cats;
                
                buf = new StringBuffer();
                buf.append("<!DOCTYPE html>\n");
@@ -111,7 +118,9 @@ public class HTML {
                buf.append("</head>\n");
                buf.append("<body>\n");
                
-               appendMenu(buf, catActive, cats);
+               cats = cfg.getCategories();
+               
+               appendMenu(buf, catActive, cfg);
                
                i = 0;
                for (Article e: articles) {
index 4003fed..029f3c7 100644 (file)
@@ -112,13 +112,13 @@ public class Pnews extends HttpServlet {
                 try {
                         articles = provider.getArticles(cat);
                         if (articles != null) {
-                                html = HTML.toHTML(articles, cat, config.getCategories());
+                                html = HTML.toHTML(articles, cat, config);
                                 rp.setContentType("text/html;charset=utf-8");
                                 rp.getWriter().write(html);
                                 rp.setCharacterEncoding("utf-8");
                         } else {
                                 LOG.severe("writeArticles cannot retrieve any articles");
-                                html = HTML.toHTML(new ArrayList<Article>(), cat, config.getCategories());
+                                html = HTML.toHTML(new ArrayList<Article>(), cat, config);
                                 rp.setContentType("text/html");
                                 rp.getWriter().write(html);
                         }
index 8e71c0c..7e971d0 100644 (file)
                         "label": "People",
                         "title": "People",
                         "language": "fr"
+                }, {
+                        "id": "ubuntu",
+                        "label": "Ubuntu",
+                        "title": "Ubuntu",
+                        "language": "en"
                 }
-
         ],
         "feeds": {
                 "http://www.europe1.fr/var/export/rss/europe1/actus.xml": { "categories": ["actualite"] },
@@ -87,6 +91,7 @@
                 "http://www.fredzone.org/feed": { "categories": ["technologie"] },
                 "http://www.futura-sciences.com/rss/actualites.xml": { "categories": ["technologie"] },
                 "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all": { "categories": ["technologie"] },
-                "https://korben.info/feed": { "categories": ["technologie"]}
+                "https://korben.info/feed": { "categories": ["technologie"]},
+                "https://insights.ubuntu.com/feed/": { "categories": ["ubuntu"]}
         }
 }