Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 331260 | Differences between
and this patch

Collapse All | Expand All

(-).project (+5 lines)
Lines 25-30 Link Here
25
			<arguments>
25
			<arguments>
26
			</arguments>
26
			</arguments>
27
		</buildCommand>
27
		</buildCommand>
28
		<buildCommand>
29
			<name>org.eclipse.pde.ds.core.builder</name>
30
			<arguments>
31
			</arguments>
32
		</buildCommand>
28
	</buildSpec>
33
	</buildSpec>
29
	<natures>
34
	<natures>
30
		<nature>org.eclipse.pde.PluginNature</nature>
35
		<nature>org.eclipse.pde.PluginNature</nature>
(-)META-INF/MANIFEST.MF (+3 lines)
Lines 11-16 Link Here
11
 CDC-1.1/Foundation-1.1
11
 CDC-1.1/Foundation-1.1
12
Import-Package: javax.inject,
12
Import-Package: javax.inject,
13
 org.eclipse.osgi.service.debug;version="1.1.0",
13
 org.eclipse.osgi.service.debug;version="1.1.0",
14
 org.eclipse.osgi.service.localization;version="1.1.0",
14
 org.osgi.framework;version="1.3.0",
15
 org.osgi.framework;version="1.3.0",
15
 org.osgi.service.event;resolution:=optional,
16
 org.osgi.service.event;resolution:=optional,
16
 org.osgi.util.tracker;version="1.4.2"
17
 org.osgi.util.tracker;version="1.4.2"
Lines 42-49 Link Here
42
   org.eclipse.e4.ui.workbench.swt,
43
   org.eclipse.e4.ui.workbench.swt,
43
   org.eclipse.ui.workbench",
44
   org.eclipse.ui.workbench",
44
 org.eclipse.e4.core.services.statusreporter;x-friends:="org.eclipse.e4.ui.workbench.swt",
45
 org.eclipse.e4.core.services.statusreporter;x-friends:="org.eclipse.e4.ui.workbench.swt",
46
 org.eclipse.e4.core.services.translation,
45
 org.eclipse.e4.core.services.util;x-internal:=true,
47
 org.eclipse.e4.core.services.util;x-internal:=true,
46
 org.eclipse.e4.core.services.work;x-friends:="org.eclipse.e4.ui.workbench"
48
 org.eclipse.e4.core.services.work;x-friends:="org.eclipse.e4.ui.workbench"
