KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > LocaleUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import java.util.Locale JavaDoc;
6 import java.util.ResourceBundle JavaDoc;
7
8 public class LocaleUtil {
9
10     private static Locale JavaDoc lastDefaultLocale;
11     private static ResourceBundle JavaDoc localeElements; // cached value
12

13     public static final String JavaDoc LOCALE_ELEMENTS_BUNDLE_NAME = "sun.text.resources.LocaleElements";
14
15
16     private static void checkBundles() {
17         Locale JavaDoc locale = Locale.getDefault();
18         if (locale != lastDefaultLocale) { // if default locale has been change, reload all cached resource bundles
19
lastDefaultLocale = locale;
20             localeElements = ResourceBundle.getBundle(LOCALE_ELEMENTS_BUNDLE_NAME, lastDefaultLocale);
21         }
22     }
23
24     /**
25      * Returns ResourceBundle for 'LocaleElements' and for provided locale.
26      */

27     public static ResourceBundle JavaDoc getLocaleElementsBundle(Locale JavaDoc locale) {
28         return ResourceBundle.getBundle(LOCALE_ELEMENTS_BUNDLE_NAME, locale);
29     }
30
31     /**
32      * Returns current ResourceBundle for 'LocaleElements'.
33      */

34     public static ResourceBundle JavaDoc getLocaleElementsBundle() {
35         checkBundles();
36         return localeElements;
37     }
38
39
40     // ---------------------------------------------------------------- locale elements
41

42     public static String JavaDoc[] getMonthNames() {
43         checkBundles();
44         return (String JavaDoc[]) localeElements.getObject("MonthNames");
45     }
46
47     public static String JavaDoc[] getMonthNames(Locale JavaDoc locale) {
48         if (locale == lastDefaultLocale) {
49             return getMonthNames();
50         }
51         return (String JavaDoc[]) getLocaleElementsBundle(locale).getObject("MonthNames");
52     }
53
54     public static String JavaDoc[] getMonthAbbreviations() {
55         checkBundles();
56         return (String JavaDoc[]) localeElements.getObject("MonthAbbreviations");
57     }
58
59     public static String JavaDoc[] getMonthAbbreviations(Locale JavaDoc locale) {
60         if (locale == lastDefaultLocale) {
61             return getMonthAbbreviations();
62         }
63         return (String JavaDoc[]) getLocaleElementsBundle(locale).getObject("MonthAbbreviations");
64     }
65
66     public static String JavaDoc[] getDayNames() {
67         checkBundles();
68         return (String JavaDoc[]) localeElements.getObject("DayNames");
69     }
70
71     public static String JavaDoc[] getDayNames(Locale JavaDoc locale) {
72         if (locale == lastDefaultLocale) {
73             return getDayNames();
74         }
75         return (String JavaDoc[]) getLocaleElementsBundle(locale).getObject("DayNames");
76     }
77
78     public static String JavaDoc[] getDayAbbreviations() {
79         checkBundles();
80         return (String JavaDoc[]) localeElements.getObject("DayAbbreviations");
81     }
82
83     public static String JavaDoc[] getDayAbbreviations(Locale JavaDoc locale) {
84         if (locale == lastDefaultLocale) {
85             return getDayAbbreviations();
86         }
87         return (String JavaDoc[]) getLocaleElementsBundle(locale).getObject("DayAbbreviations");
88     }
89
90
91
92
93 }
94
Popular Tags