我在UTF-8中显示Hibernate Validator的自定义ValidationMessages时遇到问题。
对于常见的jsf消息,我遵循了以下建议:i18n with UTF-8 encoded properties files in JSF 2.0 appliaction - 我创建了类Text并在faces-config.xml中使用它。这工作正常。
但是这种方法不适用于ValidationMessages;特殊字符不以UTF-8显示。
有人可以给我一些建议吗?非常感谢你
答案 0 :(得分:0)
我以同样的方式解决了。 Hibernate验证器在META-INF / validation.xml
中有配置文件validation.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 with UTF-8 encoded properties files in JSF 2.0 appliaction
的类答案 1 :(得分: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);
}
}
然后使用特定的Locale(_en,_de等)创建相同的Class:
public class ValidationMessages_en extends I18NUtf8RessourceBundle{
public ValidationMessages_en() {
super(Locale.ENGLISH);
}
}
对所有语言执行相同操作,并且每次都传递不同的区域设置:
有了这个,它可以工作,甚至可以使用与正常翻译相同的验证消息文件!