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 186742 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 alternative v.2
patch translation Oleg 2.txt (text/plain), 15.70 KB, created by
Oleg Besedin
on 2011-01-13 11:13:30 EST
(
hide
)
Description:
Patch alternative v.2
Filename:
MIME Type:
Creator:
Oleg Besedin
Created:
2011-01-13 11:13:30 EST
Size:
15.70 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.e4.core.services >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 13 Jan 2011 16:12:33 -0000 >@@ -11,8 +11,11 @@ > 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.service.log;version="1.3.0", >+ org.osgi.service.packageadmin;version="1.2.0", > org.osgi.util.tracker;version="1.4.2" > Require-Bundle: org.eclipse.equinox.common;bundle-version="3.4.0", > org.eclipse.equinox.preferences;bundle-version="3.3.0", >@@ -42,6 +45,7 @@ > 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 >Index: src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java >=================================================================== >RCS file: src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java >diff -N src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/internal/services/BundleTranslationProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,113 @@ >+/******************************************************************************* >+ * Copyright (c) 2011 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 >+ ******************************************************************************/ >+package org.eclipse.e4.core.internal.services; >+ >+import java.net.URI; >+import java.net.URISyntaxException; >+import java.util.MissingResourceException; >+import java.util.ResourceBundle; >+import org.eclipse.e4.core.services.translation.TranslationService; >+import org.eclipse.osgi.service.localization.BundleLocalization; >+import org.osgi.framework.Bundle; >+import org.osgi.service.log.LogService; >+import org.osgi.service.packageadmin.PackageAdmin; >+ >+// There is no replacement for PackageAdmin#getBundles() >+@SuppressWarnings("deprecation") >+public class BundleTranslationProvider extends TranslationService { >+ >+ /** >+ * The schema identifier used for Eclipse platform references >+ */ >+ final private static String PLATFORM_SCHEMA = "platform"; //$NON-NLS-1$ >+ final private static String PLUGIN_SEGMENT = "/plugin/"; //$NON-NLS-1$ >+ final private static String FRAGMENT_SEGMENT = "/fragment/"; //$NON-NLS-1$ >+ >+ /** >+ * Prefix for keys to be translated >+ */ >+ private static final String KEY_PREFIX = "%"; //$NON-NLS-1$ >+ >+ /** >+ * Prefix that aborts translation >+ */ >+ private static final String KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$ >+ >+ @Override >+ public String translate(String key, String contributorURI) { >+ Bundle bundle = getBundle(contributorURI); >+ if (bundle == null) >+ return key; >+ BundleLocalization localizationService = ServicesActivator.getDefault() >+ .getLocalizationService(); >+ if (localizationService == null) >+ return key; >+ // TBD locale might contain extra information, such as calendar specification >+ // that might need to be removed. >+ ResourceBundle resourceBundle = localizationService.getLocalization(bundle, locale); >+ return getResourceString(key, resourceBundle); >+ } >+ >+ private Bundle getBundle(String contributorURI) { >+ if (contributorURI == null) >+ return null; >+ URI uri; >+ try { >+ uri = new URI(contributorURI); >+ } catch (URISyntaxException e) { >+ LogService logService = ServicesActivator.getDefault().getLogService(); >+ if (logService != null) >+ logService.log(LogService.LOG_ERROR, "Invalid contributor URI: " + contributorURI); //$NON-NLS-1$ >+ return null; >+ } >+ if (!PLATFORM_SCHEMA.equals(uri.getScheme())) >+ return null; // not implemented >+ String bundleName = uri.getPath(); >+ if (bundleName.startsWith(PLUGIN_SEGMENT)) >+ bundleName = bundleName.substring(PLUGIN_SEGMENT.length()); >+ else if (bundleName.startsWith(FRAGMENT_SEGMENT)) >+ bundleName = bundleName.substring(FRAGMENT_SEGMENT.length()); >+ PackageAdmin packageAdmin = ServicesActivator.getDefault().getPackageAdmin(); >+ Bundle[] bundles = packageAdmin.getBundles(bundleName, null); >+ if (bundles == null) >+ return null; >+ // Return the first bundle that is not installed or uninstalled >+ for (int i = 0; i < bundles.length; i++) { >+ if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { >+ return bundles[i]; >+ } >+ } >+ return null; >+ } >+ >+ public String getResourceString(String value, ResourceBundle resourceBundle) { >+ String s = value.trim(); >+ if (!s.startsWith(KEY_PREFIX, 0)) >+ return s; >+ if (s.startsWith(KEY_DOUBLE_PREFIX, 0)) >+ return s.substring(1); >+ >+ int ix = s.indexOf(' '); >+ String key = ix == -1 ? s : s.substring(0, ix); >+ String dflt = ix == -1 ? s : s.substring(ix + 1); >+ >+ if (resourceBundle == null) >+ return dflt; >+ >+ try { >+ return resourceBundle.getString(key.substring(1)); >+ } catch (MissingResourceException e) { >+ // this will avoid requiring a bundle access on the next lookup >+ return dflt; >+ } >+ } >+ >+} >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 13 Jan 2011 16:12:33 -0000 >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2009 IBM Corporation and others. >+ * Copyright (c) 2009, 2011 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 >@@ -10,16 +10,22 @@ > *******************************************************************************/ > package org.eclipse.e4.core.internal.services; > >-import org.eclipse.osgi.service.debug.DebugOptions; >+import org.eclipse.osgi.service.localization.BundleLocalization; > import org.osgi.framework.BundleActivator; > import org.osgi.framework.BundleContext; >+import org.osgi.service.log.LogService; >+import org.osgi.service.packageadmin.PackageAdmin; > import org.osgi.util.tracker.ServiceTracker; > >+//there is no replacement for PackageAdmin#getBundles() >+@SuppressWarnings("deprecation") > public class ServicesActivator implements BundleActivator { > > static private ServicesActivator defaultInstance; > private BundleContext bundleContext; >- private ServiceTracker debugTracker = null; >+ private ServiceTracker<PackageAdmin, PackageAdmin> pkgAdminTracker; >+ private ServiceTracker<LogService, LogService> logTracker; >+ private ServiceTracker<BundleLocalization, BundleLocalization> localizationTracker = null; > > public ServicesActivator() { > defaultInstance = this; >@@ -34,25 +40,51 @@ > } > > public void stop(BundleContext context) throws Exception { >- if (debugTracker != null) { >- debugTracker.close(); >- debugTracker = null; >+ if (pkgAdminTracker != null) { >+ pkgAdminTracker.close(); >+ pkgAdminTracker = null; >+ } >+ if (localizationTracker != null) { >+ localizationTracker.close(); >+ localizationTracker = null; >+ } >+ if (logTracker != null) { >+ logTracker.close(); >+ logTracker = null; > } > bundleContext = null; > } > >- public boolean getBooleanDebugOption(String option, boolean defaultValue) { >- if (debugTracker == null) { >- debugTracker = new ServiceTracker(bundleContext, DebugOptions.class.getName(), null); >- debugTracker.open(); >- } >- DebugOptions options = (DebugOptions) debugTracker.getService(); >- if (options != null) { >- String value = options.getOption(option); >- if (value != null) >- return value.equalsIgnoreCase("true"); //$NON-NLS-1$ >+ public PackageAdmin getPackageAdmin() { >+ if (pkgAdminTracker == null) { >+ if (bundleContext == null) >+ return null; >+ pkgAdminTracker = new ServiceTracker<PackageAdmin, PackageAdmin>(bundleContext, >+ PackageAdmin.class, null); >+ pkgAdminTracker.open(); > } >- return defaultValue; >+ return (PackageAdmin) pkgAdminTracker.getService(); > } > >+ public LogService getLogService() { >+ if (logTracker == null) { >+ if (bundleContext == null) >+ return null; >+ logTracker = new ServiceTracker<LogService, LogService>(bundleContext, >+ LogService.class, null); >+ logTracker.open(); >+ } >+ return logTracker.getService(); >+ } >+ >+ public BundleLocalization getLocalizationService() { >+ if (localizationTracker == null) { >+ if (bundleContext == null) >+ return null; >+ localizationTracker = new ServiceTracker<BundleLocalization, BundleLocalization>( >+ bundleContext, BundleLocalization.class, null); >+ localizationTracker.open(); >+ } >+ return localizationTracker.getService(); >+ } > } >Index: src/org/eclipse/e4/core/services/translation/TranslationProviderFactory.java >=================================================================== >RCS file: src/org/eclipse/e4/core/services/translation/TranslationProviderFactory.java >diff -N src/org/eclipse/e4/core/services/translation/TranslationProviderFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/services/translation/TranslationProviderFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,37 @@ >+/******************************************************************************* >+ * Copyright (c) 2011 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 >+ ******************************************************************************/ >+package org.eclipse.e4.core.services.translation; >+ >+import org.eclipse.e4.core.contexts.ContextInjectionFactory; >+import org.eclipse.e4.core.contexts.IEclipseContext; >+import org.eclipse.e4.core.internal.services.BundleTranslationProvider; >+ >+/** >+ * Factory for translation providers. >+ */ >+final public class TranslationProviderFactory { >+ >+ private TranslationProviderFactory() { >+ // prevents instantiation >+ } >+ >+ /** >+ * Returns default bundle-based translation provider. >+ * >+ * @param context >+ * the context for the translation provider >+ * @return bundle-based translation provider >+ */ >+ static public TranslationService bundleTranslationService(IEclipseContext context) { >+ return ContextInjectionFactory.make(BundleTranslationProvider.class, context); >+ } >+ >+} >Index: src/org/eclipse/e4/core/services/translation/TranslationService.java >=================================================================== >RCS file: src/org/eclipse/e4/core/services/translation/TranslationService.java >diff -N src/org/eclipse/e4/core/services/translation/TranslationService.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/e4/core/services/translation/TranslationService.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,51 @@ >+/******************************************************************************* >+ * Copyright (c) 2011 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 >+ ******************************************************************************/ >+package org.eclipse.e4.core.services.translation; >+ >+import javax.inject.Inject; >+import javax.inject.Named; >+ >+/** >+ * Provides localization service. >+ */ >+abstract public class TranslationService { >+ >+ /** >+ * The name of the context variable with locale information >+ */ >+ static public final String LOCALE = "org.eclipse.e4.core.locale"; //$NON-NLS-1$ >+ >+ @Inject >+ @Named(LOCALE) >+ protected String locale; >+ >+ @Inject >+ public TranslationService() { >+ // placeholder >+ } >+ >+ /** >+ * Translates the key from the contributor. If translation can not be found, the original key >+ * should be returned. >+ * <p> >+ * This method is expected to be overridden by the implementors. >+ * </p> >+ * >+ * @param key >+ * the key >+ * @param contributorURI >+ * URI of the contributor >+ * @return localized value, or the original key if the translation can not be done >+ */ >+ public String translate(String key, String contributorURI) { >+ return key; >+ } >+} >#P org.eclipse.e4.ui.workbench.swt >Index: src/org/eclipse/e4/ui/internal/workbench/swt/E4Application.java >=================================================================== >RCS file: /cvsroot/eclipse/e4/org.eclipse.e4.ui/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/E4Application.java,v >retrieving revision 1.30 >diff -u -r1.30 E4Application.java >--- src/org/eclipse/e4/ui/internal/workbench/swt/E4Application.java 29 Nov 2010 20:13:34 -0000 1.30 >+++ src/org/eclipse/e4/ui/internal/workbench/swt/E4Application.java 13 Jan 2011 16:12:33 -0000 >@@ -18,6 +18,7 @@ > import java.io.OutputStream; > import java.net.URL; > import java.util.List; >+import java.util.Locale; > import java.util.Properties; > import org.eclipse.core.databinding.observable.Realm; > import org.eclipse.core.runtime.Assert; >@@ -35,6 +36,8 @@ > import org.eclipse.e4.core.services.log.ILoggerProvider; > import org.eclipse.e4.core.services.log.Logger; > import org.eclipse.e4.core.services.statusreporter.StatusReporter; >+import org.eclipse.e4.core.services.translation.TranslationService; >+import org.eclipse.e4.core.services.translation.TranslationProviderFactory; > import org.eclipse.e4.ui.internal.workbench.ActiveChildLookupFunction; > import org.eclipse.e4.ui.internal.workbench.ActivePartLookupFunction; > import org.eclipse.e4.ui.internal.workbench.DefaultLoggerProvider; >@@ -428,6 +431,15 @@ > appContext > .set(Logger.class.getName(), ContextInjectionFactory.make( > WorkbenchLogger.class, appContext)); >+ >+ // translation >+ String locale = Locale.getDefault().toString(); >+ appContext.set(TranslationService.LOCALE, locale); >+ TranslationService bundleTranslationProvider = TranslationProviderFactory >+ .bundleTranslationService(appContext); >+ appContext.set(TranslationService.class, >+ bundleTranslationProvider); >+ > appContext.set(Adapter.class.getName(), > ContextInjectionFactory.make(EclipseAdapter.class, appContext)); >
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