| 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 | 3552 | this(DEFAULT_LOCALE); |
| 26 | 3552 | } |
| 27 | |
|
| 28 | |
public LocalizedKeywords(Locale locale) { |
| 29 | 3575 | this(locale, DEFAULT_BUNDLE_NAME, DEFAULT_CLASS_LOADER); |
| 30 | 3574 | } |
| 31 | |
|
| 32 | |
public LocalizedKeywords(Locale locale, String bundleName, ClassLoader classLoader) { |
| 33 | 3576 | super(keywords(bundleName, locale, classLoader)); |
| 34 | 3574 | this.locale = locale; |
| 35 | 3574 | } |
| 36 | |
|
| 37 | |
public Locale getLocale(){ |
| 38 | 29 | return locale; |
| 39 | |
} |
| 40 | |
|
| 41 | |
private static Map<String, String> keywords(String bundleName, Locale locale, |
| 42 | |
ClassLoader classLoader) { |
| 43 | 3576 | ResourceBundle bundle = lookupBunde(bundleName.trim(), locale, classLoader); |
| 44 | 3575 | Map<String, String> keywords = new HashMap<String, String>(); |
| 45 | 3575 | for (String key : KEYWORDS) { |
| 46 | |
try { |
| 47 | 121517 | keywords.put(key, bundle.getString(key)); |
| 48 | 1 | } catch (MissingResourceException e) { |
| 49 | 1 | throw new LocalizedKeywordNotFound(key, bundleName, locale); |
| 50 | 121516 | } |
| 51 | 121516 | } |
| 52 | 3574 | return keywords; |
| 53 | |
} |
| 54 | |
|
| 55 | |
private static ResourceBundle lookupBunde(String bundleName, Locale locale, ClassLoader classLoader) { |
| 56 | |
try { |
| 57 | 3576 | if (classLoader != null) { |
| 58 | 3575 | return getBundle(bundleName, locale, classLoader); |
| 59 | |
} |
| 60 | 1 | return getBundle(bundleName, locale); |
| 61 | 1 | } catch (MissingResourceException e) { |
| 62 | 1 | throw new ResourceBundleNotFound(bundleName, locale, classLoader, e); |
| 63 | |
} |
| 64 | |
} |
| 65 | |
|
| 66 | |
@SuppressWarnings("serial") |
| 67 | |
public static class ResourceBundleNotFound extends RuntimeException { |
| 68 | |
|
| 69 | |
public ResourceBundleNotFound(String bundleName, Locale locale, ClassLoader classLoader, |
| 70 | |
MissingResourceException cause) { |
| 71 | 1 | super("Resource bundle " + bundleName + " not found for locale " + locale + " in classLoader " |
| 72 | |
+ classLoader, cause); |
| 73 | 1 | } |
| 74 | |
|
| 75 | |
} |
| 76 | |
|
| 77 | |
@SuppressWarnings("serial") |
| 78 | |
public static class LocalizedKeywordNotFound extends RuntimeException { |
| 79 | |
|
| 80 | |
public LocalizedKeywordNotFound(String key, String bundleName, Locale locale) { |
| 81 | 1 | super("Keyword " + key + " not found for locale " + locale + " in bundle "+bundleName); |
| 82 | 1 | } |
| 83 | |
|
| 84 | |
} |
| 85 | |
|
| 86 | |
} |