JSF 语言环境问题

发布于 2024-10-13 03:19:06 字数 117 浏览 4 评论 0原文

我遇到一种情况,当用户从下拉列表中选择一种语言时,我希望应用程序区域设置相应更改。捕获语言环境并不困难,但是如何为之后的所有页面设置语言环境。

我在资源包和 faces-config.xml 中设置了配置

I have a situation where when a user selects a language from a drop down, I want the the application locale to change accordingly. Capturing locale is not difficult, but how to set the locale for all pages there after.

I have configuration set in resource bundles and faces-config.xml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳根太软 2024-10-20 03:19:06

注册您自己的 ViewHandler 并覆盖其 calculateLocale() 方法是一种方法。

自定义ViewHandler需要在faces-config.xml文件中指出,例如:

<faces-config version="2.0" xmlns...>   
   <application>
           ...
       <view-handler>your.package.CustomLocaleViewHandler</view-handler> ...

并且实现将仅覆盖calculateLocale()并委托其他代理ViewHandler的方法:

public class CustomLocaleViewHandler extends ViewHandler {

    private final ViewHandler base;

    public CustomLocaleViewHandler(ViewHandler base) {
        this.base = base;
    }

    @Override
    public Locale calculateLocale(FacesContext context) {
    //... your logic goes here
    return locale;
    }

    @Override
    public String calculateRenderKitId(FacesContext context) {
        return base.calculateRenderKitId(context);
    }

    @Override
    public UIViewRoot createView(FacesContext context, String viewId) {
        return base.createView(context, viewId);
    }

    ... other proxied methods

}

Registering your own ViewHandler and overriding its calculateLocale() method would be one way to go.

Custom ViewHandler needs to be indicated in faces-config.xml file, e.g.:

<faces-config version="2.0" xmlns...>   
   <application>
           ...
       <view-handler>your.package.CustomLocaleViewHandler</view-handler> ...

And the implementation would override the calculateLocale() only and delegate other methods to proxied ViewHandler:

public class CustomLocaleViewHandler extends ViewHandler {

    private final ViewHandler base;

    public CustomLocaleViewHandler(ViewHandler base) {
        this.base = base;
    }

    @Override
    public Locale calculateLocale(FacesContext context) {
    //... your logic goes here
    return locale;
    }

    @Override
    public String calculateRenderKitId(FacesContext context) {
        return base.calculateRenderKitId(context);
    }

    @Override
    public UIViewRoot createView(FacesContext context, String viewId) {
        return base.createView(context, viewId);
    }

    ... other proxied methods

}
温馨耳语 2024-10-20 03:19:06

我相信这就是您正在寻找的:

FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(locale);

I believe this is what you are looking for:

FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(locale);
温柔嚣张 2024-10-20 03:19:06

我刚刚经历过这个,编码并不难,但需要花一些时间来弄清楚如何以面向未来和自动化的方式有效地进行工作,但我认为我有一个合理的方法,可以自动检测 JSF 中配置的语言,并自动将菜单项本地化为当前区域设置。

首先,确保您使用某种 Facelets 模板,否则您将必须在您使用的每个页面上复制第一部分。您使用 f:view locale= 参数设置区域设置

您的页面应如下所示:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <f:view locale="#{LocaleBean.currentLocale}">
        <h:head>
...
        </h:head>
        <h:body>
...
            <!-- Put this somewhere convenient on your page -->
            <h:form>
                <h:selectOneMenu value="#{Locale.currentLocale}" onchange="this.form.submit();" immediate="true" >
                    <f:selectItems value="#{Locale.supportedLocales}" var="loc" itemLabel="#{loc.getDisplayName(Locale.currentLocale)}" itemValue="#{loc}"/>
                    <f:converter converterId="LocaleConverter"/>
                </h:selectOneMenu>
            </h:form>
...
        </h:body>
    </f:view>
</html>

这是我的 LocaleBean:(使用 @ManagedBean 注册它,或通过具有会话范围的 faces-config.xml 注册)

public class LocaleBean {

    protected List<Locale> supportedLocales = new ArrayList<Locale>();

    protected Locale currentLocale;

