久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 JSF 中 - 獲取客戶端的區域設置(在瀏覽器的時

In JSF - Getting the Client#39;s Locale (To Display time in browser#39;s timezone)(在 JSF 中 - 獲取客戶端的區域設置(在瀏覽器的時區中顯示時間))
本文介紹了在 JSF 中 - 獲取客戶端的區域設置(在瀏覽器的時區中顯示時間)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在用 JSF 2.0 編寫一個 webapp,它將時間戳作為信息的一部分顯示給用戶.我希望用戶看到本地化到他們的位置(瀏覽器的語言環境)的時間戳.

I'm writing a webapp in JSF 2.0 that displays Timestamps as part of the information to the user. I would like the user to see the timestamps localized to their location (browser's locale).

到目前為止,無論我做什么,時間戳都會顯示為本地化到服務器的時區.

So far, whatever I did, the timestamps would always appear localized to the timezone of the server.

我嘗試使用以下方法獲取語言環境:

I tried getting the locale with these methods:

Locale browserLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

Locale browserLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();

兩者都返回了服務器的語言環境.

Both returned the server's locale.

然后我使用帶有 SimpleDateFormat 的語言環境對象來打印時間戳.

I then use the locale object with SimpleDateFormat to print timestamps.

我是否使用了正確的 API?
我在某處讀到您必須使用客戶端代碼(Javascript)來獲取瀏覽器的時區.那是對的嗎?你會怎么做?

Am I using the correct API?
I've read somewhere that you have to use client side code (Javascript) to get the browser's timezone. Is that correct? How would you do that?

感謝'n' Advance.

Thank's 'n' Advance.

更新發現 this (jsTimezoneDetect) JavaScript 代碼.但我仍然不確定如何將時區轉換為 Java Locale Object

UPDATE found this (jsTimezoneDetect) JavaScript code. But I'm still not sure how to translate the timezone to a Java Locale Object

推薦答案

您可能應該使用內置格式化標簽而不是 SimpleDateFormat.您的問題暗示您想向國際用戶顯示日期和時間,在這種情況下,您應該真正使用用戶的本地格式(您知道,它們往往不同).

You probably should use built-in formatting tag instead of SimpleDateFormat. Your question implies that you want to show date and time to International user, and in this case you should really use user's local format (they tend to differ, you know).

就時區而言,它與國際化和本地化無關,即美國有幾個不同的時區.您可以在這里使用兩種方法:

In case of time zone, it has nothing to do with Internationalization and Localization, i.e. there are several different time zones in USA. There are two approaches you can use here:

  1. 在用戶配置文件中存儲時區信息(如果有的話).這是最簡單的方法,允許您使用內置的 <f:convertDateTime> 標簽.

從網絡瀏覽器獲取時區信息.您可以像 Ben 的示例一樣通過 Ajax 請求獲取它.從技術上講,您也可以在此處使用 <f:convertDateTime> 標簽.

Get time zone information from web browser. You can get it via Ajax request just like in Ben's example. Technically you can also use <f:convertDateTime> tag here.

您可以以某種常見的、與區域設置無關(或者如果您愿意,也可以是不變的)格式發送 UTC 時間戳,在客戶端對其進行解析以創建 JavaScript 的日期對象和帶有 全球化.

You can send the timestamps in UTC in some common, locale-independent (or invariant if you prefer) format, parse it on the client side to create JavaScript's date object and format for locale with Globalize.

下面會有一些例子,但讓我先解釋一下.

Some examples will follow but let me explain something first.

Locale browserLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

為您提供網絡瀏覽器的區域設置(但不是時區,因為這與區域設置相關).它實際上會讀取 HTTP Accept-Language 標頭的內容并選擇最佳的語言環境.如果它不適合您,請確保您在 faces-config.xml 中正確設置了支持的語言環境.通過最好的語言環境,我知道它將嘗試使用用戶最喜歡的語言環境(如果您的應用程序支持),然后是第二好的,依此類推.如果不支持任何區域設置,它將回退到您的應用程序的默認區域設置(同樣,faces-config.xml 必須具有此設置)或如果缺少此設置(或者至少我是這么認為的,這有點道理).

