~openbias/bias-trunk/addons_minerales

« back to all changes in this revision

Viewing changes to jasper_reports/java/com/nantic/jasperreports/i18n.java

  • Committer: root
  • Date: 2012-02-17 17:29:00 UTC
  • mfrom: (564.1.1 addons_mx)
  • Revision ID: root@minerales-20120217172900-o0tqaj0bnt95lk3w
 cambios en jasper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.nantic.jasperreports;
 
2
 
 
3
import java.io.FileInputStream;
 
4
import java.util.PropertyResourceBundle;
 
5
import java.util.Hashtable;
 
6
import java.util.Map;
 
7
import java.util.Locale;
 
8
import java.util.ResourceBundle;
 
9
import java.util.Enumeration;
 
10
 
 
11
import org.xnap.commons.i18n.I18n;
 
12
 
 
13
public class 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>();
 
18
 
 
19
        public static void init(String baseName, Locale defaultLocale) {
 
20
                i18n.baseName = baseName;
 
21
                i18n.defaultLocale = defaultLocale;
 
22
        }
 
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 )
 
27
                        return false;
 
28
                if ( unavailableResources.containsKey( locale ) )
 
29
                        return false;
 
30
                if ( ! resources.containsKey( locale ) ) {
 
31
                        String fileName = baseName + "_" + locale.toString() + ".properties";
 
32
                        ResourceBundle bundle; 
 
33
                        try {
 
34
                                FileInputStream fis = new FileInputStream( fileName );
 
35
                                bundle = new PropertyResourceBundle(fis);
 
36
                                resources.put( locale, new I18n( bundle ) );
 
37
                        } catch (Exception e) {
 
38
                                e.printStackTrace();
 
39
                                unavailableResources.put( locale, true );
 
40
                                System.out.println( "No bundle file named: " + fileName );
 
41
                                return false;
 
42
                        }
 
43
                }
 
44
                return true;
 
45
        }
 
46
        public static Locale stringToLocale(String localeCode) {
 
47
                Locale locale;
 
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] );
 
53
                else
 
54
                        locale = new Locale( locales[0], locales[1], locales[2] );
 
55
                return locale;
 
56
        }
 
57
        /* tr(Locale..) and tr(Locale..Object) functions */
 
58
        public static String tr(Locale locale, String text) {
 
59
                if ( ! loadLocale( locale ) )
 
60
                        return text;
 
61
                return resources.get( locale ).tr( text );
 
62
        }
 
63
        public static String tr(Locale locale, String text, Object o) {
 
64
                if ( ! loadLocale( locale ) )
 
65
                        return text;
 
66
                return resources.get( locale ).tr( text, o );
 
67
        }
 
68
        public static String tr(Locale locale, String text, Object o1, Object o2) {
 
69
                if ( ! loadLocale( locale ) )
 
70
                        return text;
 
71
                return resources.get( locale ).tr( text, o1, o2 );
 
72
        }
 
73
        public static String tr(Locale locale, String text, Object o1, Object o2, Object o3) {
 
74
                if ( ! loadLocale( locale ) )
 
75
                        return text;
 
76
                return resources.get( locale ).tr( text, o1, o2, o3 );
 
77
        }
 
78
        public static String tr(Locale locale, String text, Object o1, Object o2, Object o3, Object o4) {
 
79
                if ( ! loadLocale( locale ) )
 
80
                        return text;
 
81
                return resources.get( locale ).tr( text, o1, o2, o3, o4 );
 
82
        }
 
83
        public static String tr(Locale locale, String text, Object[] objects) {
 
84
                if ( ! loadLocale( locale ) )
 
85
                        return text;
 
86
                return resources.get( locale ).tr( text, objects );
 
87
        }
 
88
        /* trl() and trl(..Object) functions */
 
89
        public static String trl(String localeCode, String text) {
 
90
                return tr(stringToLocale(localeCode), text);
 
91
        }
 
