diff gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/ressource/RessourceFactory.java @ 313:7b4e2f80cba2

New method in RessourceFactory to read locales supported by the server. Added config file for supported locales. gnv-artifacts/trunk@371 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 26 Nov 2009 08:40:15 +0000
parents 7fb9441dd8af
children e6e172a4915e
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/ressource/RessourceFactory.java	Wed Nov 25 11:41:42 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/ressource/RessourceFactory.java	Thu Nov 26 08:40:15 2009 +0000
@@ -3,6 +3,16 @@
  */
 package de.intevation.gnv.artifacts.ressource;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Locale;
+import java.util.List;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
@@ -28,11 +38,14 @@
 
     private static String RESSOURCE_BASE_ID = "artifact.ressource.dir";
 
-    private static String ressourceName = "artifactMessages";
-    private static String DEFAULT_DIR = "lang";
+    private static String ressourceName    = "artifactMessages";
+    private static String DEFAULT_DIR      = "lang";
+    private static String LANG_CONFIG_FILE = "lang.conf";
 
     private String ressourceDir = null;
 
+    private Locale[] locales = null;
+
     /**
      * Basic-Constructor of this Class
      */
@@ -53,6 +66,71 @@
         return instance;
     }
 
+
+    /**
+     * This method reads locales, configured in LANG_CONFIG_FILE, from
+     * filesystem and returns them as array.
+     *
+     * @return Array of locales supported by the the server.
+     */
+    public Locale[] getLocales() {
+
+        if (locales != null)
+            return locales;
+
+        log.debug("Supported locales have not been read - read now.");
+        try {
+            String config  = "/" + ressourceDir + "/" + LANG_CONFIG_FILE;
+            InputStream in = RessourceFactory.class.getResourceAsStream(config);
+
+            BufferedReader reader = new BufferedReader(
+                new InputStreamReader(in)
+            );
+
+            String line     = null;
+            String country  = null;
+            String language = null;
+            int idx         =   -1;
+
+            List tmpLocales = new ArrayList();
+
+            while((line = reader.readLine()) != null) {
+                // validate if defined locale has a valid length
+                if (line.length() != 2 && line.length() != 5) {
+                    log.warn("Illegal locale definition found.");
+                    continue;
+                }
+
+                idx = line.indexOf("_");
+                if (idx > 0) {
+                    // found locale containing language and country code
+                    language = line.substring(0, idx);
+                    country  = line.substring(idx+1);
+                    tmpLocales.add(new Locale(language, country));
+                }
+                else {
+                    // found locale containing languagey code only
+                    tmpLocales.add(new Locale(line));
+                }
+            }
+
+            locales = (Locale[]) tmpLocales.toArray(
+                new Locale[tmpLocales.size()]
+            );
+
+            return locales;
+        }
+        catch (FileNotFoundException fnfe) {
+            log.warn("File not found: " + LANG_CONFIG_FILE, fnfe);
+        }
+        catch (IOException ioe) {
+            log.warn(ioe.getLocalizedMessage(), ioe);
+        }
+
+        return null;
+    }
+
+
     /**
      * Deliveres the translated Value for an Key to an given Language
      * 

http://dive4elements.wald.intevation.org