47
Eclipse-ExtensibleAPI: true
49
Eclipse-ExtensibleAPI: true
48
Bundle-ClassPath: injection_annotations.jar, .
50
Bundle-ClassPath: injection_annotations.jar, .
49
Bundle-Activator: org.eclipse.e4.core.internal.services.ServicesActivator
51
Bundle-Activator: org.eclipse.e4.core.internal.services.ServicesActivator
52
Service-Component: OSGI-INF/translationService.xml
(-)OSGI-INF/translationService.xml (+8 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.e4.core.services">
3
   <implementation class="org.eclipse.e4.core.internal.services.TranslationServiceImpl"/>
4
   <service>
5
      <provide interface="org.eclipse.e4.core.services.translation.ITranslationService"/>
6
   </service>
7
   <reference bind="dsRegisterTranslationProvider" cardinality="0..n" interface="org.eclipse.e4.core.services.translation.ITranslationProvider" name="ITranslationProvider" policy="static" unbind="dsUnregisterTranslationProvider"/>
8
</scr:component>
(-)build.properties (-12 / +3 lines)
Lines 1-19 Link Here
1
###############################################################################
2
#  Copyright (c) 2008 IBM Corporation and others.
3
#  All rights reserved. This program and the accompanying materials
4
#  are made available under the terms of the Eclipse Public License v1.0
5
#  which accompanies this distribution, and is available at
6
#  http://www.eclipse.org/legal/epl-v10.html
7
# 
8
#  Contributors:
9
#      IBM Corporation - initial API and implementation
10
###############################################################################
11
source.. = src/
12
output.. = bin/
1
output.. = bin/
13
bin.includes = META-INF/,\
2
bin.includes = META-INF/,\
14
               .,\
3
               .,\
15
               plugin.xml,\
4
               plugin.xml,\
16
               .options,\
5
               .options,\
17
               about.html,\
6
               about.html,\
18
               plugin.properties
7
               plugin.properties,\
8
               OSGI-INF/
19
src.includes = schema/
9
src.includes = schema/
10
source.. = src/
(-)src/org/eclipse/e4/core/internal/services/ServicesActivator.java (+37 lines)
Lines 11-16 Link Here
11
package org.eclipse.e4.core.internal.services;
11
package org.eclipse.e4.core.internal.services;
12
12
13
import org.eclipse.osgi.service.debug.DebugOptions;
13
import org.eclipse.osgi.service.debug.DebugOptions;
14
import org.eclipse.osgi.service.localization.BundleLocalization;
15
import org.eclipse.osgi.service.localization.LocaleProvider;
14
import org.osgi.framework.BundleActivator;
16
import org.osgi.framework.BundleActivator;
15
import org.osgi.framework.BundleContext;
17
import org.osgi.framework.BundleContext;
16
import org.osgi.util.tracker.ServiceTracker;
18
import org.osgi.util.tracker.ServiceTracker;
Lines 20-25 Link Here
20
	static private ServicesActivator defaultInstance;
22
	static private ServicesActivator defaultInstance;
21
	private BundleContext bundleContext;
23
	private BundleContext bundleContext;
22
	private ServiceTracker debugTracker = null;
24
	private ServiceTracker debugTracker = null;
25
	private ServiceTracker localeProviderTracker = null;
26
	private ServiceTracker bundleLocalizationTracker = null;
23
27
24
	public ServicesActivator() {
28
	public ServicesActivator() {
25
		defaultInstance = this;
29
		defaultInstance = this;
Lines 38-43 Link Here
38
			debugTracker.close();
42
			debugTracker.close();
39
			debugTracker = null;
43
			debugTracker = null;
40
		}
44
		}
45
46
		if (localeProviderTracker != null) {
47
			localeProviderTracker.close();
48
			localeProviderTracker = null;
49
		}
50
51
		if (bundleLocalizationTracker != null) {
52
			bundleLocalizationTracker.close();
53
			bundleLocalizationTracker = null;
54
		}
55
41
		bundleContext = null;
56
		bundleContext = null;
42
	}
57
	}
43
58
Lines 55-58 Link Here
55
		return defaultValue;
70
		return defaultValue;
56
	}
71
	}
57
72
73
	public LocaleProvider getLocaleProvider() {
74
		if (localeProviderTracker == null) {
75
			localeProviderTracker = new ServiceTracker(bundleContext,
76
					LocaleProvider.class.getName(), null);
77
			localeProviderTracker.open();
78
		}
79
80
		return (LocaleProvider) localeProviderTracker.getService();
81
	}
82
83
	public BundleLocalization getBundleLocalization() {
84
		if (bundleLocalizationTracker == null) {
85
			bundleLocalizationTracker = new ServiceTracker(bundleContext,
86
					BundleLocalization.class.getName(), null);
87
			bundleLocalizationTracker.open();
88
		}
89
		return (BundleLocalization) bundleLocalizationTracker.getService();
90
	}
91
92
	public BundleContext getContext() {
93
		return bundleContext;
94
	}
