32 * horizontalSpan, labelStyle); |
32 * horizontalSpan, labelStyle); |
33 * |
33 * |
34 * Label noteLabel = createLabel( |
34 * Label noteLabel = createLabel( |
35 * res.string("note"), horizontalSpan, labelStyle); |
35 * res.string("note"), horizontalSpan, labelStyle); |
36 * </pre> |
36 * </pre> |
|
37 * |
|
38 * @author Nokia Corporation |
|
39 * @version 1.0 |
37 */ |
40 */ |
38 public class ResourceLoader |
41 public class ResourceLoader |
39 { |
42 { |
40 /** AVKON UI identifier. */ |
|
41 public static final int AVKON = 1; |
|
42 /** QT UI identifier. */ |
|
43 public static final int QT = 2; |
|
44 |
|
45 /** Localisation resource basepath */ |
43 /** Localisation resource basepath */ |
46 private static final String LOC_RESOURCE_BASE = "/resources/com/nokia/mj/impl/"; |
44 private static final String LOC_RESOURCE_BASE = "/resources/com/nokia/mj/impl/"; |
47 |
45 |
48 /** Map for ResourceLoader instances. */ |
46 /** Map for ResourceLoader instances. */ |
49 private static Hashtable resourceLoaders = new Hashtable(); |
47 private static Hashtable resourceLoaders = new Hashtable(); |
50 |
48 |
51 /** Resource string map. Null if resource could not be loaded. */ |
49 /** Resource string map. Null if resource could not be loaded. */ |
52 private Hashtable resourceMap = new Hashtable(); |
50 private Hashtable resourceMap = new Hashtable(); |
53 |
51 |
54 /** Resource name prefix */ |
52 /** Resource name prefix */ |
55 private String prefix = null; |
53 private String prefix; |
56 |
|
57 /** Platform localisation type. */ |
|
58 private int locType = -1; |
|
59 |
54 |
60 /*** ----------------------------- PUBLIC ------------------------------ */ |
55 /*** ----------------------------- PUBLIC ------------------------------ */ |
61 |
|
62 public static ResourceLoader getInstance(String avkonFileName, |
|
63 String avkonPrefix, |
|
64 String qtFileName, |
|
65 String qtPrefix) |
|
66 { |
|
67 // Construct key from filenames and prefixes, this is the same |
|
68 // between platforms. |
|
69 String key = (new StringBuffer()).append(avkonFileName).append(":") |
|
70 .append(avkonPrefix).append(":").append(qtFileName).append(":") |
|
71 .append(qtPrefix).toString(); |
|
72 ResourceLoader result = (ResourceLoader)resourceLoaders.get(key); |
|
73 |
|
74 if (result == null) |
|
75 { |
|
76 result = new ResourceLoader(avkonFileName, avkonPrefix, qtFileName, qtPrefix); |
|
77 resourceLoaders.put(key, result); |
|
78 } |
|
79 return result; |
|
80 } |
|
81 |
56 |
82 /** |
57 /** |
83 * Returns a resource loader instance. |
58 * Returns a resource loader instance. |
84 * |
59 * |
85 * @param resourceName name of the resource |
60 * @param resourceName name of the resource |
97 } |
72 } |
98 return result; |
73 return result; |
99 } |
74 } |
100 |
75 |
101 /** |
76 /** |
102 * Private constructor. Loads localisation resource file. |
|
103 * On Avkon UI it's resources are loaded. On Qt platfor it's |
|
104 * resource is first read and if that fails Avkon one is read. |
|
105 * |
|
106 * @param avkonFileName Avkon localisation resource file. |
|
107 * @param avkonPrefix Avkon logical string prefix. |
|
108 * @param qtFileName Qt localisation resource file. |
|
109 * @param qtPrefix Qt logical string prefix. |
|
110 */ |
|
111 private ResourceLoader(String avkonFileName, |
|
112 String avkonPrefix, |
|
113 String qtFileName, |
|
114 String qtPrefix) |
|
115 { |
|
116 String localeId = getLocaleIdQt(); |
|
117 |
|
118 if (localeId == null) |
|
119 { |
|
120 locType = AVKON; |
|
121 prefix = avkonPrefix; |
|
122 loadFile(avkonFileName, true); |
|
123 } |
|
124 else |
|
125 { |
|
126 if (!loadFile(qtFileName, false)) |
|
127 { |
|
128 // Fallback to Avkon |
|
129 locType = AVKON; |
|
130 prefix = avkonPrefix; |
|
131 loadFile(avkonFileName, true); |
|
132 } |
|
133 else |
|
134 { |
|
135 locType = QT; |
|
136 prefix = qtPrefix; |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Creates resource loader, using the current locale of the environment. |
77 * Creates resource loader, using the current locale of the environment. |
143 * |
78 * |
144 * @param resourceName name of the resource |
79 * @param resourceName name of the resource |
145 * @param aPrefix prefix added before each id when retrieving |
80 * @param aPrefix prefix added before each id when retrieving |
146 */ |
81 */ |
147 public ResourceLoader(String resourceName, String aPrefix) |
82 public ResourceLoader(String resourceName, String aPrefix) |
148 { |
83 { |
149 locType = AVKON; |
|
150 prefix = aPrefix; |
84 prefix = aPrefix; |
151 loadFile(resourceName, true); // Avkon |
85 loadFile(resourceName); |
152 } |
86 } |
153 |
87 |
154 /** |
88 /** |
155 * Get a string formatter of a given resource id. |
89 * Get a string formatter of a given resource id. |
156 * |
90 * |
158 * @return formatter instance |
92 * @return formatter instance |
159 * @see Formatter |
93 * @see Formatter |
160 */ |
94 */ |
161 public Formatter format(String id) |
95 public Formatter format(String id) |
162 { |
96 { |
163 return new Formatter(string(id), locType); |
97 return new Formatter(string(id)); |
164 } |
|
165 |
|
166 /** |
|
167 * Get a string formatter of a given resource id. |
|
168 * |
|
169 * @param avkonId Avkon resource id. |
|
170 * @param qtId Qt resource id. |
|
171 * @return formatter instance |
|
172 * @see Formatter |
|
173 */ |
|
174 public Formatter format(String avkonId, String qtId) |
|
175 { |
|
176 if (locType == AVKON) |
|
177 { |
|
178 return new Formatter(string(avkonId), locType); |
|
179 } |
|
180 else |
|
181 { |
|
182 return new Formatter(string(qtId), locType); |
|
183 } |
|
184 } |
|
185 |
|
186 /** |
|
187 * Formats localised text with specified parameters from an array. |
|
188 * |
|
189 * @param avkonId Avkon resource id. |
|
190 * @param qtId Qt resource id. |
|
191 * @param textParameters parameters to be filled into the text. |
|
192 * @return localised text formatted with the provided parameters. |
|
193 * @see Formatter |
|
194 */ |
|
195 public String format(String avkonId, String qtId, Object[] textParameters) |
|
196 { |
|
197 if (locType == AVKON) |
|
198 { |
|
199 return new Formatter(string(avkonId), locType).format(textParameters); |
|
200 } |
|
201 else |
|
202 { |
|
203 return new Formatter(string(qtId), locType).format(textParameters); |
|
204 } |
|
205 } |
|
206 |
|
207 /** |
|
208 * Get a string formatter of a given resource id. |
|
209 * |
|
210 * @param id resource id |
|
211 * @return formatter instance |
|
212 * @see Formatter |
|
213 */ |
|
214 public Formatter format(Id id) |
|
215 { |
|
216 return new Formatter(id.getString(locType), locType); |
|
217 } |
98 } |
218 |
99 |
219 /** |
100 /** |
220 * Formats localised text with specified parameters from an array. |
101 * Formats localised text with specified parameters from an array. |
221 * |
102 * |
224 * @return localised text formatted with the provided parameters |
105 * @return localised text formatted with the provided parameters |
225 * @see Formatter |
106 * @see Formatter |
226 */ |
107 */ |
227 public String format(String id, Object[] textParameters) |
108 public String format(String id, Object[] textParameters) |
228 { |
109 { |
229 return new Formatter(string(id), locType).format(textParameters); |
110 return new Formatter(string(id)).format(textParameters); |
230 } |
111 } |
231 |
|
232 /** |
|
233 * Formats localised text with specified parameters from an array. |
|
234 * |
|
235 * @param id resource id |
|
236 * @param textParameters parameters to be filled into the text |
|
237 * @return localised text formatted with the provided parameters |
|
238 * @see Formatter |
|
239 */ |
|
240 public String format(Id id, Object[] textParameters) |
|
241 { |
|
242 return new Formatter(string(id.getString(locType)), locType).format(textParameters); |
|
243 } |
|
244 |
|
245 |
112 |
246 /** |
113 /** |
247 * Get a plain string resource with a given resource id. |
114 * Get a plain string resource with a given resource id. |
248 * |
115 * |
249 * @param id resource id, either with prefix or without |
116 * @param id resource id, either with prefix or without |
303 } |
170 } |
304 } |
171 } |
305 return "sc"; |
172 return "sc"; |
306 } |
173 } |
307 |
174 |
308 /** |
|
309 * Return locale id string on Qt platform. |
|
310 * |
|
311 * @return Qt Locale Id String, null if not in Qt. |
|
312 */ |
|
313 public static String getLocaleIdQt() |
|
314 { |
|
315 return _getLocaleIdQt(); |
|
316 } |
|
317 |
|
318 |
|
319 /*** ----------------------------- PRIVATE ---------------------------- */ |
175 /*** ----------------------------- PRIVATE ---------------------------- */ |
320 |
176 |
321 /** |
177 /** |
322 * Loads the resources from .loc type file. |
178 * Loads the resources from .loc type file |
323 * |
179 */ |
324 * @param resourceName name of the resource file. |
180 private void loadFile(String resourceName) |
325 * @param aIs InputStream pointing to resource. It will be closed after use. |
|
326 * @param true if operation succeed. |
|
327 */ |
|
328 private boolean loadFile(String resourceName, boolean avkon) |
|
329 { |
181 { |
330 InputStream is = null; |
182 InputStream is = null; |
331 |
183 |
332 if (!avkon) // Qt resources. |
184 // Load with real locale id |
333 { |
185 is = this.getClass().getResourceAsStream( |
334 String langName = getLocaleIdQt(); |
186 LOC_RESOURCE_BASE + resourceName + "_" + getLocaleId() + ".loc"); |
335 |
187 if (is == null) |
336 // Emulator returns falsely en_GB as engineering English locale name. |
188 { |
337 if (langName.equals("en_GB")) |
189 // Load the engineering english |
338 { |
|
339 langName = "en"; |
|
340 } |
|
341 |
|
342 // Load with real locale id |
|
343 is = this.getClass().getResourceAsStream( |
190 is = this.getClass().getResourceAsStream( |
344 LOC_RESOURCE_BASE + resourceName + "_" + langName + ".loc"); |
191 LOC_RESOURCE_BASE + resourceName + "_sc" + ".loc"); |
345 |
192 } |
346 if (is == null) |
193 if (is == null) |
347 { |
194 { |
348 /* |
195 // Load the reference engineering english |
349 * Does not exist. No need to continue as avkon file cannot |
|
350 * found using qt name. |
|
351 */ |
|
352 return false; |
|
353 } |
|
354 } |
|
355 else // Avkon resources. |
|
356 { |
|
357 // Load with real locale id |
|
358 is = this.getClass().getResourceAsStream( |
196 is = this.getClass().getResourceAsStream( |
359 LOC_RESOURCE_BASE + resourceName + "_" + getLocaleId() + ".loc"); |
197 LOC_RESOURCE_BASE + resourceName + ".loc"); |
360 |
198 } |
361 if (is == null) |
199 if (is == null) |
362 { |
200 { |
363 // Load the engineering english |
201 Logger.WLOG(Logger.EUtils, |
364 is = this.getClass().getResourceAsStream( |
202 "Cannot load resource file: " + resourceName); |
365 LOC_RESOURCE_BASE + resourceName + "_sc" + ".loc"); |
203 return; |
366 } |
|
367 if (is == null) |
|
368 { |
|
369 // Load the reference engineering english |
|
370 is = this.getClass().getResourceAsStream( |
|
371 LOC_RESOURCE_BASE + resourceName + ".loc"); |
|
372 } |
|
373 if (is == null) |
|
374 { |
|
375 Logger.WLOG(Logger.EUtils, |
|
376 "Cannot load resource file: " + resourceName); |
|
377 return false; |
|
378 } |
|
379 } |
204 } |
380 |
205 |
381 try |
206 try |
382 { |
207 { |
383 // Loc-files area always on UTF8 format |
208 // Loc-files area always on UTF8 format |
421 { |
246 { |
422 Logger.WLOG(Logger.EUtils, |
247 Logger.WLOG(Logger.EUtils, |
423 "Resource file " + resourceName + " handling failed: " |
248 "Resource file " + resourceName + " handling failed: " |
424 + ex.getMessage()); |
249 + ex.getMessage()); |
425 } |
250 } |
426 |
|
427 return true; |
|
428 } |
251 } |
429 |
252 |
430 /** |
253 /** |
431 * Decode given string. Decoding means unescaping escaped characters. |
254 * Decode given string. Decoding means unescaping escaped characters. |
432 * Currently \n, \t, \', \\ and \" patterns are decoded to respective |
255 * Currently \n, \t, \', \\ and \" patterns are decoded to respective |