vogella.de

Follow me on twitter
About Lars Vogel
Flattr this

Eclipse RCP and Plugin Internationalization - Tutorial

Lars Vogel

Version 0.2

10.08.2010

Revision History
Revision 0.116.08.2009Lars Vogel
Created
Revision 0.210.08.2010Lars Vogel
bugfixes and enhancements

Eclipse Internationalization

This article describes how to externalize your strings in Eclipse RCP and Eclipse plugins and how to support different languages.

This article is based on Eclipse 3.6 (Eclipse Helios).


Table of Contents

1. Internationalization - i18n
2. Example
2.1. Project
2.2. Internationalize plugin.xml
2.3. Externalize strings in the source code
3. Thank you
4. Questions and Discussion
5. Links and Literature
5.1. Source Code
5.2. Eclipse Resources
5.3. vogella Resources

1. Internationalization - i18n

The process of preparing an application for beeing translated into several languages is called internationalization which is typically

Within Eclipse RCP and Eclipse plugins two elements are relevant for translation, entries in the plugin.mxl and text in the source code. abbriviated via i18n. This article describes how to internationalize both of them.

2. Example

Tip

You can use the runtime parameter -nl to pass the parameters for the selected language. This is useful for testing. Per default the language of the user is used.

2.1. Project

Create a new Eclipse RCP application "de.vogella.rcp.i18n" based on the "RCP application with a view" template. Have a look at the extension point "org.eclipse.ui.views" and validate that the name of the View is hard coded to "View".

2.2. Internationalize plugin.xml

Create the file "plugin.properties" in your plugin. This file can be referred to by the plugin.xml for strings. Add the following entry to this file.

				
View = Das hier ist Deutsch

			

To make OSGi aware that you want to use internationalization add the entry "Bundle-Localization: plugin" to your "MANIFEST.MF" file.

				
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: I18n
Bundle-SymbolicName: de.vogella.rcp.i18n; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: de.vogella.rcp.i18n.Activator
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Localization: plugin

			

Now you can use the keys from plugin.properties in your plugin.xml via "%key".

				
<extension
       point="org.eclipse.ui.views">
    <view
         name="%View"
         class="de.vogella.rcp.i18n.View"
         id="de.vogella.rcp.i18n.view">
    </view>
</extension>
			

You can now create another plugin.properties file with the language key included, e.g. "plugin_en.properties". If the user language is set to english this file will be used.

				
View = Lars Test

			

2.3. Externalize strings in the source code

Change "View.java"to the following.

				
package de.vogella.rcp.i18n;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {
	public void createPartControl(Composite parent) {
		Label label = new Label(parent, SWT.BORDER);
		label.setText("This is a hardcoded text");
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
}

			

Right mouse-click on the source code. Select Source -> Externalize Strings. Select the strings which should be externalized

Press finished.

As a result a class Message.java is created which contains static member variables which can be used in the coding as String replacements. The message.properties contain the real strings. This has the advantages that you can use the Java tools to search for the usage of a string.

Copy message.properties to message_de.properties or message_en.properties to support different strings for different languages.

3. Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by recommending this tutorial to other people.

Flattr this

4. Questions and Discussion

Before posting questions, please see the vogella FAQ . If you have questions or find an error in this article please use the www.vogella.de Google Group . I have created a short list how to create good questions which might also help you. .

5. Links and Literature

5.1. Source Code

Source Code of Examples

5.2. Eclipse Resources

http://www.eclipse.org/articles/Article-SWT-graphics/SWT_graphics.html SWT Graphics

http://www-128.ibm.com/developerworks/opensource/library/os-ecllink// View linking, e.g. how can view 1 react to selection changes in view 2

http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html Understanding Layout in SWT, updated version by Wayne Beaton

http://www.eclipse.org/articles/article.php?file=Article-AddingHelpToRCP/index.html Adding help to an Eclipse RCP application

http://wiki.eclipse.org/index.php/JFaceSnippets JFace Snippets (Code Examples for certain tasks)

http://www.eclipse.org/swt/snippets/ SWT Snippets (Code Examples for certain tasks)