92
        public static String trl(String localeCode, String text, Object o) {
 
93
                return tr(stringToLocale(localeCode), text, o);
 
94
        }
 
95
        public static String trl(String localeCode, String text, Object o1, Object o2) {
 
96
                return tr(stringToLocale(localeCode), text, o1, o2);
 
97
        }
 
98
        public static String trl(String localeCode, String text, Object o1, Object o2, Object o3) {
 
99
                return tr(stringToLocale(localeCode), text, o1, o2, o3);
 
100
        }
 
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);
 
103
        }
 
104
        public static String trl(String localeCode, String text, Object[] objects) {
 
105
                return tr(stringToLocale(localeCode), text, objects);
 
106
        }
 
107
        /* tr(..) and tr(..Object) functions */
 
108
        public static String tr(String text) {
 
109
                return tr(defaultLocale, text);
 
110
        }
 
111
        public static String tr(String text, Object o) {
 
112
                return tr(defaultLocale, text, o);
 
113
        }
 
114
        public static String tr(String text, Object o1, Object o2) {
 
115
                return tr(defaultLocale, text, o1, o2);
 
116
        }
 
117
        public static String tr(String text, Object o1, Object o2, Object o3) {
 
118
                return tr(defaultLocale, text, o1, o2, o3);
 
119
        }
 
120
        public static String tr(String text, Object o1, Object o2, Object o3, Object o4) {
 
121
                return tr(defaultLocale, text, o1, o2, o3, o4);
 
122
        }
 
123
        public static String tr(String text, Object[] objects) {
 
124
                return tr(defaultLocale, text, objects);
 
125
        }
 
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 ) )
 
129
                        return text;
 
130
                return resources.get( locale ).trn( text, pluralText, n );
 
131
        }
 
132
        public static String trn(Locale locale, String text, String pluralText, long n, Object o) {
 
133
                if ( ! loadLocale( locale ) )
 
134
                        return text;
 
135
                return resources.get( locale ).trn( text, pluralText, n, o );
 
136
        }
 
137
        public static String trn(Locale locale, String text, String pluralText, long n, Object o1, Object o2) {
 
138
                if ( ! loadLocale( locale ) )
 
139
                        return text;
 
140
                return resources.get( locale ).trn( text, pluralText, n, o1, o2 );
 
141
        }
 
142
        public static String trn(Locale locale, String text, String pluralText, long n, Object o1, Object o2, Object o3) {
 
143
                if ( ! loadLocale( locale ) )
 
144
                        return text;
 
145
                return resources.get( locale ).trn( text, pluralText, n, o1, o2, o3 );
 
146
        }
 
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 ) )
 
149
                        return text;
 
150
                return resources.get( locale ).trn( text, pluralText, n, o1, o2, o3, o4 );
 
151
        }
 
152
        public static String trn(Locale locale, String text, String pluralText, long n, Object[] objects) {
 
153
                if ( ! loadLocale( locale ) )
 
154
                        return text;
 
155
                return resources.get( locale ).trn( text, pluralText, n, objects );
 
156
        }
 
157
        /* trn(..) and trn(..Object) functions */
 
158
        public static String trn(String text, String pluralText, long n) {
 
159
                return trn(defaultLocale, text, pluralText, n);
 
160
        }
 
161
        public static String trn(String text, String pluralText, long n, Object o) {
 
162
                return trn(defaultLocale, text, pluralText, n, o);
 
163
        }
 
164
        public static String trn(String text, String pluralText, long n, Object o1, Object o2) {
 
165
                return trn(defaultLocale, text, pluralText, n, o1, o2);
 
166
        }
 
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);
 
169
        }
 
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);
 
172
        }
 
173
        public static String trn(String text, String pluralText, long n, Object[] objects) {
 
174
                return trn(defaultLocale, text, pluralText, n, objects);
 
175
        }
 
176
}
 
177