Localization in JSF, how to remember selected locale per session instead of per request/view

后端 未结 5 1205
夕颜
夕颜 2020-11-22 04:36

faces-config.xml:


    
        ru
        

        
5条回答
  •  温柔的废话
    2020-11-22 05:03

    One small remark to @BalusC great solution. If we have which executes some method in backing bean. Locale available from call to FacesContext.getCurrentInstance().getViewRoot().getLocale() inside that method would be locale that is set by user browser or default application locale, not that locale that is set on session bean by user selection(of course they can match if browser locale equals that locale that user selected).

    I can stand corrected, because maybe I did something wrong when implementing solution provided by @BalusC.

    EDIT. After playing with JSF lifecycle, this behavior with locale is not related to , because there is similar behavior also with @PostContruct. in request(after user selected locale) is executed in render response phase. and @PostContruct methods are executed in invoke application phase. That is why logic that is executed in this method do not have access to user selected locale.

    Solution that we using when we need correct locale is to inject(CDI) localeBean in other backing bean that contains and @PostContruct methods, and then set locale with UIViewRoot#setLocale() from localeBean in beginning of these methods.

提交回复
热议问题