Commit | Line | Data |
---|---|---|
2a96e7b2 NR |
1 | package be.nikiroo.jvcard.resources; |
2 | ||
3 | import java.util.Locale; | |
4 | import java.util.ResourceBundle; | |
5 | ||
6 | /** | |
7 | * This class help you get UTF-8 bundles for this application. | |
8 | * | |
9 | * @author niki | |
10 | * | |
11 | */ | |
12 | public class Bundles { | |
7f82bf68 NR |
13 | static private String confDir = getConfDir(); |
14 | ||
1c03abaf NR |
15 | // TODO: rename to bundle, use it as base for i18n.Trans, create one for |
16 | // each resource | |
17 | private int TODO; | |
18 | ||
2a96e7b2 NR |
19 | /** |
20 | * Return the non-localised bundle of the given name. | |
21 | * | |
22 | * @param name | |
23 | * the name of the bundle to load | |
24 | * | |
25 | * @return the bundle | |
26 | */ | |
27 | static public ResourceBundle getBundle(String name) { | |
28 | return ResourceBundle.getBundle(Bundles.class.getPackage().getName() | |
7f82bf68 | 29 | + "." + name, new FixedResourceBundleControl(confDir)); |
2a96e7b2 NR |
30 | } |
31 | ||
32 | /** | |
33 | * Return the localised bundle of the given name and {@link Locale}. | |
34 | * | |
35 | * @param name | |
36 | * the name of the bundle to load | |
37 | * @param locale | |
38 | * the {@link Locale} to use | |
39 | * | |
40 | * @return the localised bundle | |
41 | */ | |
42 | static public ResourceBundle getBundle(String name, Locale locale) { | |
2a96e7b2 | 43 | return ResourceBundle.getBundle(Bundles.class.getPackage().getName() |
7f82bf68 | 44 | + "." + name, locale, new FixedResourceBundleControl(confDir)); |
2a96e7b2 | 45 | } |
7f82bf68 NR |
46 | |
47 | /** | |
48 | * Set the primary configuration directory to look for <tt>.properties</tt> | |
49 | * files in. | |
50 | * | |
51 | * All {@link ResourceBundle}s returned by this class after that point will | |
52 | * respect this new directory. | |
53 | * | |
54 | * @param confDir | |
55 | * the new directory | |
56 | */ | |
57 | static public void setDirectory(String confDir) { | |
58 | Bundles.confDir = confDir; | |
59 | } | |
60 | ||
61 | /** | |
62 | * Return the configuration directory where to try to find the | |
63 | * <tt>.properties</tt> files in priority. | |
64 | * | |
65 | * @return the configuration directory | |
66 | */ | |
67 | static private String getConfDir() { | |
68 | // Do not override user-supplied config directory (see --help) | |
69 | if (Bundles.confDir != null) | |
70 | return Bundles.confDir; | |
71 | ||
72 | try { | |
73 | ResourceBundle bundle = ResourceBundle.getBundle(Bundles.class | |
74 | .getPackage().getName() + "." + "jvcard", | |
75 | Locale.getDefault(), new FixedResourceBundleControl(null)); | |
76 | ||
77 | String configDir = bundle.getString("CONFIG_DIR"); | |
78 | if (configDir != null && configDir.trim().length() > 0) | |
79 | return configDir; | |
80 | } catch (Exception e) { | |
81 | } | |
82 | ||
83 | return null; | |
84 | } | |
85 | ||
2a96e7b2 | 86 | } |