comparison 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
comparison
equal deleted inserted replaced
312:19fbd03544ea 313:7b4e2f80cba2
1 /** 1 /**
2 * 2 *
3 */ 3 */
4 package de.intevation.gnv.artifacts.ressource; 4 package de.intevation.gnv.artifacts.ressource;
5 5
6 import java.io.BufferedReader;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Locale;
15 import java.util.List;
6 import java.util.MissingResourceException; 16 import java.util.MissingResourceException;
7 import java.util.ResourceBundle; 17 import java.util.ResourceBundle;
8 18
9 import org.apache.log4j.Logger; 19 import org.apache.log4j.Logger;
10 20
26 */ 36 */
27 private static RessourceFactory instance = null; 37 private static RessourceFactory instance = null;
28 38
29 private static String RESSOURCE_BASE_ID = "artifact.ressource.dir"; 39 private static String RESSOURCE_BASE_ID = "artifact.ressource.dir";
30 40
31 private static String ressourceName = "artifactMessages"; 41 private static String ressourceName = "artifactMessages";
32 private static String DEFAULT_DIR = "lang"; 42 private static String DEFAULT_DIR = "lang";
43 private static String LANG_CONFIG_FILE = "lang.conf";
33 44
34 private String ressourceDir = null; 45 private String ressourceDir = null;
46
47 private Locale[] locales = null;
35 48
36 /** 49 /**
37 * Basic-Constructor of this Class 50 * Basic-Constructor of this Class
38 */ 51 */
39 private RessourceFactory() { 52 private RessourceFactory() {
50 if (instance == null) { 63 if (instance == null) {
51 instance = new RessourceFactory(); 64 instance = new RessourceFactory();
52 } 65 }
53 return instance; 66 return instance;
54 } 67 }
68
69
70 /**
71 * This method reads locales, configured in LANG_CONFIG_FILE, from
72 * filesystem and returns them as array.
73 *
74 * @return Array of locales supported by the the server.
75 */
76 public Locale[] getLocales() {
77
78 if (locales != null)
79 return locales;
80
81 log.debug("Supported locales have not been read - read now.");
82 try {
83 String config = "/" + ressourceDir + "/" + LANG_CONFIG_FILE;
84 InputStream in = RessourceFactory.class.getResourceAsStream(config);
85
86 BufferedReader reader = new BufferedReader(
87 new InputStreamReader(in)
88 );
89
90 String line = null;
91 String country = null;
92 String language = null;
93 int idx = -1;
94
95 List tmpLocales = new ArrayList();
96
97 while((line = reader.readLine()) != null) {
98 // validate if defined locale has a valid length
99 if (line.length() != 2 && line.length() != 5) {
100 log.warn("Illegal locale definition found.");
101 continue;
102 }
103
104 idx = line.indexOf("_");
105 if (idx > 0) {
106 // found locale containing language and country code
107 language = line.substring(0, idx);
108 country = line.substring(idx+1);
109 tmpLocales.add(new Locale(language, country));
110 }
111 else {
112 // found locale containing languagey code only
113 tmpLocales.add(new Locale(line));
114 }
115 }
116
117 locales = (Locale[]) tmpLocales.toArray(
118 new Locale[tmpLocales.size()]
119 );
120
121 return locales;
122 }
123 catch (FileNotFoundException fnfe) {
124 log.warn("File not found: " + LANG_CONFIG_FILE, fnfe);
125 }
126 catch (IOException ioe) {
127 log.warn(ioe.getLocalizedMessage(), ioe);
128 }
129
130 return null;
131 }
132
55 133
56 /** 134 /**
57 * Deliveres the translated Value for an Key to an given Language 135 * Deliveres the translated Value for an Key to an given Language
58 * 136 *
59 * @param locale 137 * @param locale

http://dive4elements.wald.intevation.org