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