1 package be
.nikiroo
.jvcard
.resources
.bundles
;
4 import java
.io
.IOException
;
6 import java
.util
.LinkedList
;
8 import java
.util
.Locale
;
9 import java
.util
.regex
.Pattern
;
11 import be
.nikiroo
.jvcard
.resources
.Bundles
;
12 import be
.nikiroo
.jvcard
.resources
.Bundles
.Bundle
;
13 import be
.nikiroo
.jvcard
.resources
.Bundles
.Target
;
14 import be
.nikiroo
.jvcard
.resources
.ResourceList
;
15 import be
.nikiroo
.jvcard
.resources
.enums
.StringId
;
18 * This class manages the translation of {@link TransBundle.StringId}s into
19 * user-understandable text.
24 public class TransBundle
extends Bundle
<StringId
> {
25 private boolean utf
= true;
26 private Locale locale
;
27 private boolean defaultLocale
= false;
30 * Create a translation service with the default language.
32 public TransBundle() {
33 new Bundles().super(StringId
.class, Target
.resources
);
38 * Create a translation service for the given language. (Will fall back to
39 * the default one i not found.)
44 public TransBundle(String language
) {
45 new Bundles().super(StringId
.class, Target
.resources
);
47 setLanguage(language
);
51 * Translate the given {@link StringId} into user text.
56 * the values to insert instead of the place holders in the
59 * @return the translated text with the given value where required
61 public String
getString(StringId stringId
, Object
... values
) {
62 StringId id
= stringId
;
67 id
= StringId
.valueOf(stringId
.name() + "_NOUTF");
68 } catch (IllegalArgumentException iae
) {
69 // no special _NOUTF version found
73 if (id
== StringId
.NULL
) {
75 } else if (id
== StringId
.DUMMY
) {
77 } else if (map
.containsKey(id
.name())) {
78 result
= map
.getString(id
.name());
80 result
= id
.toString();
83 if (values
!= null && values
.length
> 0)
84 return String
.format(locale
, result
, values
);
90 * Check if unicode characters should be used.
92 * @return TRUE to allow unicode
94 public boolean isUnicode() {
99 * Allow or disallow unicode characters in the program.
102 * TRUE to allow unuciode, FALSE to only allow ASCII characters
104 public void setUnicode(boolean utf
) {
109 * Initialise the translation mappings for the given language.
112 * the language to initialise, in the form "en-GB" or "fr" for
115 private void setLanguage(String language
) {
116 defaultLocale
= (language
== null || language
.length() == 0);
117 locale
= getLocaleFor(language
);
118 map
= getBundle(Target
.resources
, locale
);
122 public String
getString(StringId id
) {
123 return getString(id
, (Object
[]) null);
127 protected File
getUpdateFile(String path
) {
128 String code
= locale
.toString();
130 if (!defaultLocale
&& code
.length() > 0) {
131 file
= new File(path
, name
.name() + "_" + code
+ ".properties");
133 // Default properties file:
134 file
= new File(path
, name
.name() + ".properties");
141 protected void writeHeader(Writer writer
) throws IOException
{
142 String code
= locale
.toString();
143 String name
= locale
.getDisplayCountry(locale
);
145 if (name
.length() == 0)
146 name
= locale
.getDisplayLanguage(locale
);
147 if (name
.length() == 0)
150 if (code
.length() > 0) {
151 name
= name
+ " (" + code
+ ")";
154 StringId
.writeHeader(writer
, name
);
158 protected void writeValue(Writer writer
, StringId id
) throws IOException
{
159 super.writeValue(writer
, id
);
161 String name
= id
.name() + "_NOUTF";
162 if (map
.containsKey(name
)) {
163 String value
= map
.getString(name
).trim();
164 writeValue(writer
, name
, value
);
169 * Return the {@link Locale} representing the given language.
172 * the language to initialise, in the form "en-GB" or "fr" for
175 * @return the corresponding {@link Locale} or the default {@link Locale} if
178 static private Locale
getLocaleFor(String language
) {
181 if (language
== null) {
182 locale
= Locale
.getDefault();
184 language
= language
.replaceAll("_", "-");
185 String lang
= language
;
186 String country
= null;
187 if (language
.contains("-")) {
188 lang
= language
.split("-")[0];
189 country
= language
.split("-")[1];
193 locale
= new Locale(lang
, country
);
195 locale
= new Locale(lang
);
202 * Return all the languages known by the program.
204 * @return the known language codes
206 static public List
<String
> getKnownLanguages() {
207 List
<String
> resources
= new LinkedList
<String
>();
209 String regex
= ".*" + Target
.resources
.name()
210 + "[_a-zA-Za]*\\.properties$";
212 for (String res
: ResourceList
.getResources(Pattern
.compile(regex
))) {
213 String resource
= res
;
214 int index
= resource
.lastIndexOf('/');
215 if (index
>= 0 && index
< (resource
.length() - 1))
216 resource
= resource
.substring(index
+ 1);
217 if (resource
.startsWith(Target
.resources
.name())) {
218 resource
= resource
.substring(0, resource
.length()
219 - ".properties".length());
220 resource
= resource
.substring(Target
.resources
.name().length());
221 if (resource
.startsWith("_")) {
222 resource
= resource
.substring(1);
223 resources
.add(resource
);