我使用此代码但我无法设置除英语之外的默认语言环境。我用最新的Mojarra版本尝试了这个:
面-config.xml中:
<?xml version="1.0"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<locale-config>
<default-locale>bg</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>bg</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.web.common.ints.Text</base-name>
<var>text</var>
</resource-bundle>
</application>
</faces-config>
加载属性文件的Java类:
public class Text extends ResourceBundle
{
protected static final String BUNDLE_NAME = "com.web.common.ints.text";
protected static final String BUNDLE_EXTENSION = "properties";
protected static final Control UTF8_CONTROL = new UTF8Control();
public Text()
{
setParent(ResourceBundle.getBundle(BUNDLE_NAME,
FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
}
@Override
protected Object handleGetObject(String key)
{
return parent.getObject(key);
}
@Override
public Enumeration getKeys()
{
return parent.getKeys();
}
protected static class UTF8Control extends Control
{
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
// The below code is copied from default Control#newBundle() implementation.
// Only the PropertyResourceBundle line is changed to read the file as UTF-8.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, BUNDLE_EXTENSION);
ResourceBundle bundle = null;
InputStream stream = null;
if (reload)
{
URL url = loader.getResource(resourceName);
if (url != null)
{
URLConnection connection = url.openConnection();
if (connection != null)
{
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
}
else
{
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null)
{
try
{
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
}
finally
{
stream.close();
}
}
return bundle;
}
}
}
当我打开网页时,他的语言环境始终是英文的? 我如何设置自定义区域设置区域设置?
您是否知道如何解决此问题?
我使用了本教程:http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/
所以我改变了这一行:
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
要:
locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
如果我使用原始代码,我会获得NPE。但是使用第二个代码,我总是将其作为默认语言环境。
答案 0 :(得分:0)
该区域设置不是来自浏览器吗?语言环境是英语b / c您的浏览器在一个系统上运行,该系统说它的语言环境是英语。在Windows下,我安装了西班牙语语言包,然后我可以通过切换计算机和打开新浏览器来控制默认语言环境,或者至少是浏览器要求的语言环境,如果我没记错的话。
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>es</supported-locale>
<supported-locale>zh</supported-locale>
</locale-config>
<resource-bundle>
<base-name>i18n.Text</base-name>
<var>text</var>
</resource-bundle>
</application>
</faces-config>
和
@ManagedBean
@SessionScoped
public class LocaleBean implements Serializable {
private static final long serialVersionUID = -2786895465419133453L;
private Locale locale;
@PostConstruct
public void init() {
locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
}
public Locale getLocale() {
return locale;
}
public String getLanguage() {
return locale.getLanguage();
}
public void setLanguage(String language) {
locale = new Locale(language);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
和
<p:submenu label="#{text['menu.language']}" style="float:right">
<p:menuitem value="#{text['menu.language.english']}" action="#{localeBean.setLanguage('en')}" oncomplete="location.reload(true)" />
<p:menuitem value="#{text['menu.language.spanish']}" action="#{localeBean.setLanguage('es')}" oncomplete="location.reload(true)"/>
<p:menuitem value="#{text['menu.language.chinese']}" action="#{localeBean.setLanguage('zh')}" oncomplete="location.reload(true)"/>
然后是各种i18n/Text_en.properties
,i18n/Text_es.properties
,i18n/Text_zh.properties
。