Bạn đã gắn thẻ câu hỏi của mình với Spring, vì vậy, tôi khuyên bạn nên sử dụng Spring MessageSource. Spring MessageSource có thể tổng hợp nhiều tệp thuộc tính thậm chí phân cấp. Nó cung cấp cho bạn nhiều lợi thế hơn cũ java ResourceBundle
.
Bạn có thể định nghĩa mùa xuân MessageSource
trong bạn spring-config.xml
như thế này:
<!--
Application messages configuration.
-->
<bean id="messageSource" name="resourceBundle"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:fallbackToSystemLocale="false"
p:cacheSeconds="0">
<property name="basenames">
<list>
<value>/messages/Messages</value>
<!-- <value>${application.messages}</value>-->
</list>
</property>
</bean>
hơn bạn có thể xác định bạn Class
kéo dài ResourceBundle
như thế này (Needs some làm sạch và tái cấu trúc):
public class SpringResourceBundle extends ResourceBundle
{
private MessageSource messages;
private FacesContext fc;
private Locale locale = null;
public SpringResourceBundle()
{
fc = FacesContext.getCurrentInstance();
WebApplicationContext webAppCtx = (WebApplicationContext) fc.getExternalContext().getApplicationMap().get(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
messages = (MessageSource) webAppCtx.getBean("messageSource");
}
@Override
public Locale getLocale()
{
Locale loc = fc.getELContext().getLocale();
if (fc.getExternalContext() != null) {
loc = fc.getExternalContext().getRequestLocale();
}
try {
UIViewRoot viewRoot = fc.getViewRoot();
if (viewRoot != null) {
loc = viewRoot.getLocale();
}
if (loc == null) {
loc = fc.getApplication().getDefaultLocale();
}
} catch (Throwable th) {
System.out.println(th.getMessage());
loc = locale;
}
locale = loc;
return loc;
}
@Override
protected Object handleGetObject(String key)
{
try {
return messages.getMessage(key, null, getLocale());
} catch (NoSuchMessageException e) {
return "???" + key + "???";
}
}
@Override
public Enumeration<String> getKeys()
{
return Collections.enumeration(Collections.EMPTY_LIST);
}
}
finnaly trong faces-config.xml
khai báo gói tài nguyên của bạn với Lớp ở trên. Một cái gì đó như thế này:
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>cs</supported-locale>
<supported-locale>de</supported-locale>
<supported-locale>en</supported-locale>
</locale-config>
<message-bundle>your.package.SpringResourceBundle</message-bundle>
</application>
Ở đây bạn đi Spring MessageSource in JSF.Hy vọng nó là dễ hiểu.
Nguồn
2012-10-15 16:27:41
Tôi đã nghĩ liệu Spring Framework có một giải pháp cho điều này? – Abdul