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 public String
getString(StringId id
) {
47 return getString(id
, (Object
[]) null);
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
, (Object
[]) 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 protected File
getUpdateFile(String path
) {
110 String code
= locale
.toString();
112 if (code
.length() > 0) {
113 file
= new File(path
, name
.name() + "_" + code
+ ".properties");
115 // Default properties file:
116 file
= new File(path
, name
.name() + ".properties");
123 protected void writeHeader(Writer writer
) throws IOException
{
124 String code
= locale
.toString();
125 String name
= locale
.getDisplayCountry(locale
);
127 if (name
.length() == 0)
128 name
= locale
.getDisplayLanguage(locale
);
129 if (name
.length() == 0)
132 if (code
.length() > 0) {
133 name
= name
+ " (" + code
+ ")";
136 StringId
.writeHeader(writer
, name
);
140 * Initialise the translation mappings for the given language.
143 * the language to initialise, in the form "en-GB" or "fr" for
146 private void setLanguage(String language
) {
147 locale
= getLocaleFor(language
);
148 map
= getBundle(Target
.resources
, locale
);
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
);