如何在本地化网站上更改日期时间格式? [英] How to change date time format on a localized website?

查看:354
本文介绍了如何在本地化网站上更改日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用不同语言的本地化网站

I have a localized website for different languages

用户可以选择配置文件中使用的语言,这将在PageLoad之前应用 使用

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

context.setLocalString("en")

我相信

"en"默认为en-US,因此网站上的日期以美国格式显示,因此我知道可以使用.

"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"))

setLocal的问题在于它不会更新HTML lang ="en"属性,因此尽管使用setLocal后日期正确,但使用的语言文件仍然是englisn(US),而不是english(uk),因此事件是正确的) 一. (即不是html lang ="zh-cn")

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")

因此,当来自英国的用户在其个人资料中将他们的语言设置为en-uk时,就会获得美国语言文件.

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")

,但是setLocalString会覆盖setLocalString,反之亦然.所以看起来我不能同时使用它们

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

有什么方法可以在beforePageLoad上添加代码,以确保html lang属性使用正确的语言更新,并且我的日期以正确的语言格式显示?

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?

推荐答案

beforeRenderResponse 期间,尝试直接在视图根目录中设置语言环境,而不是使用 context 对象:

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>

或者您可以按如下所述在阶段侦听器中切换语言环境: http://openntf.org/XSnippets.nsf/snippet.xsp ?id = xpages-localization-setter

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

语言环境设置有些奇怪.使用 context.setLocaleString()时,必须在 en GB 之间使用下划线(如Panu Haaramo回答),但这不会解决问题,因为 ViewRootRender 在呈现HTML输出时仅使用语言设置来生成 lang 属性.

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.

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

仅返回 en ,"GB"将被忽略.

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

使用 context.setLocaleString 会带来相同的结果,因为这只会解析给定的字符串并将其转换为 java.util.Locale 并返回与之相同的结果.沮丧.

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.

但是使用未定义的语言环境会生成小写的 lang 属性. F.e.

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>

生成以下HTML标记:

generates the following HTML tag:

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

这就是为什么此答案顶部的代码将lang属性设置为 en-gb 的原因,但这仍然是不正确的:应将其设置为 en-GB 描述如下: w3.org:最佳做法:在XHTML中指定语言& HTML内容

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

这篇关于如何在本地化网站上更改日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