2

我在 UTF-8 中显示 Hibernate Validator 的自定义 ValidationMessages 时遇到问题。

对于常见的 jsf 消息,我遵循了以下建议:在 JSF 2.0 应用程序中使用 UTF-8 编码的属性文件的 i18n - 我创建了类 Text 并在 faces-config.xml 中使用它。这工作正常。

但是这种方法不适用于 ValidationMessages;UTF-8 中不显示特殊字符。

有人可以给我一些建议吗?非常感谢

4

2 回答 2

0

我已经用同样的方法解决了。Hibernate 验证器在 META-INF/validation.xml 中有配置文件

验证.xml的示例

<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
    <message-interpolator>com.mycompany.validation.utf8.UTF8ResourceBundleMessageInterpolator</message-interpolator>
</validation-config>

UTF8ResourceBundleMessageInterpolator的实现

public class UTF8ResourceBundleMessageInterpolator extends ResourceBundleMessageInterpolator {

    public UTF8ResourceBundleMessageInterpolator() {
        super(new UTF8ResourceBundleLocator(ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES));
    }
}

UTF8ResourceBundleLocator的实现(带有小修复的 PlatformResourceBundleLocator 类的克隆)

public class UTF8ResourceBundleLocator implements ResourceBundleLocator {
    private static final Logger logger = LoggerFactory.getLogger(UTF8ResourceBundleLocator.class);

    protected static final ResourceBundle.Control UTF8_CONTROL = new UTF8Control();

    private final String bundleName;

    public UTF8ResourceBundleLocator(String bundleName) {
        this.bundleName = bundleName;
    }


    /**
     * Search current thread classloader for the resource bundle. If not found,
     * search validator (this) classloader.
     *
     * @param locale The locale of the bundle to load.
     * @return the resource bundle or <code>null</code> if none is found.
     */
    @Override
    public ResourceBundle getResourceBundle(Locale locale) {
        ResourceBundle rb = null;
        ClassLoader classLoader = GetClassLoader.fromContext();
        if (classLoader != null) {
            rb = loadBundle(
                    classLoader, locale, bundleName
                            + " not found by thread local classloader"
            );
        }
        if (rb == null) {
            classLoader = GetClassLoader.fromClass(PlatformResourceBundleLocator.class);
            rb = loadBundle(
                    classLoader, locale, bundleName
                            + " not found by validator classloader"
            );
        }

        return rb;
    }

    private ResourceBundle loadBundle(ClassLoader classLoader, Locale locale, String message) {
        ResourceBundle rb = null;
        try {
            rb = ResourceBundle.getBundle(
                    bundleName, locale,
                    classLoader, UTF8_CONTROL
            );
        } catch (MissingResourceException ignored) {
            logger.trace(message);
        }
        return rb;
    }

    private static class GetClassLoader implements PrivilegedAction<ClassLoader> {
        private final Class<?> clazz;

        private static ClassLoader fromContext() {
            final GetClassLoader action = new GetClassLoader(null);
            if (System.getSecurityManager() != null) {
                return AccessController.doPrivileged(action);
            } else {
                return action.run();
            }
        }

        private static ClassLoader fromClass(Class<?> clazz) {
            if (clazz == null) {
                throw new IllegalArgumentException("Class is null");
            }
            final GetClassLoader action = new GetClassLoader(clazz);
            if (System.getSecurityManager() != null) {
                return AccessController.doPrivileged(action);
            } else {
                return action.run();
            }
        }

        private GetClassLoader(Class<?> clazz) {
            this.clazz = clazz;
        }

        @Override
        public ClassLoader run() {
            if (clazz != null) {
                return clazz.getClassLoader();
            } else {
                return Thread.currentThread().getContextClassLoader();
            }
        }
    }
}

其中UTF8Control类是来自i18n 的类,在 JSF 2.0 应用程序中具有 UTF-8 编码的属性文件

于 2014-08-07T13:14:19.343 回答
0

如果您使用与https://stackoverflow.com/a/3646601/5072526中相同的资源包,您可以这样做:

从该答案修改 ResourceBundle,因此您有一个额外的构造函数,它采用语言环境:

public I18NUtf8RessourceBundle(Locale locale) {
    setParent(ResourceBundle.getBundle(BUNDLE_NAME, 
            locale, UTF8_CONTROL));
}

ValidationMessages然后在默认包中创建一个类:

public class ValidationMessages extends I18NUtf8RessourceBundle{
   public ValidationMessages() {
        super(null);
    }
}

然后使用特定的语言环境(_en、_de 等)创建相同的类:

public class ValidationMessages_en extends I18NUtf8RessourceBundle{
    public ValidationMessages_en() {
        super(Locale.ENGLISH);
    }
}

对所有语言执行相同操作,并每次传递不同的语言环境:

有了它,它就可以工作了,您甚至可以拥有与普通翻译相同的验证消息文件!

于 2015-10-09T15:13:03.833 回答