How to change date time format on a localized website?

喜你入骨 提交于 2019-12-11 05:43:01

问题


I have a localized website for different languages

Users can select which language to use in a profile and this will be applied beforePageLoad using

context.setLocalString("en")

"en" is default to en-US i believe so the dates on the website is displayed in the US format so I learned that I can use instead.

context.setLocale(new Locale("en","gb"))

the problem with setLocal is that is does not update the HTML lang="en" attribute so event though the dates are correct after using setLocal the language file that is used is still the englisn(US) and not the english(uk) one. (i.e not html lang="en-gb")

so when users from england set their language to en-uk in thier profile they get the US language file.

So I tried to do both like this

 context.setLocale(new Locale("en","gb"))
 context.setLocalString("en-GB")

but then the setLocalString overrides the setLocalString and vice versa. so it looks like I can't use them both

Is there any way I can add code on beforePageLoad to make sure both the html lang attribute gets updated with correct language and my dates dispay in the correct format for the language set?


回答1:


Instead of using the context object try to set the locale directly in the view root during beforeRenderResponse:

<xp:this.beforeRenderResponse>
    <![CDATA[#{javascript:
       facesContext.getViewRoot().setLocale( new java.util.Locale("en-GB") );
    }]]>
</xp:this.beforeRenderResponse>

Or you can switch the locale in a phase listener as described here: http://openntf.org/XSnippets.nsf/snippet.xsp?id=xpages-localization-setter

EDIT:

The locale setting is a little bit strange. You have to use an underscore between en and GB when using context.setLocaleString() (as Panu Haaramo answered), but this won't solve problem, because the ViewRootRender uses only the language setting for the generation of the lang attribute while rendering the HTML output.

This

new java.util.Locale("en", "GB").getLanguage()

will return en only, the "GB" is ignored.

Using the context.setLocaleString will bring the same result because this only parses the given string and converts it into a java.util.Locale which returns the same result as descibed.

But using an undefined Locale will generate a lowercased lang attribute. F.e. this

<xp:this.beforeRenderResponse>
   <![CDATA[#{javascript:
      facesContext.getViewRoot().setLocale( new java.util.Locale("en-Blabla-Blubb") );
   }]]>
</xp:this.beforeRenderResponse>

generates the following HTML tag:

<html lang="en-blabla-blubb">

Thats why the code at top of this answer sets the lang attribute to en-gb, but this is still incorrect: It should set it to en-GB as described here: w3.org: Best Practices: Specifying Language in XHTML & HTML Content



来源:https://stackoverflow.com/questions/14640981/how-to-change-date-time-format-on-a-localized-website

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