24a9252d17a3fefafb2f430d69ad0d50e61e412a
[fanfix.git] / src / be / nikiroo / fanfix / bundles / StringIdBundle.java
1 package be.nikiroo.fanfix.bundles;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Locale;
6
7 import be.nikiroo.utils.resources.TransBundle;
8
9 /**
10 * This class manages the translation resources of the application.
11 *
12 * @author niki
13 */
14 public class StringIdBundle extends TransBundle<StringId> {
15 private String lang;
16
17 /**
18 * Create a translation service for the given language (will fall back to
19 * the default one i not found).
20 *
21 * @param lang
22 * the language to use
23 */
24 public StringIdBundle(String lang) {
25 super(StringId.class, Target.resources, lang);
26 this.lang = lang;
27 }
28
29 /**
30 * Return the currently used language as a String.
31 *
32 * @return the language
33 *
34 * @deprecated use the call from {@link TransBundle} when available
35 */
36 public Locale getLanguage() {
37 return getLocaleFor(lang);
38
39 }
40
41 /**
42 * Update resource file.
43 *
44 * @param args
45 * not used
46 *
47 * @throws IOException
48 * in case of I/O error
49 */
50 public static void main(String[] args) throws IOException {
51 String path = new File(".").getAbsolutePath()
52 + "/src/be/nikiroo/fanfix/bundles/";
53 new StringIdBundle(null).updateFile(path);
54 System.out.println("Path updated: " + path);
55 }
56
57 /**
58 * Return the {@link Locale} representing the given language.
59 *
60 * @param language
61 * the language to initialise, in the form "en-GB" or "fr" for
62 * instance
63 *
64 * @return the corresponding {@link Locale} or the default {@link Locale} if
65 * it is not known
66 *
67 * @deprecated Use the call from {@link TransBundle} when available.
68 */
69 static private Locale getLocaleFor(String language) {
70 Locale locale;
71
72 if (language == null) {
73 locale = Locale.getDefault();
74 } else {
75 language = language.replaceAll("_", "-");
76 String lang = language;
77 String country = null;
78 if (language.contains("-")) {
79 lang = language.split("-")[0];
80 country = language.split("-")[1];
81 }
82
83 if (country != null)
84 locale = new Locale(lang, country);
85 else
86 locale = new Locale(lang);
87 }
88
89 return locale;
90 }
91 }