首页 文章

使用Prettyfaces重写多语言网址

提问于
浏览
2

我将我的网站 Build 为多语言 . 我希望根据语言环境将语言代码嵌入到页面地址中 . 我有以下内容:

http://localhost:8080/Wirote/index

我希望如下:

http://localhost:8080/Wirote/de/index --- display German content
http://localhost:8080/Wirote/en/index --- display English content
http://localhost:8080/Wirote/ar/index --- display Arabic content

为实现这一目标,我按照以下步骤操作:multi-language url rewiting. Is it possibl e?

漂亮-config.xml中

<url-mapping id="base">    
    <pattern value="/#{localeManger.language}"/> 
 </url-mapping>

 <url-mapping id="index" parentId="base">
    <pattern value="/index"/>
    <view-id value="/index.xhtml"/> 
 </url-mapping>

faces-config.xml中

<application>
        <locale-config>
            <default-locale>de</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>ar</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>I18n.lang</base-name>
            <var>sprache</var>
        </resource-bundle>
    </application>

LocaleManger.java

@ManagedBean(name = "localeManger")
@SessionScoped
public class LocaleManger implements Serializable{

    private Locale locale;
    private static final long serialVersionUID = 2756934361134603857L;

    @PostConstruct
    public void init() {
 FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
    }

    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);
    }}

现在当我运行项目时,我只得到:

http://localhost:8080/Wirote/index

页面也被卡住了,所以我无法使用index.xtml中的链接导航到另一个页面

替代方法我将以下内容添加到index.xhtml:

<f:metadata>
        <f:viewParam name="locale" value="#{localeManger.language}"/>
  </f:metadata>

漂亮-config.xml中

<url-mapping id="index">
    <pattern value="/#{locale}/index"/>
    <view-id value="/index.xhtml"/> 
 </url-mapping>

现在,当我运行项目时,我得到以下内容:

http://localhost:8080/Wirote/de/index

但是当我尝试更改语言时,通过单击英语或阿拉伯语语言切换器,它无法正常工作,页面内容会更改,但地址页面不会更改 . 但如果我手动更改它

http://localhost:8080/Wirote/en/index   or
http://localhost:8080/Wirote/ar/index

它以阿拉伯语和英语显示正确的内容,但我需要自动更改地址而不是手动更改 .

如何获取与当前区域设置相关的正确地址?

1 回答

相关问题