Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 185001 Details for
Bug 331260
Implement an extensible translation service
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
patch
patch.txt (text/plain), 41.31 KB, created by
Thomas Schindl
on 2010-12-10 16:42:35 EST
(
hide
)
Description:
patch
Filename:
MIME Type:
Creator:
Thomas Schindl
Created:
2010-12-10 16:42:35 EST
Size:
41.31 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.e4.core.services >Index: .project >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/bundles/org.eclipse.e4.core.services/.project,v >retrieving revision 1.2 >diff -u -r1.2 .project >--- .project 4 Feb 2009 18:56:31 -0000 1.2 >+++ .project 10 Dec 2010 21:40:23 -0000 >@@ -25,6 +25,11 @@ > <arguments> > </arguments> > </buildCommand> >+ <buildCommand> >+ <name>org.eclipse.pde.ds.core.builder</name> >+ <arguments> >+ </arguments> >+ </buildCommand> > </buildSpec> > <natures> > <nature>org.eclipse.pde.PluginNature</nature> >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/bundles/org.eclipse.e4.core.services/META-INF/MANIFEST.MF,v >retrieving revision 1.33 >diff -u -r1.33 MANIFEST.MF >--- META-INF/MANIFEST.MF 20 Jul 2010 19:43:28 -0000 1.33 >+++ META-INF/MANIFEST.MF 10 Dec 2010 21:40:23 -0000 >@@ -11,6 +11,7 @@ > CDC-1.1/Foundation-1.1 > Import-Package: javax.inject, > org.eclipse.osgi.service.debug;version="1.1.0", >+ org.eclipse.osgi.service.localization;version="1.1.0", > org.osgi.framework;version="1.3.0", > org.osgi.service.event;resolution:=optional, > org.osgi.util.tracker;version="1.4.2" >@@ -42,8 +43,10 @@ > org.eclipse.e4.ui.workbench.swt, > org.eclipse.ui.workbench", > org.eclipse.e4.core.services.statusreporter;x-friends:="org.eclipse.e4.ui.workbench.swt", >+ org.eclipse.e4.core.services.translation, > org.eclipse.e4.core.services.util;x-internal:=true, > org.eclipse.e4.core.services.work;x-friends:="org.eclipse.e4.ui.workbench" > Eclipse-ExtensibleAPI: true > Bundle-ClassPath: injection_annotations.jar, . > Bundle-Activator: org.eclipse.e4.core.internal.services.ServicesActivator >+Service-Component: OSGI-INF/translationService.xml >Index: OSGI-INF/translationService.xml >=================================================================== >RCS file: OSGI-INF/translationService.xml >diff -N OSGI-INF/translationService.xml >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ OSGI-INF/translationService.xml 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,8 @@ >+<?xml version="1.0" encoding="UTF-8"?> >+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.e4.core.services"> >+ <implementation class="org.eclipse.e4.core.internal.services.TranslationServiceImpl"/> >+ <service> >+ <provide interface="org.eclipse.e4.core.services.translation.ITranslationService"/> >+ </service> >+ <reference bind="dsRegisterTranslationProvider" cardinality="0..n" interface="org.eclipse.e4.core.services.translation.ITranslationProvider" name="ITranslationProvider" policy="static" unbind="dsUnregisterTranslationProvider"/> >+</scr:component> >Index: build.properties >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/bundles/org.eclipse.e4.core.services/build.properties,v >retrieving revision 1.5 >diff -u -r1.5 build.properties >--- build.properties 20 Jul 2010 19:43:28 -0000 1.5 >+++ build.properties 10 Dec 2010 21:40:24 -0000 >@@ -1,19 +1,10 @@ >-############################################################################### >-# Copyright (c) 2008 IBM Corporation 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: >-# IBM Corporation - initial API and implementation >-############################################################################### >-source.. = src/ > output.. = bin/ > bin.includes = META-INF/,\ > .,\ > plugin.xml,\ > .options,\ > about.html,\ >- plugin.properties >+ plugin.properties,\ >+ OSGI-INF/ > src.includes = schema/ >+source.. = src/ >Index: src/org/eclipse/e4/core/internal/services/ServicesActivator.java >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ServicesActivator.java,v >retrieving revision 1.1 >diff -u -r1.1 ServicesActivator.java >--- src/org/eclipse/e4/core/internal/services/ServicesActivator.java 30 Nov 2009 16:55:53 -0000 1.1 >+++ src/org/eclipse/e4/core/internal/services/ServicesActivator.java 10 Dec 2010 21:40:24 -0000 >@@ -11,6 +11,8 @@ > package org.eclipse.e4.core.internal.services; > > import org.eclipse.osgi.service.debug.DebugOptions; >+import org.eclipse.osgi.service.localization.BundleLocalization; >+import org.eclipse.osgi.service.localization.LocaleProvider; > import org.osgi.framework.BundleActivator; > import org.osgi.framework.BundleContext; > import org.osgi.util.tracker.ServiceTracker; >@@ -20,6 +22,8 @@ > static private ServicesActivator defaultInstance; > private BundleContext bundleContext; > private ServiceTracker debugTracker = null; >+ private ServiceTracker localeProviderTracker = null; >+ private ServiceTracker bundleLocalizationTracker = null; > > public ServicesActivator() { > defaultInstance = this; >@@ -38,6 +42,17 @@ > debugTracker.close(); > debugTracker = null; > } >+ >+ if (localeProviderTracker != null) { >+ localeProviderTracker.close(); >+ localeProviderTracker = null; >+ } >+ >+ if (bundleLocalizationTracker != null) { >+ bundleLocalizationTracker.close(); >+ bundleLocalizationTracker = null; >+ } >+ > bundleContext = null; > } > >@@ -55,4 +70,26 @@ > return defaultValue; > } > >+ public LocaleProvider getLocaleProvider() { >+ if (localeProviderTracker == null) { >+ localeProviderTracker = new ServiceTracker(bundleContext, >+ LocaleProvider.class.getName(), null); >+ localeProviderTracker.open(); >+ } >+ >+ return (LocaleProvider) localeProviderTracker.getService(); >+ } >+ >+ public BundleLocalization getBundleLocalization() { >+ if (bundleLocalizationTracker == null) { >+ bundleLocalizationTracker = new ServiceTracker(bundleContext, >+ BundleLocalization.class.getName(), null); >+ bundleLocalizationTracker.open(); >+ } >+ return (BundleLocalization) bundleLocalizationTracker.getService(); >+ } >+ >+ public BundleContext getContext() { >+ return bundleContext; >+ } > } >Index: src/org/eclipse/e4/core/internal/services/TranslationServiceImpl.java >=================================================================== >RCS file: src/org/eclipse/e4/core/internal/services/TranslationServiceImpl.java >diff -N src/org/eclipse/e4/core/internal/services/TranslationServiceImpl.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/internal/services/TranslationServiceImpl.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,136 @@ >+package org.eclipse.e4.core.internal.services; >+ >+import java.util.Collections; >+import java.util.HashMap; >+import java.util.Locale; >+import java.util.Map; >+import java.util.MissingResourceException; >+import java.util.ResourceBundle; >+import org.eclipse.e4.core.services.translation.ITranslationProvider; >+import org.eclipse.e4.core.services.translation.ITranslationService; >+import org.eclipse.osgi.service.localization.LocaleProvider; >+import org.osgi.framework.Bundle; >+ >+public class TranslationServiceImpl implements ITranslationService { >+ private Map<String, ITranslationProvider> providerMap = Collections >+ .synchronizedMap(new HashMap<String, ITranslationProvider>()); >+ >+ public TranslationServiceImpl() { >+ System.err.println("Service is created!!!"); >+ } >+ >+ private Locale getLocale() { >+ LocaleProvider localeProvider = ServicesActivator.getDefault().getLocaleProvider(); >+ if (localeProvider != null) { >+ Locale currentLocale = localeProvider.getLocale(); >+ if (currentLocale != null) >+ return currentLocale; >+ } >+ return Locale.getDefault(); >+ } >+ >+ public String translate(String category, String key) { >+ return translate(getLocale(), category, key); >+ } >+ >+ public String translate(Locale locale, String category, String key) { >+ ITranslationProvider pv = getProvider(category); >+ >+ if (pv != null) { >+ return pv.translate(locale, category, key); >+ } >+ >+ throw new MissingResourceException("Category '" + category + " is not known'", null, key); >+ } >+ >+ private ITranslationProvider getProvider(String category) { >+ ITranslationProvider pv = providerMap.get(category); >+ if (pv == null) { >+ pv = tryCreateBundleLocalization(category); >+ // TODO Should we cache it? Then we need to add a tracker if the bundle is >+ // installed/uninstalled >+ } >+ >+ return pv; >+ } >+ >+ private ITranslationProvider tryCreateBundleLocalization(String category) { >+ for (Bundle b : ServicesActivator.getDefault().getContext().getBundles()) { >+ String[] parts = category.split("@"); >+ if (b.getSymbolicName().equals(parts[0]) >+ && (parts.length == 1 || parts[1].equals(b.getVersion().toString()))) { >+ final Bundle bundle = b; >+ return new ITranslationProvider() { >+ >+ public String translate(Locale locale, String category, String key) { >+ ResourceBundle resBundle = ServicesActivator.getDefault() >+ .getBundleLocalization().getLocalization(bundle, locale.toString()); >+ if (resBundle != null) { >+ try { >+ return resBundle.getString(key); >+ } catch (Exception e) { >+ throw new MissingResourceException("Could not find '" + key >+ + "' in bundle localization.", resBundle.getClass() >+ .getName(), key); >+ } >+ } >+ throw new MissingResourceException("Bundle '" + category >+ + " has no localization informations.'", null, key); >+ } >+ }; >+ } >+ } >+ >+ return null; >+ } >+ >+ public String[] translate(String category, String... keys) { >+ return translate(getLocale(), category, keys); >+ } >+ >+ public String[] translate(Locale locale, String category, String... keys) { >+ ITranslationProvider pv = getProvider(category); >+ if (pv != null) { >+ String[] rv = new String[keys.length]; >+ for (int i = 0; i < keys.length; i++) { >+ rv[i] = pv.translate(locale, category, keys[i]); >+ } >+ return rv; >+ } >+ throw new MissingResourceException("Category '" + category + " is not known'", null, null); >+ } >+ >+ public void registerTranslationProvider(String category, ITranslationProvider provider) { >+ if (providerMap.containsKey(category)) { >+ throw new IllegalArgumentException( >+ "Can not register more than one provider for category '" + category + "'."); //$NON-NLS-1$//$NON-NLS-2$ >+ } >+ providerMap.put(category, provider); >+ } >+ >+ public boolean unregisterTranslationProvider(String category, ITranslationProvider provider) { >+ if (providerMap.get(category) == provider) { >+ providerMap.remove(category); >+ return true; >+ } >+ return false; >+ } >+ >+ public void dsRegisterTranslationProvider(ITranslationProvider provider, >+ Map<String, String> data) { >+ for (String s : data.get("CATEGORIES").split(";")) { //$NON-NLS-1$//$NON-NLS-2$ >+ try { >+ registerTranslationProvider(s, provider); >+ } catch (IllegalArgumentException e) { >+ // TODO: handle exception >+ } >+ } >+ } >+ >+ public void dsUnregisterTranslationProvider(ITranslationProvider provider, >+ Map<String, String> data) { >+ for (String s : data.get("CATEGORIES").split(";")) { //$NON-NLS-1$//$NON-NLS-2$ >+ unregisterTranslationProvider(s, provider); >+ } >+ } >+} >\ No newline at end of file >Index: src/org/eclipse/e4/core/services/events/IEventBroker.java >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/services/events/IEventBroker.java,v >retrieving revision 1.3 >diff -u -r1.3 IEventBroker.java >--- src/org/eclipse/e4/core/services/events/IEventBroker.java 5 Apr 2010 19:58:06 -0000 1.3 >+++ src/org/eclipse/e4/core/services/events/IEventBroker.java 10 Dec 2010 21:40:24 -0000 >@@ -10,10 +10,9 @@ > *******************************************************************************/ > package org.eclipse.e4.core.services.events; > >-import org.eclipse.e4.core.contexts.IEclipseContext; >- > import java.util.Dictionary; > import java.util.Map; >+import org.eclipse.e4.core.contexts.IEclipseContext; > import org.osgi.service.event.EventHandler; > > /** >@@ -28,6 +27,10 @@ > */ > public interface IEventBroker { > >+ public interface Subscription { >+ public void unsubscribe(); >+ } >+ > /** > * The name of the default event attribute used to pass data. > */ >@@ -78,7 +81,7 @@ > * @return <code>true</code> if this operation was performed successfully; <code>false</code> > * otherwise > */ >- public boolean subscribe(String topic, EventHandler eventHandler); >+ public Subscription subscribe(String topic, EventHandler eventHandler); > > /** > * Subscribe for events on the given topic. >@@ -98,16 +101,7 @@ > * @return <code>true</code> if this operation was performed successfully; <code>false</code> > * otherwise > */ >- public boolean subscribe(String topic, String filter, EventHandler eventHandler, >+ public Subscription subscribe(String topic, String filter, EventHandler eventHandler, > boolean headless); > >- /** >- * Unsubscribe handler previously registered using {@link #subscribe(String, EventHandler)}. >- * >- * @param eventHandler >- * previously registered event handler >- * @return <code>true</code> if this operation was performed successfully; <code>false</code> >- * otherwise >- */ >- public boolean unsubscribe(EventHandler eventHandler); > } >Index: src/org/eclipse/e4/core/services/translation/ITranslationProvider.java >=================================================================== >RCS file: src/org/eclipse/e4/core/services/translation/ITranslationProvider.java >diff -N src/org/eclipse/e4/core/services/translation/ITranslationProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/services/translation/ITranslationProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,51 @@ >+/******************************************************************************* >+ * Copyright (c) 2010 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 Schind<tom.schindl@bestsolution.at> - initial API and implementation >+ ******************************************************************************/ >+package org.eclipse.e4.core.services.translation; >+ >+import java.util.Locale; >+import java.util.MissingResourceException; >+import java.util.ResourceBundle; >+ >+/** >+ * Provides translation for a given category >+ */ >+public interface ITranslationProvider { >+ /** >+ * <p> >+ * Translate the give key in the given locale's text. >+ * </p> >+ * The lookup of the translation the same than the one in >+ * {@link ResourceBundle#getBundle(String, Locale, ClassLoader)} >+ * <ul> >+ * <li>Ls + "_" + Cs + "_" + Vs</li> >+ * <li>Ls + "_" + Cs</li> >+ * <li>Ls</li> >+ * <li>Ld + "_" + Cd + "_" + Vd</li> >+ * <li>Ld + "_" + Cd</li> >+ * <li>Ld</li> >+ * <li>DEFAULT</li> >+ * </ul> >+ * Where Ls, Cs and Vs are the specified locale (language, country, variant) and Ld, Cd and Vd >+ * are the default locale (language, country, variant). >+ * >+ * @param locale >+ * the locale, must not be <code>null</code> >+ * @param category >+ * the category >+ * @param key >+ * the key >+ * @return the translated value must not be <code>null</code> >+ * @throws MissingResourceException >+ * if translation fails because of whatever reason >+ */ >+ public String translate(Locale locale, String category, String key) >+ throws MissingResourceException; >+} >Index: src/org/eclipse/e4/core/services/translation/ITranslationService.java >=================================================================== >RCS file: src/org/eclipse/e4/core/services/translation/ITranslationService.java >diff -N src/org/eclipse/e4/core/services/translation/ITranslationService.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/services/translation/ITranslationService.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,146 @@ >+/******************************************************************************* >+ * Copyright (c) 2010 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 Schind<tom.schindl@bestsolution.at> - initial API and implementation >+ ******************************************************************************/ >+package org.eclipse.e4.core.services.translation; >+ >+import java.util.Locale; >+import java.util.MissingResourceException; >+import java.util.ResourceBundle; >+import org.eclipse.osgi.service.localization.BundleLocalization; >+import org.eclipse.osgi.service.localization.LocaleProvider; >+ >+/** >+ * Service which can be used to translate texts into locale specific ones. >+ * >+ * The lookup of the translation the same than the one in >+ * {@link ResourceBundle#getBundle(String, Locale, ClassLoader)} >+ * <ul> >+ * <li>Ls + "_" + Cs + "_" + Vs</li> >+ * <li>Ls + "_" + Cs</li> >+ * <li>Ls</li> >+ * <li>Ld + "_" + Cd + "_" + Vd</li> >+ * <li>Ld + "_" + Cd</li> >+ * <li>Ld</li> >+ * <li>DEFAULT</li> >+ * </ul> >+ * Where Ls, Cs and Vs are the specified locale (language, country, variant) and Ld, Cd and Vd are >+ * the default locale (language, country, variant). >+ */ >+public interface ITranslationService { >+ /** >+ * Translate the key into the default locale. >+ * <p> >+ * <b>Locale lookup</b> >+ * <ul> >+ * <li>try to use {@link LocaleProvider} service</li> >+ * <li>use {@link Locale#getDefault()}</li> >+ * </ul> >+ * </p> >+ * <p> >+ * <b>Category lookup</b> >+ * <ul> >+ * <li>try to find a {@link ITranslationProvider} for the category</li> >+ * <li>interpret the value as a symbolic bundle name and try to access the >+ * {@link BundleLocalization}</li> >+ * </ul> >+ * </p> >+ * >+ * @param category >+ * the category/bundle name to use >+ * @param key >+ * the key to translate >+ * @return the translated value, must not be <code>null</code> >+ * @throws MissingResourceException >+ * if some problem occurrs while translating >+ */ >+ public String translate(String category, String key) throws MissingResourceException; >+ >+ /** >+ * Translate the key into the default locale. >+ * >+ * <p> >+ * <b>Category lookup</b> >+ * <ul> >+ * <li>try to find a {@link ITranslationProvider} for the category</li> >+ * <li>interpret the value as a symbolic bundle name and try to access the >+ * {@link BundleLocalization}</li> >+ * </ul> >+ * </p> >+ * >+ * @param locale >+ * the locale to use >+ * @param category >+ * the category/bundle name to use >+ * @param key >+ * the key to translate >+ * @return the translated value, must not be <code>null</code> >+ * @throws MissingResourceException >+ * if some problem occurrs while translating >+ */ >+ public String translate(Locale locale, String category, String key) >+ throws MissingResourceException; >+ >+ /** >+ * Translate multiple keys at once. See {@link #translate(String, String)} for a detailed >+ * explanation how the locale and category look up is done >+ * >+ * @param category >+ * the category/bundle name to use >+ * @param keys >+ * the keys >+ * @return the translated keys >+ * @throws MissingResourceException >+ * if one of the keys is not found >+ * @see #translate(String, String) >+ */ >+ public String[] translate(String category, String... keys) throws MissingResourceException; >+ >+ /** >+ * Translate multiple keys at once. See {@link #translate(String, String)} for a detailed >+ * explanation how the category look up is done >+ * >+ * @param locale >+ * the locale to use >+ * @param category >+ * the category/bundle name to use >+ * @param keys >+ * the keys >+ * @return the translated keys >+ * @throws MissingResourceException >+ * if one of the keys is not found >+ * @see #translate(String, String) >+ */ >+ public String[] translate(Locale locale, String category, String... keys) >+ throws MissingResourceException; >+ >+ /** >+ * Register a translation provider for a category >+ * >+ * @param category >+ * the category >+ * @param provider >+ * the provider >+ * @throws IllegalArgumentException >+ * thrown if there's already a provider registered >+ */ >+ public void registerTranslationProvider(String category, ITranslationProvider provider) >+ throws IllegalArgumentException; >+ >+ /** >+ * Unregister a translation provider for a category >+ * >+ * @param category >+ * the category of the provider >+ * @param provider >+ * the provider instance to unregister >+ * @return <code>true</code> if unregistering succeeded >+ */ >+ public boolean unregisterTranslationProvider(String category, ITranslationProvider provider); >+} >\ No newline at end of file >Index: src/org/eclipse/e4/core/services/translation/PropertiesBundleTranslationProvider.java >=================================================================== >RCS file: src/org/eclipse/e4/core/services/translation/PropertiesBundleTranslationProvider.java >diff -N src/org/eclipse/e4/core/services/translation/PropertiesBundleTranslationProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/services/translation/PropertiesBundleTranslationProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,218 @@ >+/******************************************************************************* >+ * Copyright (c) 2004, 2010 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation in org.eclipse.osgi.framework.internal.core.ManifestLocalization >+ * Tom Schindl <tom.schindl@bestsolution.at> - Adjusted for useage out of equinox >+ *******************************************************************************/ >+package org.eclipse.e4.core.services.translation; >+ >+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 class PropertiesBundleTranslationProvider implements ITranslationProvider { >+ final static String DEFAULT_ROOT = getEquinoxRootLocale(); >+ >+ private ClassLoader loader; >+ private String baseName; >+ >+ private final Hashtable<String, BundleResourceBundle> cache = new Hashtable<String, BundleResourceBundle>( >+ 5); >+ >+ public PropertiesBundleTranslationProvider(ClassLoader loader, String baseName) { >+ this.loader = loader; >+ this.baseName = baseName; >+ } >+ >+ 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; >+ } >+ >+ public String translate(Locale locale, String category, String key) { >+ String defaultLocale = Locale.getDefault().toString(); >+ String localeString = locale.toString(); >+ ResourceBundle bundle = getResourceBundle(locale.toString(), >+ defaultLocale.equals(localeString)); >+ return bundle.getString(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 = baseName; >+ 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 = loader.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(); >+ } >+ } >+} >#P org.eclipse.e4.core.tests >Index: .project >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/tests/org.eclipse.e4.core.tests/.project,v >retrieving revision 1.1 >diff -u -r1.1 .project >--- .project 7 Apr 2010 19:48:50 -0000 1.1 >+++ .project 10 Dec 2010 21:40:25 -0000 >@@ -20,6 +20,11 @@ > <arguments> > </arguments> > </buildCommand> >+ <buildCommand> >+ <name>org.eclipse.pde.ds.core.builder</name> >+ <arguments> >+ </arguments> >+ </buildCommand> > </buildSpec> > <natures> > <nature>org.eclipse.pde.PluginNature</nature> >Index: CoreTestSuite.launch >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/tests/org.eclipse.e4.core.tests/CoreTestSuite.launch,v >retrieving revision 1.1 >diff -u -r1.1 CoreTestSuite.launch >--- CoreTestSuite.launch 12 Nov 2010 16:57:20 -0000 1.1 >+++ CoreTestSuite.launch 10 Dec 2010 21:40:25 -0000 >@@ -1,4 +1,4 @@ >-<?xml version="1.0" encoding="UTF-8"?> >+<?xml version="1.0" encoding="UTF-8" standalone="no"?> > <launchConfiguration type="org.eclipse.pde.ui.JunitLaunchConfig"> > <booleanAttribute key="append.args" value="true"/> > <stringAttribute key="application" value="org.eclipse.pde.junit.runtime.coretestapplication"/> >@@ -25,7 +25,7 @@ > <stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/> > <stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/> > <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.e4.core.tests.CoreTestSuite"/> >-<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog"/> >+<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog -console"/> > <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.e4.core.tests"/> > <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.pde.ui.workbenchClasspathProvider"/> > <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xms40m -Xmx384m"/> >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/tests/org.eclipse.e4.core.tests/META-INF/MANIFEST.MF,v >retrieving revision 1.7 >diff -u -r1.7 MANIFEST.MF >--- META-INF/MANIFEST.MF 13 May 2010 14:24:08 -0000 1.7 >+++ META-INF/MANIFEST.MF 10 Dec 2010 21:40:25 -0000 >@@ -13,7 +13,8 @@ > org.eclipse.osgi.services;bundle-version="3.2.100", > com.google.code.atinject.tck;bundle-version="1.0.0", > org.eclipse.equinox.registry;bundle-version="3.5.0", >- org.eclipse.core.tests.harness;bundle-version="3.6.0" >+ org.eclipse.core.tests.harness;bundle-version="3.6.0", >+ org.eclipse.e4.core.services;bundle-version="0.9.1" > Bundle-ActivationPolicy: lazy > Bundle-RequiredExecutionEnvironment: J2SE-1.5 > Import-Package: javax.annotation;version="1.0.0", >@@ -26,4 +27,4 @@ > org.eclipse.e4.core.internal.tests.di.extensions;x-internal:=true, > org.eclipse.e4.core.internal.tests.manual;x-internal:=true, > org.eclipse.e4.core.tests >-Service-Component: OSGI-INF/adder.xml >+Service-Component: OSGI-INF/adder.xml, OSGI-INF/customtranslations.xml >Index: OSGI-INF/customtranslations.xml >=================================================================== >RCS file: OSGI-INF/customtranslations.xml >diff -N OSGI-INF/customtranslations.xml >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ OSGI-INF/customtranslations.xml 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,8 @@ >+<?xml version="1.0" encoding="UTF-8"?> >+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.e4.core.tests.customtranslations"> >+ <implementation class="org.eclipse.e4.core.internal.tests.translation.CustomTranslationProvider"/> >+ <service> >+ <provide interface="org.eclipse.e4.core.services.translation.ITranslationProvider"/> >+ </service> >+ <property name="CATEGORIES" type="String" value="org.eclipse.e4.core.tests.customcat"/> >+</scr:component> >Index: OSGI-INF/l10n/bundle.properties >=================================================================== >RCS file: OSGI-INF/l10n/bundle.properties >diff -N OSGI-INF/l10n/bundle.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ OSGI-INF/l10n/bundle.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,2 @@ >+key1 = Value En >+key2 = Value En 2 >\ No newline at end of file >Index: OSGI-INF/l10n/bundle_de.properties >=================================================================== >RCS file: OSGI-INF/l10n/bundle_de.properties >diff -N OSGI-INF/l10n/bundle_de.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ OSGI-INF/l10n/bundle_de.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,2 @@ >+key1 = Wert De >+key2 = Wert De 2 >\ No newline at end of file >Index: OSGI-INF/l10n/messages.properties >=================================================================== >RCS file: OSGI-INF/l10n/messages.properties >diff -N OSGI-INF/l10n/messages.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ OSGI-INF/l10n/messages.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,2 @@ >+key1 = Cust Value En >+key2 = Cust Value En 2 >\ No newline at end of file >Index: OSGI-INF/l10n/messages_de.properties >=================================================================== >RCS file: OSGI-INF/l10n/messages_de.properties >diff -N OSGI-INF/l10n/messages_de.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ OSGI-INF/l10n/messages_de.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,2 @@ >+key1 = Cust Wert De >+key2 = Cust Wert De 2 >\ No newline at end of file >Index: build.properties >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/tests/org.eclipse.e4.core.tests/build.properties,v >retrieving revision 1.3 >diff -u -r1.3 build.properties >--- build.properties 28 Apr 2010 14:28:49 -0000 1.3 >+++ build.properties 10 Dec 2010 21:40:26 -0000 >@@ -1,6 +1,7 @@ >-source.. = src/ > output.. = bin/ > bin.includes = META-INF/,\ > .,\ > test.xml,\ >- OSGI-INF/ >+ OSGI-INF/,\ >+ OSGI-INF/customtranslations.xml >+source.. = src/ >Index: src/org/eclipse/e4/core/internal/tests/translation/CustomTranslationProvider.java >=================================================================== >RCS file: src/org/eclipse/e4/core/internal/tests/translation/CustomTranslationProvider.java >diff -N src/org/eclipse/e4/core/internal/tests/translation/CustomTranslationProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/internal/tests/translation/CustomTranslationProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,12 @@ >+package org.eclipse.e4.core.internal.tests.translation; >+ >+import org.eclipse.e4.core.services.translation.PropertiesBundleTranslationProvider; >+ >+public class CustomTranslationProvider extends >+ PropertiesBundleTranslationProvider { >+ public CustomTranslationProvider() { >+ super(CustomTranslationProvider.class.getClassLoader(), >+ "OSGI-INF/l10n/messages"); >+ >+ } >+} >Index: src/org/eclipse/e4/core/internal/tests/translation/TestTranslationService.java >=================================================================== >RCS file: src/org/eclipse/e4/core/internal/tests/translation/TestTranslationService.java >diff -N src/org/eclipse/e4/core/internal/tests/translation/TestTranslationService.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/internal/tests/translation/TestTranslationService.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,132 @@ >+package org.eclipse.e4.core.internal.tests.translation; >+ >+import java.util.Locale; >+import java.util.MissingResourceException; >+ >+import org.eclipse.e4.core.internal.tests.CoreTestsActivator; >+import org.eclipse.e4.core.services.translation.ITranslationService; >+import org.osgi.framework.BundleContext; >+import org.osgi.framework.ServiceReference; >+ >+import junit.framework.TestCase; >+ >+public class TestTranslationService extends TestCase { >+ private ITranslationService service; >+ >+ @Override >+ protected void setUp() throws Exception { >+ super.setUp(); >+ Locale.setDefault(Locale.ENGLISH); >+ } >+ >+ public ITranslationService getService() { >+ if (service == null) { >+ BundleContext ctx = CoreTestsActivator.getDefault() >+ .getBundleContext(); >+ ServiceReference<ITranslationService> ref = ctx >+ .getServiceReference(ITranslationService.class); >+ service = ctx.getService(ref); >+ } >+ >+ return service; >+ } >+ >+ public void testBundleTranslations() { >+ String category = "org.eclipse.e4.core.tests"; >+ ITranslationService s = getService(); >+ >+ assertEquals("Value En", s.translate(category, "key1")); >+ assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1")); >+ >+ Locale.setDefault(Locale.GERMAN); >+ >+ assertEquals("Wert De", s.translate(category, "key1")); >+ assertEquals("Wert De", s.translate(Locale.GERMAN, category, "key1")); >+ >+ assertEquals("Wert De", >+ s.translate(Locale.FRENCH, "org.eclipse.e4.core.tests", "key1")); >+ >+ assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1")); >+ >+ try { >+ s.translate(category, "unknown"); >+ fail("Unknown key has to fail with MissingResourceException"); >+ } catch (MissingResourceException e) { >+ } >+ >+ try { >+ s.translate(Locale.ENGLISH, category, "unknown"); >+ fail("Unknown key has to fail with MissingResourceException"); >+ } catch (MissingResourceException e) { >+ } >+ >+ assertEquals(2, s.translate(category, "key1", "key2").length); >+ assertEquals("Wert De", s.translate(category, "key1", "key2")[0]); >+ assertEquals("Wert De 2", s.translate(category, "key1", "key2")[1]); >+ >+ assertEquals(2, >+ s.translate(Locale.FRENCH, category, "key1", "key2").length); >+ assertEquals("Wert De", >+ s.translate(Locale.FRENCH, category, "key1", "key2")[0]); >+ assertEquals("Wert De 2", >+ s.translate(Locale.FRENCH, category, "key1", "key2")[1]); >+ >+ assertEquals(2, >+ s.translate(Locale.ENGLISH, category, "key1", "key2").length); >+ assertEquals("Value En", >+ s.translate(Locale.ENGLISH, category, "key1", "key2")[0]); >+ assertEquals("Value En 2", >+ s.translate(Locale.ENGLISH, category, "key1", "key2")[1]); >+ } >+ >+ public void testTranslationProvider() { >+ String category = "org.eclipse.e4.core.tests.customcat"; >+ ITranslationService s = getService(); >+ >+ assertEquals("Cust Value En", s.translate(category, "key1")); >+ assertEquals("Cust Value En", >+ s.translate(Locale.ENGLISH, category, "key1")); >+ >+ Locale.setDefault(Locale.GERMAN); >+ >+ assertEquals("Cust Wert De", s.translate(category, "key1")); >+ assertEquals("Cust Wert De", >+ s.translate(Locale.GERMAN, category, "key1")); >+ >+ assertEquals("Cust Wert De", >+ s.translate(Locale.FRENCH, category, "key1")); >+ >+ assertEquals("Cust Value En", >+ s.translate(Locale.ENGLISH, category, "key1")); >+ >+ try { >+ s.translate(category, "unknown"); >+ fail("Unknown key has to fail with MissingResourceException"); >+ } catch (MissingResourceException e) { >+ } >+ >+ try { >+ s.translate(Locale.ENGLISH, category, "unknown"); >+ fail("Unknown key has to fail with MissingResourceException"); >+ } catch (MissingResourceException e) { >+ } >+ >+ assertEquals(2, s.translate(category, "key1", "key2").length); >+ assertEquals("Cust Wert De", s.translate(category, "key1", "key2")[0]); >+ assertEquals("Cust Wert De 2", s.translate(category, "key1", "key2")[1]); >+ >+ assertEquals(2, >+ s.translate(Locale.FRENCH, category, "key1", "key2").length); >+ assertEquals("Cust Wert De", >+ s.translate(Locale.FRENCH, category, "key1", "key2")[0]); >+ assertEquals("Cust Wert De 2", >+ s.translate(Locale.FRENCH, category, "key1", "key2")[1]); >+ >+ assertEquals(2, >+ s.translate(Locale.ENGLISH, category, "key1", "key2").length); >+ assertEquals("Cust Value En", >+ s.translate(Locale.ENGLISH, category, "key1", "key2")[0]); >+ assertEquals("Cust Value En 2", >+ s.translate(Locale.ENGLISH, category, "key1", "key2")[1]); >+ } >+} >Index: src/org/eclipse/e4/core/tests/CoreTestSuite.java >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/tests/CoreTestSuite.java,v >retrieving revision 1.17 >diff -u -r1.17 CoreTestSuite.java >--- src/org/eclipse/e4/core/tests/CoreTestSuite.java 1 Oct 2010 19:28:27 -0000 1.17 >+++ src/org/eclipse/e4/core/tests/CoreTestSuite.java 10 Dec 2010 21:40:27 -0000 >@@ -48,6 +48,7 @@ > import org.eclipse.e4.core.internal.tests.di.extensions.InjectionEventTest; > import org.eclipse.e4.core.internal.tests.di.extensions.InjectionMixedSuppliersTest; > import org.eclipse.e4.core.internal.tests.di.extensions.InjectionPreferencesTest; >+import org.eclipse.e4.core.internal.tests.translation.TestTranslationService; > > public class CoreTestSuite extends TestSuite { > public static Test suite() { >@@ -96,5 +97,8 @@ > addTestSuite(Bug317183Test.class); > addTestSuite(DependenciesLeakTest.class); > addTestSuite(ActivationInjectionTest.class); >+ >+ // Translation >+ addTestSuite(TestTranslationService.class); > } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 331260
:
183968
|
184155
| 185001 |
185457
|
186664
|
186665
|
186735
|
186742
|
186743
|
186844