Vaadin的国际化与财产文件

时间:2014-08-13 05:43:40

标签: vaadin vaadin7

我在Vaadin找到了useful example国际化。这是online-demo。我尝试使用combobox进行区域设置更改..

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" },
};
}

并呼吁国际化为btnUpdate.setCaption(message.getLocalizeMessage(OfficeMessage.OK));

我的问题是

  1. 我不想用常量java类创建。我想要 使用属性文件创建(例如:OfficeMessage_my.properties, OfficeMessage_en.properties)。我没有找到任何参考资料 这个。请有人帮我解决 xxx.properties 的问题 文件。

  2. 如何处理邮件?我还想用国际化创建消息(可能是动态的)。例如:message.sayHello(currentLoginUser)。当我想使用动态消息时,我该如何理解?

2 个答案:

答案 0 :(得分:1)

我找到了一种通过this链接创建属性文件的常量消息的方法。 最后,我的常量消息类如下,

<强> 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 + '!';
    }
}
}

答案 1 :(得分:0)

查看关于vaadin和Spring框架中的国际化(i18n)的示例解决方案,由Jaroslav H.在'Vaadin 7 Cookbook'中实现