    /**
     * Get the value of currentLocale
     *
     * @return the value of currentLocale
     */
    public Locale getCurrentLocale() {
        if (currentLocale == null && FacesContext.getCurrentInstance() != null) {
            currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }
        return currentLocale;
    }

    /**
     * Set the value of currentLocale
     *
     * @param currentLocale new value of currentLocale
     */
    public void setCurrentLocale(Locale currentLocale) {
        this.currentLocale = currentLocale;
        FacesContext.getCurrentInstance().getViewRoot().setLocale(currentLocale);
    }

    public List<Locale> getSupportedLocales() {
        if (supportedLocales.isEmpty() && FacesContext.getCurrentInstance() != null) {
            Iterator<Locale> facesLocales = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
            while (facesLocales.hasNext()) {
                supportedLocales.add(facesLocales.next());
            }
        }
        return supportedLocales;
    }

    public void setSupportedLocaleStrings(Collection<String> localeStrings) {
        supportedLocales.clear();
        for (String checkLocale : localeStrings) {
            for (Locale locale : Locale.getAvailableLocales()) {
                if (locale.toString().equals(checkLocale)) {
                    supportedLocales.add(locale);
                    break;
                }
            }
        }

    }

}

和 LocaleConverter (同样,通过注释或 faces-config.xml 注册)

public class LocaleConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        for (Locale locale : Locale.getAvailableLocales()) {
            if (locale.toString().equals(value)) {
                return locale;
            }
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value.toString();
    }

}

I just went through this, and it's not difficult to code, but takes a bit to figure out how to do effectively in a future-proof and automated fashion, but I think I have a reasonable approach that automatically detects what languages are configured in JSF, and that localizes the menu items to the current Locale automatically.

First, make sure you're using a facelets template of some sort, or you're going to have to replicate this first part on every page you use. You set the Locale using the f:view locale= parameter

Your page(s) should look something like this:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <f:view locale="#{LocaleBean.currentLocale}">
        <h:head>
...
        </h:head>
        <h:body>
...
            <!-- Put this somewhere convenient on your page -->
            <h:form>
                <h:selectOneMenu value="#{Locale.currentLocale}" onchange="this.form.submit();" immediate="true" >
                    <f:selectItems value="#{Locale.supportedLocales}" var="loc" itemLabel="#{loc.getDisplayName(Locale.currentLocale)}" itemValue="#{loc}"/>
                    <f:converter converterId="LocaleConverter"/>
                </h:selectOneMenu>
            </h:form>
...
        </h:body>
    </f:view>
</html>

Here's my LocaleBean: (Register it with either @ManagedBean, or via faces-config.xml with a session scope)

public class LocaleBean {

    protected List<Locale> supportedLocales = new ArrayList<Locale>();

    protected Locale currentLocale;

    /**
     * Get the value of currentLocale
     *
     * @return the value of currentLocale
     */
    public Locale getCurrentLocale() {
        if (currentLocale == null && FacesContext.getCurrentInstance() != null) {
            currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }
        return currentLocale;
    }

    /**
     * Set the value of currentLocale
     *
     * @param currentLocale new value of currentLocale
     */
    public void setCurrentLocale(Locale currentLocale) {
        this.currentLocale = currentLocale;
        FacesContext.getCurrentInstance().getViewRoot().setLocale(currentLocale);
    }

    public List<Locale> getSupportedLocales() {
        if (supportedLocales.isEmpty() && FacesContext.getCurrentInstance() != null) {
            Iterator<Locale> facesLocales = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
            while (facesLocales.hasNext()) {
                supportedLocales.add(facesLocales.next());
            }
        }
        return supportedLocales;
    }

    public void setSupportedLocaleStrings(Collection<String> localeStrings) {
        supportedLocales.clear();
        for (String checkLocale : localeStrings) {
            for (Locale locale : Locale.getAvailableLocales()) {
                if (locale.toString().equals(checkLocale)) {
                    supportedLocales.add(locale);
                    break;
                }
            }
        }

    }

}

And the LocaleConverter (again, register via annotations or faces-config.xml)

public class LocaleConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        for (Locale locale : Locale.getAvailableLocales()) {
            if (locale.toString().equals(value)) {
                return locale;
            }
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value.toString();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文