01: /*
02: * @(#)locale.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.util.Locale;
12: import java.util.StringTokenizer;
13: import pnuts.lang.Context;
14:
15: public class locale {
16:
17: final static String LOCALE = "pnuts$lib$locale".intern();
18:
19: static Locale toLocale(Object arg) {
20: if (arg instanceof Locale) {
21: return (Locale) arg;
22: } else if (arg instanceof String) {
23: StringTokenizer st = new StringTokenizer((String) arg, "_");
24: int n = st.countTokens();
25: if (n == 1) {
26: return new Locale(st.nextToken());
27: } else if (n == 2) {
28: return new Locale(st.nextToken(), st.nextToken());
29: } else if (n > 2) {
30: return new Locale(st.nextToken(), st.nextToken(), st
31: .nextToken());
32: }
33: }
34: throw new IllegalArgumentException(String.valueOf(arg));
35: }
36:
37: static Locale getLocale(Context context) {
38: Locale locale = (Locale) context.get(LOCALE);
39: if (locale == null) {
40: context.set(LOCALE, locale = Locale.getDefault());
41: }
42: return locale;
43: }
44:
45: static void setLocale(Locale locale, Context context) {
46: context.set(LOCALE, locale);
47: DateTimeFormat.reset(context);
48: }
49: }
|