From Dadajax's project web
| LanguageManager | |
| Current version | 0.92 |
| Last updated | 18.01.2009 |
| OS | Platform indepedent |
| License | BSD |
| Homepage | SourceForge.net |
| LanguageEditor | |
| Current version | 0.932 |
| Last updated | 14.11.2008 |
| OS | Platform indepedent |
| License | BSD |
| Homepage | SourceForge.net |
Project EasyLocalization was created because I needed some easy and light tool for creating multi-language Java applications. Because I wasn't succesful with searching such a tool, I decided to create EasyLocalization project. This project is divided into two parts:
Contents |
LanguageManager
LanguageManager is Java library, which offer possibility to translate text strings stored in external XML files.
LanguageEditor
LanguageEditor is tool, which offer easy way to edit language files. Text strings are stored in XML files and you can edit it in LanguageEditor. You can also create new languages and text strings.
Screenshots
Download
You can download EasyLocalization from SourceForge.net. Whole project is released as open source under BSD licence. So you can look at project source codes and if you want, you can edit them to fit your needs.
This project is totaly free, but if you consider this project helpful, you can make me a favor by donation. Even small gift makes me happy ;)
Tutorial
In this short tutorial I will try to explain, how to create multilanguage Java application. For that purpose you need only Java SDK and library LanguageManager.jar.
If you want to use library LanguageManager.jar, you must add it into your build path. In Eclipse IDE you can do this by selecting Project -> Properties. In list select Java Build Path and then in Libraries tab select Add External JARs. In dialog select path, where you have LanguageManager.jar.
From now, you can use in your project LanguageManager. Now I will explain, how to use it. LanguageManager is only interface. Class extending LanguageManager is called LanguageManagerImpl. LanguageManagerImpl is designed to be a Singleton, so there can be only one instance of this class. Because it have private constructor, you must use LanguageManagerImpl.getIntance() to get instance of this class.
If you will need to get localized string, only what you must do is call function localize or just loc. These two functions are exactly the same. Only reason for loc function is to have shorter and nicer version :) These functions have one String parameter, where you put string, which you need translate. Function returns localized string, or in case there isn't localization for this String, it returns original string surrounded by char "?".
Creating file containing localized strings
For easy creating that files is here LanguageEditor. You can easily add or edit strings and languages. For our test application we will need strings for window's title, label and two buttons. There are strings, which will represent these controls:
- windows_title
- label_text
- button_cancel_text
- button_ok_text
If you will use LanguageEditor, you will get some like this:
< ?xml version="1.0" encoding="UTF-8" standalone="no"?>
<languages>
<english>
<window_title>Localized application</window_title>
<label_text>This is example of localized application.</label_text>
<button_cancel_text>Cancel</button_cancel_text>
<button_ok_text>Apply</button_ok_text>
</english>
<czech>
<window_title>Lokalizovaná aplikace</window_title>
<label_text>Toto je ukázka lokalizované aplikace</label_text>
<button_cancel_text>Zrušit</button_cancel_text>
<button_ok_text>Potvrdit</button_ok_text>
</czech>
</languages>
As you can see, I created two languages: Czech and English. Every language contain these four strings and their localization.
Using localized strings in application
Now we have localization for these four strings in XML file. Default directory for localization XML file is ./res/. So if you have this file somewhere else, you need to call method loadLanguages(file). Here is code for example application:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import net.dadajax.languagemanager.LanguageManager;
import net.dadajax.languagemanager.LanguageManagerImpl;
public class LanguageExample {
public final static String WINDOW_TITLE = "window_title";
public final static String LABEL_TEXT = "label_text";
public final static String BUTTON_OK_TEXT = "button_ok_text";
public final static String BUTTON_CANCEL_TEXT = "button_cancel_text";
/**
* @param args
*/
public static void main(String[] args) {
// get instance of LanguageManagerImpl
LanguageManager lm = LanguageManagerImpl.getInstance();
// set language to czech
lm.setLanguage("czech");
// Create GUI
JFrame mainWindow = new JFrame();
mainWindow.setTitle(lm.loc(WINDOW_TITLE));
mainWindow.setLayout(new FlowLayout());
JLabel label = new JLabel();
label.setText(lm.loc(LABEL_TEXT));
mainWindow.add(label);
JButton buttonOk = new JButton();
buttonOk.setText(lm.loc(BUTTON_OK_TEXT));
mainWindow.add(buttonOk);
JButton buttonCancel = new JButton();
buttonCancel.setText(lm.loc(BUTTON_CANCEL_TEXT));
mainWindow.add(buttonCancel);
mainWindow.setVisible(true);
mainWindow.pack();
}
}
If you will compile and run this code, you will see something like this:
You can change language to English by changing code above to this:
lm.setLanguage("english");
Now application will looks like this:
New feature: strings importing
From version 0.9.4 have LanguageEditor cool feature, which offer simple strings importing. From example above we used in code these constants:
public final static String WINDOW_TITLE = "window_title"; public final static String LABEL_TEXT = "label_text"; public final static String BUTTON_OK_TEXT = "button_ok_text"; public final static String BUTTON_CANCEL_TEXT = "button_cancel_text";
Now if you want to get these strings into LanguageEditor to translation, you can simply select Import from .java file from Editor's menu. In our case it will import these strings:
- windows_title
- label_text
- button_cancel_text
- button_ok_text
| Languages |
English • Česky |



