Skip to main content
summaryrefslogtreecommitdiffstats
blob: c255ee13cb7d8e97b8aac2204a06b1521c637c58 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
 * Copyright (c) 2011 BestSolution.at 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:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 ******************************************************************************/
package org.eclipse.e4.tools.services.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.eclipse.osgi.service.localization.BundleLocalization;

/**
 * The lookup of the translation the same than the one in {@link BundleLocalization} which is based
 * upon the value found in equinox.root.locale which defaults to "en":
 * <ul>
 * <li>If set to empty string then the search order for:
 * <ul>
 * <li>bn + Ls + "_" + Cs + "_" + Vs</li>
 * <li>bn + Ls + "_" + Cs</li>
 * <li>bn + Ls</li>
 * <li>bn + Ld + "_" + Cd + "_" + Vd</li>
 * <li>bn + Ld + "_" + Cd</li>
 * <li>bn + Ld</li>
 * </ul>
 * </li>
 * <li>If Ls equals the value of equinox.root.locale then the following search order is used:
 * <ul>
 * <li>bn + Ls + "_" + Cs + "_" + Vs</li>
 * <li>bn + Ls + "_" + Cs</li>
 * <li>bn + Ls</li>
 * <li>bn + Ld + "_" + Cd + "_" + Vd</li>
 * <li>bn + Ld + "_" + Cd</li>
 * <li>bn + Ld</li>
 * </ul>
 * </li>
 * </ul>
 * Where bn is this bundle's localization basename, Ls, Cs and Vs are the specified locale
 * (language, country, variant) and Ld, Cd and Vd are the default locale (language, country,
 * variant).
 */
public abstract class AbstractTranslationProvider {
	final static String DEFAULT_ROOT = getEquinoxRootLocale();

	private final Hashtable<String, BundleResourceBundle> cache = new Hashtable<String, BundleResourceBundle>(
			5);

	private static String getEquinoxRootLocale() {
		// Logic from FrameworkProperties.getProperty("equinox.root.locale", "en")
		String root = System.getProperties().getProperty("equinox.root.locale");
		if (root == null) {
			root = "en";
		}
		return root;
	}
	
	protected abstract InputStream getResourceAsStream(String name);
	protected abstract String getBasename();

	public String translate(String locale, String key) {
		String defaultLocale = Locale.getDefault().toString();
		String localeString = locale;
		ResourceBundle bundle = getResourceBundle(locale.toString(),
				defaultLocale.equals(localeString));
		try {
			if( bundle == null ) {
				return key;
			}
			return bundle.getString(key);
		} catch (Exception e) {
			e.printStackTrace();
			return key;
		}

	}

	private ResourceBundle getResourceBundle(String localeString, boolean isDefaultLocale) {
		BundleResourceBundle resourceBundle = lookupResourceBundle(localeString);
		if (isDefaultLocale)
			return (ResourceBundle) resourceBundle;
		// need to determine if this is resource bundle is an empty stem
		// if it is then the default locale should be used
		if (resourceBundle == null || resourceBundle.isStemEmpty())
			return (ResourceBundle) lookupResourceBundle(Locale.getDefault().toString());
		return (ResourceBundle) resourceBundle;
	}

	private interface BundleResourceBundle {
		void setParent(ResourceBundle parent);

		boolean isEmpty();

		boolean isStemEmpty();
	}

	private BundleResourceBundle lookupResourceBundle(String localeString) {
		// get the localization header as late as possible to avoid accessing the raw headers
		// getting the first value from the raw headers forces the manifest to be parsed (bug
		// 332039)
		String localizationHeader = getBasename();
		synchronized (cache) {
			BundleResourceBundle result = cache.get(localeString);
			if (result != null)
				return result.isEmpty() ? null : result;
			String[] nlVarients = buildNLVariants(localeString);
			BundleResourceBundle parent = null;

			for (int i = nlVarients.length - 1; i >= 0; i--) {
				BundleResourceBundle varientBundle = null;
				InputStream resourceStream = getResourceAsStream(localizationHeader
						+ (nlVarients[i].equals("") ? nlVarients[i] : '_' + nlVarients[i])
						+ ".properties");

				if (resourceStream == null) {
					varientBundle = cache.get(nlVarients[i]);
				} else {
					try {
						varientBundle = new LocalizationResourceBundle(resourceStream);
					} catch (IOException e) {
						// ignore and continue
					} finally {
						if (resourceStream != null) {
							try {
								resourceStream.close();
							} catch (IOException e3) {
								// Ignore exception
							}
						}
					}
				}

				if (varientBundle == null) {
					varientBundle = new EmptyResouceBundle(nlVarients[i]);
				}
				if (parent != null)
					varientBundle.setParent((ResourceBundle) parent);
				cache.put(nlVarients[i], varientBundle);
				parent = varientBundle;
			}
			result = cache.get(localeString);
			return result.isEmpty() ? null : result;
		}
	}

	private String[] buildNLVariants(String nl) {
		List<String> result = new ArrayList<String>();
		while (nl.length() > 0) {
			result.add(nl);
			int i = nl.lastIndexOf('_');
			nl = (i < 0) ? "" : nl.substring(0, i); //$NON-NLS-1$
		}
		result.add(""); //$NON-NLS-1$
		return result.toArray(new String[result.size()]);
	}

	private class LocalizationResourceBundle extends PropertyResourceBundle implements
			BundleResourceBundle {
		public LocalizationResourceBundle(InputStream in) throws IOException {
			super(in);
		}

		public void setParent(ResourceBundle parent) {
			super.setParent(parent);
		}

		public boolean isEmpty() {
			return false;
		}

		public boolean isStemEmpty() {
			return parent == null;
		}
	}

	class EmptyResouceBundle extends ResourceBundle implements BundleResourceBundle {
		private final String localeString;

		public EmptyResouceBundle(String locale) {
			super();
			this.localeString = locale;
		}

		public Enumeration<String> getKeys() {
			return null;
		}

		protected Object handleGetObject(String arg0) throws MissingResourceException {
			return null;
		}

		public void setParent(ResourceBundle parent) {
			super.setParent(parent);
		}

		public boolean isEmpty() {
			if (parent == null)
				return true;
			return ((BundleResourceBundle) parent).isEmpty();
		}

		public boolean isStemEmpty() {
			if (DEFAULT_ROOT.equals(localeString))
				return false;
			if (parent == null)
				return true;
			return ((BundleResourceBundle) parent).isStemEmpty();
		}
	}
	
	protected void clearCache() {
		cache.clear();
	}
}

Back to the top