问题描述
我在 Vaadin 中找到了一个有用的例子.这是在线演示.我尝试使用组合框将区域设置更改为 ..
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
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
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
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
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" },
};
}
并调用国际化为 btnUpdate.setCaption(message.getLocalizeMessage(OfficeMessage.OK));
.
我的问题是
我不想用常量 java 类创建.我想要创建属性文件(例如:OfficeMessage_my.properties,OfficeMessage_en.properties).我没有找到任何参考资料这个.请有人帮助我如何处理 xxx.properties文件.
I don't want to create with constant java classes . I would like tocreate with properties files (eg: OfficeMessage_my.properties ,OfficeMessage_en.properties). I didn't find any references forthis. Please somebody help me how can I do with xxx.propertiesfiles.
消息怎么办?我还想用国际化创建消息(可能是动态的).例如:message.sayHello(currentLoginUser)
.当我想使用动态消息时,我该如何解决?
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 ?
推荐答案
我找到了一种通过 链接.最后,我的 Constants 和 Messages 类如下,
I found a way to create both Constants and Messages with properties files via this link.Finally , My Constants and Messages classes as below ,
常量.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 + '!';
}
}
}
这篇关于Vaadin 中的国际化与属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!