58
}
95
}
(-)src/org/eclipse/e4/core/internal/services/TranslationServiceImpl.java (+136 lines)
Added Link Here
1
package org.eclipse.e4.core.internal.services;
2
3
import java.util.Collections;
4
import java.util.HashMap;
5
import java.util.Locale;
6
import java.util.Map;
7
import java.util.MissingResourceException;
8
import java.util.ResourceBundle;
9
import org.eclipse.e4.core.services.translation.ITranslationProvider;
10
import org.eclipse.e4.core.services.translation.ITranslationService;
11
import org.eclipse.osgi.service.localization.LocaleProvider;
12
import org.osgi.framework.Bundle;
13
14
public class TranslationServiceImpl implements ITranslationService {
15
	private Map<String, ITranslationProvider> providerMap = Collections
16
			.synchronizedMap(new HashMap<String, ITranslationProvider>());
17
18
	public TranslationServiceImpl() {
19
		System.err.println("Service is created!!!");
20
	}
21
22
	private Locale getLocale() {
23
		LocaleProvider localeProvider = ServicesActivator.getDefault().getLocaleProvider();
24
		if (localeProvider != null) {
25
			Locale currentLocale = localeProvider.getLocale();
26
			if (currentLocale != null)
27
				return currentLocale;
28
		}
29
		return Locale.getDefault();
30
	}
31
32
	public String translate(String category, String key) {
33
		return translate(getLocale(), category, key);
34
	}
35
36
	public String translate(Locale locale, String category, String key) {
37
		ITranslationProvider pv = getProvider(category);
38
39
		if (pv != null) {
40
			return pv.translate(locale, category, key);
41
		}
42
43
		throw new MissingResourceException("Category '" + category + " is not known'", null, key);
44
	}
45
46
	private ITranslationProvider getProvider(String category) {
47
		ITranslationProvider pv = providerMap.get(category);
48
		if (pv == null) {
49
			pv = tryCreateBundleLocalization(category);
50
			// TODO Should we cache it? Then we need to add a tracker if the bundle is
51
			// installed/uninstalled
52
		}
53
54
		return pv;
55
	}
56
57
	private ITranslationProvider tryCreateBundleLocalization(String category) {
58
		for (Bundle b : ServicesActivator.getDefault().getContext().getBundles()) {
59
			String[] parts = category.split("@");
60
			if (b.getSymbolicName().equals(parts[0])
61
					&& (parts.length == 1 || parts[1].equals(b.getVersion().toString()))) {
62
				final Bundle bundle = b;
63
				return new ITranslationProvider() {
64
65
					public String translate(Locale locale, String category, String key) {
66
						ResourceBundle resBundle = ServicesActivator.getDefault()
67
								.getBundleLocalization().getLocalization(bundle, locale.toString());
68
						if (resBundle != null) {
69
							try {
70
								return resBundle.getString(key);
71
							} catch (Exception e) {
72
								throw new MissingResourceException("Could not find '" + key
73
										+ "' in bundle localization.", resBundle.getClass()
74
										.getName(), key);
75
							}
76
						}
77
						throw new MissingResourceException("Bundle '" + category
78
								+ " has no localization informations.'", null, key);
79
					}
80
				};
81
			}
82
		}
83
84
		return null;
85
	}
86
87
	public String[] translate(String category, String... keys) {
88
		return translate(getLocale(), category, keys);
89
	}
90
91
	public String[] translate(Locale locale, String category, String... keys) {
92
		ITranslationProvider pv = getProvider(category);
93
		if (pv != null) {
94
			String[] rv = new String[keys.length];
95
			for (int i = 0; i < keys.length; i++) {
96
				rv[i] = pv.translate(locale, category, keys[i]);
97
			}
98
			return rv;
99
		}
100
		throw new MissingResourceException("Category '" + category + " is not known'", null, null);
101
	}
102
103
	public void registerTranslationProvider(String category, ITranslationProvider provider) {
104
		if (providerMap.containsKey(category)) {
105
			throw new IllegalArgumentException(
106
					"Can not register more than one provider for category '" + category + "'."); //$NON-NLS-1$//$NON-NLS-2$
107
		}
108
		providerMap.put(category, provider);
109
	}
110
111
	public boolean unregisterTranslationProvider(String category, ITranslationProvider provider) {
112
		if (providerMap.get(category) == provider) {
113
			providerMap.remove(category);
114
			return true;
115
		}
116
		return false;
117
	}
118
119
	public void dsRegisterTranslationProvider(ITranslationProvider provider,
120
			Map<String, String> data) {
121
		for (String s : data.get("CATEGORIES").split(";")) { //$NON-NLS-1$//$NON-NLS-2$
122
			try {
123
				registerTranslationProvider(s, provider);
124
			} catch (IllegalArgumentException e) {
125
				// TODO: handle exception
126
			}
127
		}
128
	}
129
130
	public void dsUnregisterTranslationProvider(ITranslationProvider provider,
131
			Map<String, String> data) {
132
		for (String s : data.get("CATEGORIES").split(";")) { //$NON-NLS-1$//$NON-NLS-2$
133
			unregisterTranslationProvider(s, provider);
134
		}
135
	}
136
}
(-)src/org/eclipse/e4/core/services/translation/ITranslationProvider.java (+35 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2010 BestSolution.at and others.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 *  Contributors:
9
 *      Tom Schind<tom.schindl@bestsolution.at> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.e4.core.services.translation;
12
13
import java.util.Locale;
14
import java.util.MissingResourceException;
15
16
/**
17
 * Provides translation for a given category
18
 */
19
public interface ITranslationProvider {
20
	/**
21
	 * Translate the give key in the given locale's text
22
	 * 
23
	 * @param locale
24
	 *            the locale, must not be <code>null</code>
25
	 * @param category
26
	 *            the category
27
	 * @param key
28
	 *            the key
29
	 * @return the translated value must not be <code>null</code>
30
	 * @throws MissingResourceException
31
	 *             if translation fails because of whatever reason
32
	 */
33
	public String translate(Locale locale, String category, String key)
34
			throws MissingResourceException;
35
}
(-)src/org/eclipse/e4/core/services/translation/ITranslationService.java (+131 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2010 BestSolution.at and others.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 *  Contributors:
9
 *      Tom Schind<tom.schindl@bestsolution.at> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.e4.core.services.translation;
12
13
import java.util.Locale;
14
import java.util.MissingResourceException;
15
import org.eclipse.osgi.service.localization.BundleLocalization;
16
import org.eclipse.osgi.service.localization.LocaleProvider;
17
18
/**
19
 * Service which can be used to translate texts into locale specific ones
20
 */
21
public interface ITranslationService {
22
	/**
23
	 * Translate the key into the default locale.
24
	 * <p>
25
	 * <b>Locale lookup</b>
26
	 * <ul>
27
	 * <li>try to use {@link LocaleProvider} service</li>
28
	 * <li>use {@link Locale#getDefault()}</li>
29
	 * </ul>
30
	 * </p>
31
	 * <p>
32
	 * <b>Category lookup</b>
33
	 * <ul>
34
	 * <li>try to find a {@link ITranslationProvider} for the category</li>
35
	 * <li>interpret the value as a symbolic bundle name and try to access the
36
	 * {@link BundleLocalization}</li>
37
	 * </ul>
38
	 * </p>
39
	 * 
40
	 * @param category
41
	 *            the category/bundle name to use
42
	 * @param key
43
	 *            the key to translate
44
	 * @return the translated value, must not be <code>null</code>
45
	 * @throws MissingResourceException
46
	 *             if some problem occurrs while translating
47
	 */
48
	public String translate(String category, String key) throws MissingResourceException;
49
50
	/**
51
	 * Translate the key into the default locale.
52
	 * 
53
	 * <p>
54
	 * <b>Category lookup</b>
55
	 * <ul>
56
	 * <li>try to find a {@link ITranslationProvider} for the category</li>
57
	 * <li>interpret the value as a symbolic bundle name and try to access the
58
	 * {@link BundleLocalization}</li>
59
	 * </ul>
60
	 * </p>
61
	 * 
62
	 * @param locale
63
	 *            the locale to use
64
	 * @param category
65
	 *            the category/bundle name to use
66
	 * @param key
67
	 *            the key to translate
68
	 * @return the translated value, must not be <code>null</code>
69
	 * @throws MissingResourceException
70
	 *             if some problem occurrs while translating
71
	 */
72
	public String translate(Locale locale, String category, String key)
73
			throws MissingResourceException;
74
75
	/**
76
	 * Translate multiple keys at once. See {@link #translate(String, String)} for a detailed
77
	 * explanation how the locale and category look up is done
78
	 * 
79
	 * @param category
80
	 *            the category/bundle name to use
81
	 * @param keys
82
	 *            the keys
83
	 * @return the translated keys
84
	 * @throws MissingResourceException
85
	 *             if one of the keys is not found
86
	 * @see #translate(String, String)
87
	 */
88
	public String[] translate(String category, String... keys) throws MissingResourceException;
89
90
	/**
91
	 * Translate multiple keys at once. See {@link #translate(String, String)} for a detailed
92
	 * explanation how the category look up is done
93
	 * 
94
	 * @param locale
95
	 *            the locale to use
96
	 * @param category
97
	 *            the category/bundle name to use
98
	 * @param keys
99
	 *            the keys
100
	 * @return the translated keys
101
	 * @throws MissingResourceException
102
	 *             if one of the keys is not found
103
	 * @see #translate(String, String)
104
	 */
105
	public String[] translate(Locale locale, String category, String... keys)
106
			throws MissingResourceException;
107
108
	/**
109
	 * Register a translation provider for a category
110
	 * 
111
	 * @param category
112
	 *            the category
113
	 * @param provider
114
	 *            the provider
115
	 * @throws IllegalArgumentException
116
	 *             thrown if there's already a provider registered
117
	 */
118
	public void registerTranslationProvider(String category, ITranslationProvider provider)
119
			throws IllegalArgumentException;
120
121
	/**
122
	 * Unregister a translation provider for a category
123
	 * 
124
	 * @param category
125
	 *            the category of the provider
126
	 * @param provider
127
	 *            the provider instance to unregister
128
	 * @return <code>true</code> if unregistering succeeded
129
	 */
130
	public boolean unregisterTranslationProvider(String category, ITranslationProvider provider);
131
}
(-)src/org/eclipse/e4/core/services/translation/ResourceBundleTranslationProvider.java (+19 lines)
Added Link Here
1
package org.eclipse.e4.core.services.translation;
2
3
import java.util.Locale;
4
import java.util.ResourceBundle;
5
6
public class ResourceBundleTranslationProvider implements ITranslationProvider {
7
	private ClassLoader loader;
8
	private String baseName;
9
10
	public ResourceBundleTranslationProvider(ClassLoader loader, String baseName) {
11
		this.loader = loader;
12
		this.baseName = baseName;
13
	}
14
15
	public String translate(Locale locale, String category, String key) {
16
		return ResourceBundle.getBundle(baseName, locale, loader).getString(key);
17
	}
18
19
}
(-).project (+5 lines)
Lines 20-25 Link Here
20
			<arguments>
20
			<arguments>
21
			</arguments>
21
			</arguments>
22
		</buildCommand>
22
		</buildCommand>
23
		<buildCommand>
24
			<name>org.eclipse.pde.ds.core.builder</name>
25
			<arguments>
26
			</arguments>
27
		</buildCommand>
23
	</buildSpec>
28
	</buildSpec>
24
	<natures>
29
	<natures>
25
		<nature>org.eclipse.pde.PluginNature</nature>
30
		<nature>org.eclipse.pde.PluginNature</nature>
(-)CoreTestSuite.launch (-2 / +2 lines)
Lines 1-4 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
<launchConfiguration type="org.eclipse.pde.ui.JunitLaunchConfig">
2
<launchConfiguration type="org.eclipse.pde.ui.JunitLaunchConfig">
3
<booleanAttribute key="append.args" value="true"/>
3
<booleanAttribute key="append.args" value="true"/>
4
<stringAttribute key="application" value="org.eclipse.pde.junit.runtime.coretestapplication"/>
4
<stringAttribute key="application" value="org.eclipse.pde.junit.runtime.coretestapplication"/>
Lines 25-31 Link Here
25
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
25
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
26
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
26
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
27
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.e4.core.tests.CoreTestSuite"/>
27
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.e4.core.tests.CoreTestSuite"/>
28
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog"/>
28
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog -console"/>
29
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.e4.core.tests"/>
29
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.e4.core.tests"/>
30
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.pde.ui.workbenchClasspathProvider"/>
30
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.pde.ui.workbenchClasspathProvider"/>
31
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xms40m -Xmx384m"/>
31
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xms40m -Xmx384m"/>
(-)META-INF/MANIFEST.MF (-2 / +3 lines)
Lines 13-19 Link Here
13
 org.eclipse.osgi.services;bundle-version="3.2.100",
13
 org.eclipse.osgi.services;bundle-version="3.2.100",
14
 com.google.code.atinject.tck;bundle-version="1.0.0",
14
 com.google.code.atinject.tck;bundle-version="1.0.0",
15
 org.eclipse.equinox.registry;bundle-version="3.5.0",
15
 org.eclipse.equinox.registry;bundle-version="3.5.0",
16
 org.eclipse.core.tests.harness;bundle-version="3.6.0"
16
 org.eclipse.core.tests.harness;bundle-version="3.6.0",
17
 org.eclipse.e4.core.services;bundle-version="0.9.1"
17
Bundle-ActivationPolicy: lazy
18
Bundle-ActivationPolicy: lazy
18
Bundle-RequiredExecutionEnvironment: J2SE-1.5
19
Bundle-RequiredExecutionEnvironment: J2SE-1.5
19
Import-Package: javax.annotation;version="1.0.0",
20
Import-Package: javax.annotation;version="1.0.0",
Lines 26-29 Link Here
26
 org.eclipse.e4.core.internal.tests.di.extensions;x-internal:=true,
27
 org.eclipse.e4.core.internal.tests.di.extensions;x-internal:=true,
27
 org.eclipse.e4.core.internal.tests.manual;x-internal:=true,
28
 org.eclipse.e4.core.internal.tests.manual;x-internal:=true,
28
 org.eclipse.e4.core.tests
29
 org.eclipse.e4.core.tests
29
Service-Component: OSGI-INF/adder.xml
30
Service-Component: OSGI-INF/adder.xml, OSGI-INF/customtranslations.xml
(-)OSGI-INF/customtranslations.xml (+8 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.e4.core.tests.customtranslations">
3
   <implementation class="org.eclipse.e4.core.internal.tests.translation.CustomTranslationProvider"/>
4
   <service>
5
      <provide interface="org.eclipse.e4.core.services.translation.ITranslationProvider"/>
6
   </service>
7
   <property name="CATEGORIES" type="String" value="org.eclipse.e4.core.tests.customcat"/>
8
</scr:component>
(-)OSGI-INF/l10n/bundle.properties (+2 lines)
Added Link Here
1
key1 = Value En
2
key2 = Value En 2
(-)OSGI-INF/l10n/bundle_de.properties (+2 lines)
Added Link Here
1
key1 = Wert De
2
key2 = Wert De 2
(-)OSGI-INF/l10n/messages.properties (+2 lines)
Added Link Here
1
key1 = Cust Value En
2
key2 = Cust Value En 2
(-)OSGI-INF/l10n/messages_de.properties (+2 lines)
Added Link Here
1
key1 = Cust Wert De
2
key2 = Cust Wert De 2
(-)build.properties (-2 / +3 lines)
Lines 1-6 Link Here
1
source.. = src/
2
output.. = bin/
1
output.. = bin/
3
bin.includes = META-INF/,\
2
bin.includes = META-INF/,\
4
               .,\
3
               .,\
5
               test.xml,\
4
               test.xml,\
6
               OSGI-INF/
5
               OSGI-INF/,\
6
               OSGI-INF/customtranslations.xml
7
source.. = src/
(-)src/org/eclipse/e4/core/internal/tests/translation/CustomTranslationProvider.java (+12 lines)
Added Link Here
1
package org.eclipse.e4.core.internal.tests.translation;
2
3
import org.eclipse.e4.core.services.translation.ResourceBundleTranslationProvider;
4
5
public class CustomTranslationProvider extends
6
		ResourceBundleTranslationProvider {
7
	public CustomTranslationProvider() {
8
		super(CustomTranslationProvider.class.getClassLoader(),
9
				"OSGI-INF/l10n/messages");
10
11
	}
12
}
(-)src/org/eclipse/e4/core/internal/tests/translation/TestTranslationService.java (+137 lines)
Added Link Here
1
package org.eclipse.e4.core.internal.tests.translation;
2
3
import java.util.Locale;
4
import java.util.MissingResourceException;
5
6
import org.eclipse.e4.core.internal.tests.CoreTestsActivator;
7
import org.eclipse.e4.core.services.translation.ITranslationService;
8
import org.osgi.framework.BundleContext;
9
import org.osgi.framework.ServiceReference;
10
11
import junit.framework.TestCase;
12
13
public class TestTranslationService extends TestCase {
14
	private ITranslationService service;
15
16
	@Override
17
	protected void setUp() throws Exception {
18
		super.setUp();
19
		Locale.setDefault(Locale.ENGLISH);
20
	}
21
22
	public ITranslationService getService() {
23
		if (service == null) {
24
			BundleContext ctx = CoreTestsActivator.getDefault()
25
					.getBundleContext();
26
			ServiceReference<ITranslationService> ref = ctx
27
					.getServiceReference(ITranslationService.class);
28
			service = ctx.getService(ref);
29
		}
30
31
		return service;
32
	}
33
34
	public void testBundleTranslations() {
35
		String category = "org.eclipse.e4.core.tests";
36
		ITranslationService s = getService();
37
38
		assertEquals("Value En", s.translate(category, "key1"));
39
		assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1"));
40
41
		Locale.setDefault(Locale.GERMAN);
42
43
		assertEquals("Wert De", s.translate(category, "key1"));
44
		assertEquals("Wert De", s.translate(Locale.GERMAN, category, "key1"));
45
46
		// Expected to fail because Bug 330602 in Equinox
47
		// assertEquals("Wert De",s.translate(Locale.FRENCH,
48
		// "org.eclipse.e4.core.tests", "key1"));
49
50
		assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1"));
51
52
		try {
53
			s.translate(category, "unknown");
54
			fail("Unknown key has to fail with MissingResourceException");
55
		} catch (MissingResourceException e) {
56
		}
57
58
		try {
59
			s.translate(Locale.ENGLISH, category, "unknown");
60
			fail("Unknown key has to fail with MissingResourceException");
61
		} catch (MissingResourceException e) {
62
		}
63
64
		assertEquals(2, s.translate(category, "key1", "key2").length);
65
		assertEquals("Wert De", s.translate(category, "key1", "key2")[0]);
66
		assertEquals("Wert De 2", s.translate(category, "key1", "key2")[1]);
67
		
68
		// Expected to fail because Bug 330602 in Equinox
69
//		assertEquals(2, s.translate(Locale.FRENCH, category, "key1", "key2").length);
70
//		assertEquals("Wert De",
71
//				s.translate(Locale.FRENCH, category, "key1", "key2")[0]);
72
//		assertEquals("Wert De 2",
73
//				s.translate(Locale.FRENCH, category, "key1", "key2")[1]);
74
		
75
		assertEquals(2, s.translate(Locale.ENGLISH, category, "key1", "key2").length);
76
		assertEquals("Value En",
77
				s.translate(Locale.ENGLISH, category, "key1", "key2")[0]);
78
		assertEquals("Value En 2",
79
				s.translate(Locale.ENGLISH, category, "key1", "key2")[1]);
80
	}
81
82
	public void testTranslationProvider() {
83
		String category = "org.eclipse.e4.core.tests.customcat";
84
		ITranslationService s = getService();
85
86
		assertEquals("Cust Value En", s.translate(category, "key1"));
87
		assertEquals("Cust Value En",
88
				s.translate(Locale.ENGLISH, category, "key1"));
89
90
		Locale.setDefault(Locale.GERMAN);
91
92
		assertEquals("Cust Wert De", s.translate(category, "key1"));
93
		assertEquals("Cust Wert De",
94
				s.translate(Locale.GERMAN, category, "key1"));
95
96
		assertEquals("Cust Wert De",
97
				s.translate(Locale.FRENCH, category, "key1"));
98
99
		// Expected to fail because waiting for bug 330602 and the key defined
100
		// to inculde the en bundle
101
		// assertEquals("Cust Value En", s.translate(Locale.ENGLISH, category,
102
		// "key1"));
103
		
104
		try {
105
			s.translate(category, "unknown");
106
			fail("Unknown key has to fail with MissingResourceException");
107
		} catch (MissingResourceException e) {
108
		}
109
110
		try {
111
			s.translate(Locale.ENGLISH, category, "unknown");
112
			fail("Unknown key has to fail with MissingResourceException");
113
		} catch (MissingResourceException e) {
114
		}
115
116
		assertEquals(2, s.translate(category, "key1", "key2").length);
117
		assertEquals("Cust Wert De", s.translate(category, "key1", "key2")[0]);
118
		assertEquals("Cust Wert De 2", s.translate(category, "key1", "key2")[1]);
119
120
		assertEquals(2,
121
				s.translate(Locale.FRENCH, category, "key1", "key2").length);
122
		assertEquals("Cust Wert De",
123
				s.translate(Locale.FRENCH, category, "key1", "key2")[0]);
124
		assertEquals("Cust Wert De 2",
125
				s.translate(Locale.FRENCH, category, "key1", "key2")[1]);
126
127
		// Expected to fail because waiting for bug 330602 and the key defined
128
		// to inculde the en bundle
129
130
		// assertEquals(2, s.translate(Locale.ENGLISH, category, "key1",
131
		// "key2").length);
132
		// assertEquals("Cust Value En",
133
		// s.translate(Locale.ENGLISH, category, "key1", "key2")[0]);
134
		// assertEquals("Cust Value En 2",
135
		// s.translate(Locale.ENGLISH, category, "key1", "key2")[1]);
136
	}
137
}
(-)src/org/eclipse/e4/core/tests/CoreTestSuite.java (+4 lines)
Lines 48-53 Link Here
48
import org.eclipse.e4.core.internal.tests.di.extensions.InjectionEventTest;
48
import org.eclipse.e4.core.internal.tests.di.extensions.InjectionEventTest;
49
import org.eclipse.e4.core.internal.tests.di.extensions.InjectionMixedSuppliersTest;
49
import org.eclipse.e4.core.internal.tests.di.extensions.InjectionMixedSuppliersTest;
50
import org.eclipse.e4.core.internal.tests.di.extensions.InjectionPreferencesTest;
50
import org.eclipse.e4.core.internal.tests.di.extensions.InjectionPreferencesTest;
51
import org.eclipse.e4.core.internal.tests.translation.TestTranslationService;
51
52
52
public class CoreTestSuite extends TestSuite {
53
public class CoreTestSuite extends TestSuite {
53
	public static Test suite() {
54
	public static Test suite() {
Lines 96-100 Link Here
96
		addTestSuite(Bug317183Test.class);
97
		addTestSuite(Bug317183Test.class);
97
		addTestSuite(DependenciesLeakTest.class);
98
		addTestSuite(DependenciesLeakTest.class);
98
		addTestSuite(ActivationInjectionTest.class);
99
		addTestSuite(ActivationInjectionTest.class);
100
		
101
		// Translation
102
		addTestSuite(TestTranslationService.class);
99
	}
103
	}
100
}
104
}

Return to bug 331260