Implementing a Locale provider that works in JSF and JAX-RS

荒凉一梦 提交于 2019-12-06 11:37:56

I know this has been posted a while ago, but as it has not been marked as resolved, here is how I got a workaround working for this specific case:

  • First I got a custom ResourceBundle working, as @BalusC described here: http://balusc.blogspot.fr/2010/10/internationalization-in-jsf-with-utf-8.html
  • Then I updated the constructor in order to detect if a FacesContext is currently being in use, from this :

    public Text() {
        setParent(ResourceBundle.getBundle(BUNDLE_NAME, 
        FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
    }
    
  • To This:

    public Text() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        setParent(ResourceBundle.getBundle(BUNDLE_NAME, 
        ctx != null ? ctx.getViewRoot().getLocale() : Locale.ENGLISH, UTF8_CONTROL));
    }
    

This now works both in JSF and JAX-RS context.

Hope this help,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!