This will give you web browser's locale (but not time zone, since this is not locale related). It will actually read the contents of HTTP Accept-Language header and choose the best possible locale. If it is not working for you, please make sure that you have correctly set supported locales in your faces-config.xml. By best possible Locale, I understand that it will try to use user's most preferred Locale (if that's supported by your application), then second best and so on. If none of the Locales is supported, it will fall-back to your application's default Locales (again, faces-config.xml has to have this setting) or to server's default Locale if this setting is missing (or at least I think so, it kind of makes sense).

Locale browserLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();

這將為您提供來自 Accept-Language 標頭的頂級語言環境.請檢查您的網絡瀏覽器的設置 - 它幾乎無法為您提供服務器區域設置,除非它們與您的網絡瀏覽器完全相同.當且僅當 JVM 不支持任何 Web 瀏覽器的 Locale(這似乎不太可能)時,它才能為您提供服務器的默認值.

This one will give you the top Locale from Accept-Language header. Please check your web browser's settings - there is almost no way for it to give you the server Locale, unless they are exactly the same as your web browser's. It can give you server's defaults if and only if, none of the web browser's Locale is supported by JVM (which seems a bit unlikely).

順便說一句.FacesContext.getCurrentInstance().getExternalContext().getRequestLocales() 將為您提供迭代器,以便您可以手動迭代 Accept-Language 標頭中的區域設置列表.只是為了讓你知道,你可能不應該使用它(UIViewRoot 真的足夠了).

BTW. FacesContext.getCurrentInstance().getExternalContext().getRequestLocales() will give you the Iterator so you can manually iterate through the list of Locales in Accept-Language header. It is just to let you know, you probably should not use it (UIViewRoot is really enough).

現在,假設您有一些 bean,其中包含用戶配置文件和為您提供時區的方法.這種方法比 Ajax 調用更好,因為兩個不同的時區可能具有相同的 UTC 偏移量,但在不同的日期切換夏令時(換句話說,某些時間戳會打印不正確).無論如何,在這種情況下,您可以像這樣格式化您的時間戳(日期也來自某個 bean):

Now, suppose you have some bean with the user profile and the method which will give you the time zone. This method is better than Ajax call, in the sense that it might happen that two different time zones have the same UTC offset but switch Daylight Saving Time on different date (in other words some timestamps would be printed incorrectly). Anyway, in case like this, you can format your time-stamp like this (date also come from some bean):

<h:outputText value="#{someBean.timestamp}">
  <f:convertDateTime type="both" dateStyle="default" timeStyle="default" timeZone="#{userProfile.timeZone}" />
</h:outputtext>

現在讓我解釋一下屬性:

Now let me explain the attributes:

  • type - 要顯示的內容,均表示日期和時間
  • dateStyle - 日期樣式(短、中、長、完整、默認).您真的應該在這里使用默認值,因為這將為每個區域設置使用最合適的格式
  • timeStyle - 類似于日期樣式,但用于時間部分
  • timeZone - 采用 UTC 偏移量(因此您無需轉換任何內容)或時區名稱(即 America/Los_Angeles).

標簽默認使用當前視圖語言環境,所以你不必擔心這部分,特別是如果你正確設置了語言環境支持.

The tag will use current view Locale by default, so you do not have to worry about this part, especially if you set up Locale support correctly.

將它與 Ajax 相結合(請參閱 Ben 的回答)會很容易(我認為).

Combining it with Ajax (see Ben's answer) would be easy enough (I think).

我還提到你可以寫出不變的日期,在客戶端解析它們,然后用 Globalize 格式化它們.假設您已經解析了日期(這取決于您要使用的表示形式,因此我將跳過這部分),可以這樣做:

I also mentioned that you can write out invariant dates, parse them on the client-side and then format them with Globalize. Assuming that you have parsed date already (it depends on the representation you want to use, so I will skip this part), it could be done like that:

// you also have to assign the culture based on UIViewRoot locale and send it out with JavaScript
Globalize.culture(theLocale);
var formattedDateTime = Globalize.format(parsedDateTime, "f"); // this will use short date time format

與 Java 不同,Globalize 只有短 ("f") 和長 ("F") 日期和時間格式.所以我決定使用短的.
還請記住,Globalize 文化是通過連字符而不是下劃線分隔的,因此您需要fr-CA",而不是fr_CA".
如果您想使用該方法并需要更具體的示例,請告訴我.

Unlike Java, Globalize have only short ("f") and long ("F") date and time formats. So I decided on using short one.
Please also keep in mind, that Globalize cultures are separated via hyphen, not underscore, so you need "fr-CA", not "fr_CA" for example.
Please let me know if you want to use that method and need more concrete example.

這篇關于在 JSF 中 - 獲取客戶端的區域設置(在瀏覽器的時區中顯示時間)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 九九免费在线视频 | 成人av色| 超碰人人做 | www.av在线 | 国产日韩精品视频 | 国产不卡一区 | 中文在线www | 成人免费区一区二区三区 | 亚洲日本一区二区三区四区 | 精品国产鲁一鲁一区二区张丽 | 国产成人免费 | 午夜影视 | 日韩伦理电影免费在线观看 | 国产真实乱对白精彩久久小说 | 久久精品一级 | 久草精品视频 | 国产精品久久久久久久午夜片 | 国产欧美精品一区 | 久久久久久蜜桃一区二区 | 国产乱码精品1区2区3区 | 日韩视频在线播放 | 欧美激情在线一区二区三区 | 美女视频h | 国产精品国产三级国产aⅴ无密码 | 欧美一级欧美一级在线播放 | 日韩免费av一区二区 | 久久一级免费视频 | 黄色中文字幕 | 香蕉久久av | 久久99精品视频 | xx性欧美肥妇精品久久久久久 | 国产精品视频一二三区 | 天天曰夜夜 | 91av导航 | 精品国产久 | 国产黄色在线观看 | 国产高清在线视频 | 日韩精品一区二区三区中文在线 | 情侣av| 精品国产精品三级精品av网址 | 免费看a|