1
package com.nantic.jasperreports;
3
import java.io.FileInputStream;
4
import java.util.PropertyResourceBundle;
5
import java.util.Hashtable;
7
import java.util.Locale;
8
import java.util.ResourceBundle;
9
import java.util.Enumeration;
11
import org.xnap.commons.i18n.I18n;
14
static Hashtable<Locale, I18n> resources = new Hashtable<Locale, I18n>();
15
static String baseName = null;
16
static Locale defaultLocale = null;
17
static Hashtable<Locale, Boolean> unavailableResources = new Hashtable<Locale, Boolean>();
19
public static void init(String baseName, Locale defaultLocale) {
20
i18n.baseName = baseName;
21
i18n.defaultLocale = defaultLocale;
23
/* Ensures the given locale is loaded */
24
protected static boolean loadLocale( Locale locale ) {
25
// If the resource wasn't available don't try to load it each time.
26
if ( baseName == null || locale == null )
28
if ( unavailableResources.containsKey( locale ) )
30
if ( ! resources.containsKey( locale ) ) {
31
String fileName = baseName + "_" + locale.toString() + ".properties";
32
ResourceBundle bundle;
34
FileInputStream fis = new FileInputStream( fileName );
35
bundle = new PropertyResourceBundle(fis);
36
resources.put( locale, new I18n( bundle ) );
37
} catch (Exception e) {
39
unavailableResources.put( locale, true );
40
System.out.println( "No bundle file named: " + fileName );
46
public static Locale stringToLocale(String localeCode) {
48
String[] locales = localeCode.split( "_" );
49
if ( locales.length == 1 )
50
locale = new Locale( locales[0] );
51
else if ( locales.length == 2 )
52
locale = new Locale( locales[0], locales[1] );
54
locale = new Locale( locales[0], locales[1], locales[2] );
57
/* tr(Locale..) and tr(Locale..Object) functions */
58
public static String tr(Locale locale, String text) {
59
if ( ! loadLocale( locale ) )
61
return resources.get( locale ).tr( text );
63
public static String tr(Locale locale, String text, Object o) {
64
if ( ! loadLocale( locale ) )
66
return resources.get( locale ).tr( text, o );
68
public static String tr(Locale locale, String text, Object o1, Object o2) {
69
if ( ! loadLocale( locale ) )
71
return resources.get( locale ).tr( text, o1, o2 );
73
public static String tr(Locale locale, String text, Object o1, Object o2, Object o3) {
74
if ( ! loadLocale( locale ) )
76
return resources.get( locale ).tr( text, o1, o2, o3 );
78
public static String tr(Locale locale, String text, Object o1, Object o2, Object o3, Object o4) {
79
if ( ! loadLocale( locale ) )
81
return resources.get( locale ).tr( text, o1, o2, o3, o4 );
83
public static String tr(Locale locale, String text, Object[] objects) {
84
if ( ! loadLocale( locale ) )
86
return resources.get( locale ).tr( text, objects );
88
/* trl() and trl(..Object) functions */
89
public static String trl(String localeCode, String text) {
90
return tr(stringToLocale(localeCode), text);
92
public static String trl(String localeCode, String text, Object o) {
93
return tr(stringToLocale(localeCode), text, o);
95
public static String trl(String localeCode, String text, Object o1, Object o2) {
96
return tr(stringToLocale(localeCode), text, o1, o2);
98
public static String trl(String localeCode, String text, Object o1, Object o2, Object o3) {
99
return tr(stringToLocale(localeCode), text, o1, o2, o3);
101
public static String trl(String localeCode, String text, Object o1, Object o2, Object o3, Object o4) {
102
return tr(stringToLocale(localeCode), text, o1, o2, o3, o4);
104
public static String trl(String localeCode, String text, Object[] objects) {
105
return tr(stringToLocale(localeCode), text, objects);
107
/* tr(..) and tr(..Object) functions */
108
public static String tr(String text) {
109
return tr(defaultLocale, text);
111
public static String tr(String text, Object o) {
112
return tr(defaultLocale, text, o);
114
public static String tr(String text, Object o1, Object o2) {
115
return tr(defaultLocale, text, o1, o2);
117
public static String tr(String text, Object o1, Object o2, Object o3) {
118
return tr(defaultLocale, text, o1, o2, o3);
120
public static String tr(String text, Object o1, Object o2, Object o3, Object o4) {
121
return tr(defaultLocale, text, o1, o2, o3, o4);
123
public static String tr(String text, Object[] objects) {
124
return tr(defaultLocale, text, objects);
126
/* trn(Locale..) and trn(Locale..Object) functions */
127
public static String trn(Locale locale, String text, String pluralText, long n) {
128
if ( ! loadLocale( locale ) )
130
return resources.get( locale ).trn( text, pluralText, n );
132
public static String trn(Locale locale, String text, String pluralText, long n, Object o) {
133
if ( ! loadLocale( locale ) )
135
return resources.get( locale ).trn( text, pluralText, n, o );
137
public static String trn(Locale locale, String text, String pluralText, long n, Object o1, Object o2) {
138
if ( ! loadLocale( locale ) )
140
return resources.get( locale ).trn( text, pluralText, n, o1, o2 );
142
public static String trn(Locale locale, String text, String pluralText, long n, Object o1, Object o2, Object o3) {
143
if ( ! loadLocale( locale ) )
145
return resources.get( locale ).trn( text, pluralText, n, o1, o2, o3 );
147
public static String trn(Locale locale, String text, String pluralText, long n, Object o1, Object o2, Object o3, Object o4) {
148
if ( ! loadLocale( locale ) )
150
return resources.get( locale ).trn( text, pluralText, n, o1, o2, o3, o4 );
152
public static String trn(Locale locale, String text, String pluralText, long n, Object[] objects) {
153
if ( ! loadLocale( locale ) )
155
return resources.get( locale ).trn( text, pluralText, n, objects );
157
/* trn(..) and trn(..Object) functions */
158
public static String trn(String text, String pluralText, long n) {
159
return trn(defaultLocale, text, pluralText, n);
161
public static String trn(String text, String pluralText, long n, Object o) {
162
return trn(defaultLocale, text, pluralText, n, o);
164
public static String trn(String text, String pluralText, long n, Object o1, Object o2) {
165
return trn(defaultLocale, text, pluralText, n, o1, o2);
167
public static String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3) {
168
return trn(defaultLocale, text, pluralText, n, o1, o2, o3);
170
public static String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3, Object o4) {
171
return trn(defaultLocale, text, pluralText, n, o1, o2, o3, o4);
173
public static String trn(String text, String pluralText, long n, Object[] objects) {
174
return trn(defaultLocale, text, pluralText, n, objects);