Posts Tagged ‘TranslationService’

Luna Update: TranslationService not in context

Saturday, July 26th, 2014

After migrating my Eclipse/E4 application to the latest release Luna, I noticed that the TranslationService is not present anymore within the E4Workbench.getServiceContext(). At least not at the PostContextCreate stage of the application. This is not a big deal, as you can easily create this yourself:

   private static TranslationService TRANSLATIONS = getTopContext().get(TranslationService.class);
   public static TranslationService getTranslationService() {
      if (TRANSLATIONS == null) {
         TRANSLATIONS = getTopContext().get(TranslationService.class);
         if (TRANSLATIONS == null) {
            TRANSLATIONS = ContextInjectionFactory.make(BundleTranslationProvider.class, getTopContext());
            getTopContext().set(TranslationService.class, TRANSLATIONS);
      return TRANSLATIONS;

Eclipse E4: Using the TranslationService

Sunday, December 30th, 2012

This post demonstrates how the TranslationService from Eclipse/E4 can be used in real world applications. First, you need the translation files, (minimum) and e.g. for your special language:

myapp.key1 = A text.
myapp.key2 = Another text.

Store these files in OSGI-INF/l10n folder of your plugin. This way, the Eclipse/E4 translation service will find it.

Next, I recommend to have a static helper class that will retrieve the translation service from Eclipse/E4 and returns the translated keys. It could look like this:

public class E4Utils {
	public static final TranslationService TRANSLATIONS = getTopContext().get(TranslationService.class);
	 * Finds the top Eclipse context.
	 * @return the eclipse context.
	public static IEclipseContext getTopContext() {
		return E4Workbench.getServiceContext();
	 * Translate the given key.
	 * @param key key to translate
	 * @param contributorUri the contributor URI of the translation
	 * @param args variable replacements (will replace {0}, {1},... placeholders)
	public static String translate(String key, String contributorUri, Object... args) {
		if (key == null) return "";
		if (key.charAt(0) != '%') key = '%'+key;
		String rc = TRANSLATIONS.translate(key, contributorUri);
		if ((args == null) || (args.length == 0)) return rc;
		return MessageFormat.format(rc, args);

Thie helper class provides a method translate() which you pass in the key, the contributor URI of your plugin and any additional arguments. This method does a few things for me. It first checks whether the key starts with ‘%’ which is a requirement for the TranslationService. If this ‘%’ is missing, it will be added automatically. Also it will apply the MessageFormat pattern after translation so you can use more sophisticated translation patterns.

The key is very obvious the string that you mentioned in the file. The contributor URI is of form platform:/plugin/symbolicPluginName. The symbolic plugin name can be looked up in the MANIFEST file. To ease the usage of your helper class, create a Plugin class that adds all information required:

public class Plugin {
	public static final String SYMBOLIC_NAME = "rc.rcpplugins.e4";
	public static final String CONTRIBUTOR_URI = "platform:/plugin/"+SYMBOLIC_NAME;
	 * Translate the given key.
	 * @param key key to translate
	 * @param args variable replacements (will replace {0}, {1},... placeholders)
	 * @return translated value
	public static String translate(String key, Object... args) {
		return E4Utils.translate(key, CONTRIBUTOR_URI, args);

Now we can use the translation service at any class in our Eclipse/E4 project. e.g:


I implemented this scheme in all my E4 plugins now (with E4Utils as a central helper in a base plugin) and rapidly translated a medium-sized Eclipse/E4 application within a few hours.

PS: I will publish the base helpers soon as Open Source Eclipse/E4 plugins so you can just install them in your Eclipse instance.