问题
I found a useful example for internationalization in Vaadin. Here is online-demo for this. I tried with combobox for locale change as ..
private Locale localeMyanmar, localeEnglish;
private static final String LOCALE_COOKIE = "locale";
.......
localeMyanmar = new Locale("my", "Burmese");
localeEnglish = Locale.ENGLISH;
.........
final ComboBox cbLanguage = new ComboBox();
cbLanguage.addStyleName("comboIconCaption");
cbLanguage.setNullSelectionAllowed(false);
cbLanguage.setImmediate(true);
IndexedContainer ic = new IndexedContainer();
ic.addItem("Myanmar");
ic.addItem("English");
cbLanguage.setContainerDataSource(ic);
cbLanguage.setItemIcon("Myanmar", new ThemeResource("img/Myanmar-flag.png"));
cbLanguage.setItemIcon("English", new ThemeResource("img/US-flag.png"));
Cookie localeCookie = getCookieByName(LOCALE_COOKIE);
if (localeCookie != null && localeCookie.getValue() != null) {
if (localeCookie.getValue().equals("my")) {
cbLanguage.setValue("Myanmar");
setLocale(localeMyanmar);
}
else {
cbLanguage.setValue("English");
setLocale(localeEnglish);
}
}
else {
cbLanguage.setValue("Myanmar");
// Create a new cookie , 2678400 = 1 month
localeCookie = createCookie(LOCALE_COOKIE, "my", 2678400);
setLocale(localeMyanmar);
}
message.initializeMessageResource(getLocale());
cbLanguage.addValueChangeListener(new ValueChangeListener() {
public void valueChange(final ValueChangeEvent event) {
if (cbLanguage.getValue().toString().equals("Myanmar")) {
destroyCookieByName(LOCALE_COOKIE);
createCookie(LOCALE_COOKIE, "my", 2678400);
setLocale(localeMyanmar);
}
else {
destroyCookieByName(LOCALE_COOKIE);
createCookie(LOCALE_COOKIE, "en", 2678400);
setLocale(localeEnglish);
}
message.initializeMessageResource(getLocale());
}
});
Message.java
public class Message implements Serializable {
private ResourceBundle i18n;
public final void initializeMessageResource(final Locale locale) {
i18n = ResourceBundle.getBundle(OfficeMessage.class.getName(), locale);
}
public final String getLocalizeMessage(final String key) {
return i18n.getString(key);
}
}
OfficeMessage.java
public class OfficeMessage extends ListResourceBundle implements Serializable {
@Override
protected Object[][] getContents() {
return null;
}
public static String generateId() {
return new Integer(ids++).toString();
}
private static int ids = 0;
// Constants
public static final String OK = generateId();
public static final String CANCEL = generateId();
public static final String SAVE = generateId();
public static final String RESET = generateId();
}
OfficeMessage_my.java
public class OfficeMessage_my extends OfficeMessage {
@Override
public final Object[][] getContents() {
return contents_en;
}
static final Object[][] contents_en = {
// Basic Buttons Text
{ OK, "သဘောတူသည်" },
{ CANCEL, "သဘောမတူပါ" },
{ SAVE, "သိမ်းမည်" },
{ RESET, "နဂိုအတိုင်းပြန်ထားမည်" },
};
}
OfficeMessage_en.java
public class OfficeMessage_en extends OfficeMessage {
@Override
public final Object[][] getContents() {
return contents_en;
}
static final Object[][] contents_en = {
// Basic Buttons Text
{ OK, "OK" },
{ CANCEL, "Cancel" },
{ SAVE, "Save" },
{ RESET, "Reset" },
};
}
And call for internationalize as btnUpdate.setCaption(message.getLocalizeMessage(OfficeMessage.OK));.
My questions are
I don't want to create with constant java classes . I would like to create with properties files (eg: OfficeMessage_my.properties , OfficeMessage_en.properties). I didn't find any references for this. Please somebody help me how can I do with xxx.properties files.
How to do for messages ? I also want to create messages (may be dynamic) with internationalize. For example :
message.sayHello(currentLoginUser). When I want to use dynamic messages , How can I figure it out ?
回答1:
I found a way to create both Constants and Messages with properties files via this link. Finally , My Constants and Messages classes as below ,
Constants.java
public class Constants implements Serializable {
private static final String BUNDLE_NAME = "myProject.i18n.Constants.Constants";
private ResourceBundle i18n;
public final void init(final Locale locale) {
i18n = ResourceBundle.getBundle(BUNDLE_NAME, locale);
}
public final String getConstantValue(final String key) {
try {
return new String(i18n.getString(key).getBytes("ISO-8859-1"), "UTF-8");
}
catch (UnsupportedEncodingException e) {
return '!' + key + '!';
}
catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
Messages.java
public class Messages implements Serializable {
private static final String BASE_NAME = "myProject.i18n.Messages.Messages";
private ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
private Locale locale;
public final void init(final Locale locale) {
this.locale = locale;
messageSource.setBasename(BASE_NAME);
}
public final String getMessageValue(final String key) {
try {
return new String(messageSource.getMessage(key, null, locale).getBytes("ISO-8859-1"), "UTF-8");
}
catch (UnsupportedEncodingException e) {
return '!' + key + '!';
}
catch (NoSuchMessageException e) {
return '!' + key + '!';
}
}
public final String getMessageValue(final String key, final Object[] parameters) {
try {
return new String(messageSource.getMessage(key, parameters, locale).getBytes("ISO-8859-1"), "UTF-8");
}
catch (UnsupportedEncodingException e) {
return '!' + key + '!';
}
catch (NoSuchMessageException e) {
return '!' + key + '!';
}
}
}
回答2:
Checkout a sample solution in regards to Internationalization (i18n) in vaadin and Spring framework, implemented by Jaroslav H. in 'Vaadin 7 Cookbook'
- https://github.com/peholmst/vaadin-spring-webinar/tree/master/i18n
- https://vaadin.com/forum#!/thread/3704190
来源:https://stackoverflow.com/questions/25278825/internationalization-in-vaadin-with-property-file