java - 将 Hibernate Validator 与 GWT 结合使用时如何国际化错误消息?

标签 java hibernate gwt internationalization hibernate-validator

我正在寻求一些有关如何在使用 GWT 时国际化我的 hibernate 验证消息的帮助,以便它可以处理客户端和服务器消息。

根据this question ,我应该将 ValidationMessages.properties 文件放在我的类路径中,或使用以下代码:

Validation
    .byProvider(HibernateValidator.class)
    .configure()
    .messageInterpolator(
        new ResourceBundleMessageInterpolator(
            new PlatformResourceBundleLocator("com.mycompany.Messages")))
    .buildValidatorFactory()
    .getValidator();`

但是这个方法可以在客户端使用 GWT 代码工作吗?我应该怎样做才能使其在客户端工作?

最佳答案

请查看GWT Internationalize .

如果您想使用 GWT 对消息进行国际化,请检查 Static String InternationalizationDynamic String Internationalization .
我用过GWT ConstantsWithLookup对于常量数据(例如按钮的文本、标签的文本)。
我用GWT Messages用于我的消息数据(例如警报、警告、确认、提示对话框)。

不要忘记在 gwt.xml 文件中添加配置,如下所示...

<extend-property name="locale" values="en"/>
<extend-property name="locale" values="ja"/>
<!-- set the fallback for locale default Japan  -->
<set-property-fallback name="locale" value="ja"/>
........

我使用 Local en 表示英语,使用 ja 表示日语。我创建了 ListBox 供用户选择他们喜欢的区域设置并使用 Cookie以维持用户偏好。下面是我的示例代码,用于将语言环境保存在 cookie 中,以便在下次打开时进行国际化...

    public static void initializeLocaleBox(final ListBox localeBox) {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    if (currentLocale.equals("default")) {
        currentLocale = "ja";
    }
    String[] localeNames = LocaleInfo.getAvailableLocaleNames();
    for (String localeName : localeNames) {
        if (!localeName.equals("default")) {
            String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
            localeBox.addItem(nativeName, localeName);
            if (localeName.equals(currentLocale)) {
                localeBox.setSelectedIndex(localeBox.getItemCount() - 1);
            }
        }
    }
    localeBox.addChangeHandler(new ChangeHandler() {
        public void onChange(final ChangeEvent event) {
            String localeName = localeBox.getValue(localeBox.getSelectedIndex());

            Date now = new Date();
            long sevenDays = now.getTime();
            // seven days
            sevenDays = sevenDays + (1000 * 60 * 60 * 24 * 7);
            now.setTime(sevenDays);
            Cookies.setCookie("locale", localeName, now);
            UrlBuilder builder = Location.createUrlBuilder().setParameter("locale", localeName);
            Window.Location.replace(builder.buildString());
        }
    });
}

public static boolean isLocaleJapan() {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    return currentLocale != null && currentLocale.equals("ja") ? true : false;
}

public static boolean isLocaleEnglish() {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    return currentLocale != null && currentLocale.equals("en") ? true : false;
}

为了将 URL 更改为本地化,我使用 GWT 进行了如下转换..

public static final String getBaseUrl(final int codesvrPort) {
    String locale = Cookies.getCookie("locale");
    if (locale == null || locale.equals("")) {
        locale = "en";
    }
    String baseUrl = GWT.getHostPageBaseURL() + GWT.getModuleName() + ".html";
    if (GWT.getHostPageBaseURL().contains("localhost")
            || GWT.getHostPageBaseURL().contains("127.0.0.1")) {
        return baseUrl + "?locale=" + locale + "&gwt.codesvr=127.0.0.1:" + codesvrPort;
    }
    return "";
}

Here是对您有用的链接。抱歉链接太多。有一些对你有用的:)

编辑(添加属性文件后缀)

引自 this链接。

Note: Suffixing a properties file

If you've never dealt with internationalization before, you may be wondering why the _de suffix is appended to German properties file. The suffix _de is the standard language tag for the German language (Deutsch). Languages tags are abbreviations that indicate a document or application's locale. In addition to specifying the language, they can also contain a subtag indicating the region of a locale. For example, the language tag for French-speaking Canada is fr_CA.
In GWT, properties files indicate the locale with a language code suffix (just like Java resource bundles). The exception is the properties file for the default locale. When no locale is explicitly set at runtime, the properties file with no language code suffix is used. For StockWatcher, you've specified the default translation with annotations instead of using a default properties file.

关于java - 将 Hibernate Validator 与 GWT 结合使用时如何国际化错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21874349/

相关文章:

java - 在没有 SPSS 许可证的情况下从 Python、Java (groovy/grails) 或 C++ 应用程序访问 SPSS 数据?

Java Hibernate/Spring,做部分匹配查询 ("contains")?

java - Hibernate Envers 修订信息(更改列表)

java - 使用 Mockito 测试 GWTP

gwt - 如何使用新的 GWT MVP 框架?

css - GWT DataGrid 换行高度

java - 在枚举中添加长方法是不是不好的设计?

java - 如何在 java 中准备一个字符串,该字符串最终将传递到 SQL 中进行搜索

java - 这些 setDefaultCloseOperation 语句之间的区别

java - 2个表选择什么关系