1 package be
.nikiroo
.jvcard
.resources
.bundles
;
4 import java
.io
.IOException
;
6 import java
.util
.Locale
;
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
;
14 * This class manages the translation of {@link TransBundle.StringId}s into
15 * user-understandable text.
20 public class TransBundle
extends Bundle
<StringId
> {
21 private boolean utf
= true;
22 private Locale locale
;
25 * Create a translation service with the default language.
27 public TransBundle() {
28 new Bundles().super(StringId
.class, Target
.resources
);
33 * Create a translation service for the given language. (Will fall back to
34 * the default one i not found.)
39 public TransBundle(String language
) {
40 new Bundles().super(StringId
.class, Target
.resources
);
42 setLanguage(language
);
46 * Translate the given {@link StringId} into user text.
51 * the values to insert instead of the place holders in the
54 * @return the translated text with the given value where required
56 public String
getString(StringId stringId
, Object
... values
) {
57 StringId id
= stringId
;
62 id
= StringId
.valueOf(stringId
.name() + "_NOUTF");
63 } catch (IllegalArgumentException iae
) {
64 // no special _NOUTF version found
68 if (id
== StringId
.NULL
) {
70 } else if (id
== StringId
.DUMMY
) {
72 } else if (map
.containsKey(id
.name())) {
73 result
= map
.getString(id
.name());
75 result
= id
.toString();
78 if (values
!= null && values
.length
> 0)
79 return String
.format(locale
, result
, (Object
[]) values
);
85 * Check if unicode characters should be used.
87 * @return TRUE to allow unicode
89 public boolean isUnicode() {
94 * Allow or disallow unicode characters in the program.
97 * TRUE to allow unuciode, FALSE to only allow ASCII characters
99 public void setUnicode(boolean utf
) {
104 * Initialise the translation mappings for the given language.
107 * the language to initialise, in the form "en-GB" or "fr" for
110 private void setLanguage(String language
) {
111 locale
= getLocaleFor(language
);
112 map
= getBundle(Target
.resources
, locale
);
116 public String
getString(StringId id
) {
117 return getString(id
, (Object
[]) null);
121 protected File
getUpdateFile(String path
) {
122 String code
= locale
.toString();
124 if (code
.length() > 0) {
125 file
= new File(path
, name
.name() + "_" + code
+ ".properties");
127 // Default properties file:
128 file
= new File(path
, name
.name() + ".properties");
135 protected void writeHeader(Writer writer
) throws IOException
{
136 String code
= locale
.toString();
137 String name
= locale
.getDisplayCountry(locale
);
139 if (name
.length() == 0)
140 name
= locale
.getDisplayLanguage(locale
);
141 if (name
.length() == 0)
144 if (code
.length() > 0) {
145 name
= name
+ " (" + code
+ ")";
148 StringId
.writeHeader(writer
, name
);
152 * Return the {@link Locale} representing the given language.
155 * the language to initialise, in the form "en-GB" or "fr" for
158 * @return the corresponding {@link Locale} or the default {@link Locale} if
161 static private Locale
getLocaleFor(String language
) {
164 if (language
== null) {
165 locale
= Locale
.getDefault();
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];
176 locale
= new Locale(lang
, country
);
178 locale
= new Locale(lang
);