Skip to main content
summaryrefslogtreecommitdiffstats
blob: fa62e949702ff1fec7b6d809e1a8cd58fe6d7dff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.meta.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author mengbo
 * @version 1.5
 */
public class LocaleFallback {
	private static Map map = new HashMap();

	public static synchronized String[] fallBack(Locale locale) {
		if (map.get(locale) != null) {
			return (String[]) map.get(locale);
		}
		// first compute the fallback locales according to the input locale
		List inputLocaleList = calculateLocaleNames(locale);
		String[] ins = reverseList(inputLocaleList);
		// then compute the fallback locales accroding to the system default
		// locale
		int defsSize = 0;
		String[] defs = null;
		if (!locale.equals(Locale.getDefault())) {
			List defLocaleList = calculateLocaleNames(Locale.getDefault());
			defs = reverseList(defLocaleList);
			defsSize = defs.length;
		}

		int insSize = ins.length;
		int size = insSize + defsSize;

		String[] options = new String[size + 1];
		for (int i = 0; i < size; i++) {
			if (i < insSize) {
				options[i] = ins[i];
			} else {
				options[i] = defs[i - insSize];
			}
		}
		// last add blank string in order to search the base file
		options[size] = "";
		map.put(locale, options);

		return options;

	}

	private static List calculateLocaleNames(Locale locale) {
		List list = new ArrayList();
		StringBuffer item;

		String language = locale.getLanguage();
		String country = locale.getCountry();
		String variant = locale.getVariant();

		int languageLength = language.length();
		int countryLength = country.length();
		int variantLength = variant.length();

		if (languageLength == 0 && countryLength == 0 && variantLength == 0) {
			// The locale is "", "", "".
			return list;
		}
        item = new StringBuffer();
        item = item.append('_').append(language);
        list.add(item.toString());

		if (countryLength == 0 && variantLength == 0) {
			return list;
		}
        item.append('_').append(country);
        list.add(item.toString());

		if (variantLength == 0) {
			return list;
		}
        item.append('_').append(variantLength);
        list.add(item.toString());

		return list;
	}

	private static String[] reverseList(List list) {
		int size = list.size();
		String[] vals = new String[size];
		for (int i = 0; i < size; i++) {
			vals[i] = (String) list.get(size - 1 - i);
		}
		return vals;
	}
}

Back to the top