Commit | Line | Data |
---|---|---|
e119a1c1 NR |
1 | package be.nikiroo.jvcard.resources.bundles; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.io.Writer; | |
6 | import java.util.Locale; | |
7 | ||
8 | import be.nikiroo.jvcard.resources.Bundles; | |
9 | import be.nikiroo.jvcard.resources.Bundles.Bundle; | |
10 | import be.nikiroo.jvcard.resources.Bundles.Target; | |
11 | import be.nikiroo.jvcard.resources.enums.StringId; | |
12 | ||
13 | /** | |
14 | * This class manages the translation of {@link TransBundle.StringId}s into | |
15 | * user-understandable text. | |
16 | * | |
17 | * @author niki | |
18 | * | |
19 | */ | |
20 | public class TransBundle extends Bundle<StringId> { | |
21 | private boolean utf = true; | |
22 | private Locale locale; | |
23 | ||
24 | /** | |
25 | * Create a translation service with the default language. | |
26 | */ | |
27 | public TransBundle() { | |
28 | new Bundles().super(StringId.class, Target.resources); | |
29 | setLanguage(null); | |
30 | } | |
31 | ||
32 | /** | |
33 | * Create a translation service for the given language. (Will fall back to | |
34 | * the default one i not found.) | |
35 | * | |
36 | * @param language | |
37 | * the language to use | |
38 | */ | |
39 | public TransBundle(String language) { | |
40 | new Bundles().super(StringId.class, Target.resources); | |
41 | ||
42 | setLanguage(language); | |
43 | } | |
44 | ||
e119a1c1 NR |
45 | /** |
46 | * Translate the given {@link StringId} into user text. | |
47 | * | |
48 | * @param stringId | |
49 | * the ID to translate | |
50 | * @param values | |
51 | * the values to insert instead of the place holders in the | |
52 | * translation | |
53 | * | |
54 | * @return the translated text with the given value where required | |
55 | */ | |
56 | public String getString(StringId stringId, Object... values) { | |
57 | StringId id = stringId; | |
58 | String result = ""; | |
59 | ||
60 | if (!isUnicode()) { | |
61 | try { | |
62 | id = StringId.valueOf(stringId.name() + "_NOUTF"); | |
63 | } catch (IllegalArgumentException iae) { | |
64 | // no special _NOUTF version found | |
65 | } | |
66 | } | |
67 | ||
68 | if (id == StringId.NULL) { | |
69 | result = ""; | |
70 | } else if (id == StringId.DUMMY) { | |
71 | result = "[dummy]"; | |
72 | } else if (map.containsKey(id.name())) { | |
73 | result = map.getString(id.name()); | |
74 | } else { | |
75 | result = id.toString(); | |
76 | } | |
77 | ||
78 | if (values != null && values.length > 0) | |
79 | return String.format(locale, result, (Object[]) values); | |
80 | else | |
81 | return result; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Check if unicode characters should be used. | |
86 | * | |
87 | * @return TRUE to allow unicode | |
88 | */ | |
89 | public boolean isUnicode() { | |
90 | return utf; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Allow or disallow unicode characters in the program. | |
95 | * | |
96 | * @param utf | |
97 | * TRUE to allow unuciode, FALSE to only allow ASCII characters | |
98 | */ | |
99 | public void setUnicode(boolean utf) { | |
100 | this.utf = utf; | |
101 | } | |
102 | ||
88eb8122 NR |
103 | /** |
104 | * Initialise the translation mappings for the given language. | |
105 | * | |
106 | * @param language | |
107 | * the language to initialise, in the form "en-GB" or "fr" for | |
108 | * instance | |
109 | */ | |
110 | private void setLanguage(String language) { | |
111 | locale = getLocaleFor(language); | |
112 | map = getBundle(Target.resources, locale); | |
113 | } | |
114 | ||
115 | @Override | |
116 | public String getString(StringId id) { | |
117 | return getString(id, (Object[]) null); | |
118 | } | |
119 | ||
e119a1c1 NR |
120 | @Override |
121 | protected File getUpdateFile(String path) { | |
122 | String code = locale.toString(); | |
123 | File file = null; | |
124 | if (code.length() > 0) { | |
125 | file = new File(path, name.name() + "_" + code + ".properties"); | |
126 | } else { | |
127 | // Default properties file: | |
128 | file = new File(path, name.name() + ".properties"); | |
129 | } | |
130 | ||
131 | return file; | |
132 | } | |
133 | ||
134 | @Override | |
135 | protected void writeHeader(Writer writer) throws IOException { | |
136 | String code = locale.toString(); | |
137 | String name = locale.getDisplayCountry(locale); | |
138 | ||
139 | if (name.length() == 0) | |
140 | name = locale.getDisplayLanguage(locale); | |
141 | if (name.length() == 0) | |
142 | name = "default"; | |
143 | ||
144 | if (code.length() > 0) { | |
145 | name = name + " (" + code + ")"; | |
146 | } | |
147 | ||
148 | StringId.writeHeader(writer, name); | |
149 | } | |
150 | ||
e119a1c1 NR |
151 | /** |
152 | * Return the {@link Locale} representing the given language. | |
153 | * | |
154 | * @param language | |
155 | * the language to initialise, in the form "en-GB" or "fr" for | |
156 | * instance | |
157 | * | |
158 | * @return the corresponding {@link Locale} or the default {@link Locale} if | |
159 | * it is not known | |
160 | */ | |
161 | static private Locale getLocaleFor(String language) { | |
162 | Locale locale; | |
163 | ||
164 | if (language == null) { | |
165 | locale = Locale.getDefault(); | |
166 | } else { | |
167 | language = language.replaceAll("_", "-"); | |
168 | String lang = language; | |
169 | String country = null; | |
170 | if (language.contains("-")) { | |
171 | lang = language.split("-")[0]; | |
172 | country = language.split("-")[1]; | |
173 | } | |
174 | ||
175 | if (country != null) | |
176 | locale = new Locale(lang, country); | |
177 | else | |
178 | locale = new Locale(lang); | |
179 | } | |
180 | ||
181 | return locale; | |
182 | } | |
88eb8122 | 183 | |
e119a1c1 | 184 | } |