1 | |
package org.jbehave.core.i18n; |
2 | |
|
3 | |
import static java.util.ResourceBundle.getBundle; |
4 | |
|
5 | |
import java.util.HashMap; |
6 | |
import java.util.Locale; |
7 | |
import java.util.Map; |
8 | |
import java.util.MissingResourceException; |
9 | |
import java.util.ResourceBundle; |
10 | |
|
11 | |
import org.jbehave.core.configuration.Keywords; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
public class LocalizedKeywords extends Keywords { |
18 | |
|
19 | 1 | private static final Locale DEFAULT_LOCALE = Locale.ENGLISH; |
20 | |
private static final String DEFAULT_BUNDLE_NAME = "i18n/keywords"; |
21 | 1 | private static final ClassLoader DEFAULT_CLASS_LOADER = LocalizedKeywords.class.getClassLoader(); |
22 | |
private final Locale locale; |
23 | |
|
24 | |
public LocalizedKeywords() { |
25 | 2300 | this(DEFAULT_LOCALE); |
26 | 2300 | } |
27 | |
|
28 | |
public LocalizedKeywords(Locale locale) { |
29 | 2313 | this(locale, DEFAULT_BUNDLE_NAME, DEFAULT_CLASS_LOADER); |
30 | 2312 | } |
31 | |
|
32 | |
public LocalizedKeywords(Locale locale, String bundleName, ClassLoader classLoader) { |
33 | 2314 | super(keywords(bundleName, locale, classLoader)); |
34 | 2312 | this.locale = locale; |
35 | 2312 | } |
36 | |
|
37 | |
public Locale getLocale(){ |
38 | 22 | return locale; |
39 | |
} |
40 | |
|
41 | |
private static Map<String, String> keywords(String bundleName, Locale locale, |
42 | |
ClassLoader classLoader) { |
43 | 2314 | ResourceBundle bundle = lookupBunde(bundleName.trim(), locale, classLoader); |
44 | 2313 | Map<String, String> keywords = new HashMap<String, String>(); |
45 | 2313 | for (String key : KEYWORDS) { |
46 | 50865 | keywords.put(key, keyword(key, bundle)); |
47 | |
} |
48 | 2312 | return keywords; |
49 | |
} |
50 | |
|
51 | |
private static String keyword(String name, ResourceBundle bundle) { |
52 | |
try { |
53 | 50865 | return bundle.getString(name); |
54 | 1 | } catch (MissingResourceException e) { |
55 | 1 | throw new LocalizedKeywordNotFound(name, bundle); |
56 | |
} |
57 | |
} |
58 | |
|
59 | |
private static ResourceBundle lookupBunde(String bundleName, Locale locale, ClassLoader classLoader) { |
60 | |
try { |
61 | 2314 | if (classLoader != null) { |
62 | 2313 | return getBundle(bundleName, locale, classLoader); |
63 | |
} |
64 | 1 | return getBundle(bundleName, locale); |
65 | 1 | } catch (MissingResourceException e) { |
66 | 1 | throw new ResourceBundleNotFound(bundleName, locale, classLoader, e); |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
@SuppressWarnings("serial") |
71 | |
public static class ResourceBundleNotFound extends RuntimeException { |
72 | |
|
73 | |
public ResourceBundleNotFound(String bundleName, Locale locale, ClassLoader classLoader, |
74 | |
MissingResourceException cause) { |
75 | 1 | super("Resource bundle " + bundleName + " not found for locale " + locale + " in classLoader " |
76 | |
+ classLoader, cause); |
77 | 1 | } |
78 | |
|
79 | |
} |
80 | |
|
81 | |
@SuppressWarnings("serial") |
82 | |
public static class LocalizedKeywordNotFound extends RuntimeException { |
83 | |
|
84 | |
public LocalizedKeywordNotFound(String name, ResourceBundle bundle) { |
85 | 1 | super("Keyword" + name + " not found in resource bundle " + bundle); |
86 | 1 | } |
87 | |
|
88 | |
} |
89 | |
|
90 | |
} |