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

獲取訪問者語言 &帶有javascript的國家代碼

Get visitors language amp; country code with javascript (client-side)(獲取訪問者語言 amp;帶有javascript的國家代碼(客戶端))
本文介紹了獲取訪問者語言 &帶有javascript的國家代碼(客戶端)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

問題:是否有 javascript(客戶端)代碼來獲取訪問者的國家/語言代碼,準確且跨現代"瀏覽器?我正在尋找 'en-US''sv-SE''nl-NL' 等結果.

Question: Is there a javascript (client-side) code to get visitors country/language code, that is accurate and is cross-"modern"-browser ? I am looking for results like 'en-US', 'sv-SE', 'nl-NL', etc.

之前已經提出過與此相關的問題(一些 SO 鏈接:1,2,3,4 等),但我沒有找到答案,有些答案已經有幾年的歷史了,在某些情況下指的是更老的文章,這使得我認為對此有新的解決方案.

Related questions to this have been asked before (some SO links: 1,2,3,4, among others) but I didn't find answer and some of the answers are some yearls old and in some cases referring to even more old articles, which makes me think there are new solutions for this.

我試過了:

var language = window.navigator.userLanguage || window.navigator.language;
console.log(language);

在 Chrome 中獲得 "sv",在 Firefox 中獲得 "en-GB",在同一臺機器上,同一地點.

and got "sv" in Chrome and "en-GB" in Firefox, in the same machine, same place.

推薦答案

navigator.language 如您的鏈接問題之一所述并不可靠.

navigator.language isn't reliable as one of your linked questions states.

這個問題被問了很多,但你仍在搜索的原因說明了這個問題.客戶端的語言檢測純粹并不可靠.

The reason this is asked a lot, but you're still searching says something about the problem. That language detection purely on the client side is not anything close to reliable.

首先,語言首選項只能用于檢測語言首選項 - 即不是位置.我的瀏覽器設置為 en_US,因為我想要英文版.但我在英國,所以必須將其更改為 en_GB 才能通過我的瀏覽器設置檢測到我的國家.作為客戶",這不是我的問題.這對語言來說很好,但如果您網站上的所有價格都是美元,那就不好了.

First of all language preferences should only be used to detect language preferences - i.e. not location. My browser is set to en_US, because I wanted the English version. But I'm in the UK, so would have to alter this to en_GB to have my country detected via my browser settings. As the 'customer' that's not my problem. That's fine for language, but no good if all the prices on your site are in $USD.

檢測語言,您確實需要訪問服務器端腳本.如果您不是后端開發人員并且想在客戶端做盡可能多的事情(作為您的問題),那么您只需要一個 PHP 腳本來回顯 Accept-Language 標題.最簡單的可能是:

To detect language you really do need access to a server side script. If you're not a back end dev and want to do as much as possible on the client side (as your question), all you need is a one line PHP script that echos back the Accept-Language header. At its simplest it could just be:

<?php
echo $_SERVER['HTTP_ACCEPT_LANGUAGE']; 
// e.g. "en-US,en;q=0.8"

您可以通過 Ajax 獲取它并解析文本響應客戶端,例如(使用 jQuery):

You could get this via Ajax and parse the text response client side, e.g (using jQuery):

$.ajax( { url: 'script.php', success: function(raw){
    var prefs = raw.split(',');
    // process language codes ....
} } );

如果您能夠通過后端生成 HTML,則可以通過簡單地將語言首選項打印到頁面中來完全避免使用 Ajax,例如

If you were able to generate your HTML via a back end, you could avoid using Ajax completely by simply printing the language preferences into your page, e.g.

<script>
    var prefs = <?php echo json_encode($_SERVER['HTTP_ACCEPT_LANGUAGE'])?>;
</script>

如果您無法訪問服務器,但可以將腳本放到另一臺服務器上,那么簡單的 JSONP 服務將如下所示:

If you had no access to the server but could get a script onto another server, a simple JSONP service would look like:

<?php
$prefs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$jsonp = 'myCallback('.json_encode($prefs).')';

header('Content-Type: application/json; charset=UTF-8', true );
header('Content-Length: '.strlen($jsonp), true );
echo $jsonp;

在你的 Ajax 中使用 jQuery 你會做類似的事情:

Using jQuery for your Ajax you'd do something like:

function myCallback( raw ){
    var prefs = raw.split(',');
    // process language codes ....
}
$.ajax( {
    url: 'http://some.domain/script.php',
    dataType: 'jsonp'
} );

國家/地區檢測是另一回事.在客戶端有 navigator.geolocation,但它最可能會提示您的用戶獲得許可,因此不利于無縫的用戶體驗.

Country detection is another matter. On the client side there is navigator.geolocation, but it will most likely prompt your user for permission, so no good for a seamless user experience.

要進行隱形操作,您僅限于地理 IP 檢測.同理,也不要用語言來暗示國家.

To do invisibly, you're limited to geo IP detection. By the same token as above, don't use language to imply country either.

要在客戶端進行國家/地區檢測,您還需要后端服務來獲取客戶端 IP 地址并訪問 IP/位置映射數據庫.Maxmind 的 GeoIP2 JavaScript 客戶端 似乎為您將這一切包裝在一個客戶端包中,所以您不會不需要您自己的服務器(盡管我確信它會使用遠程 jsonp 服務).還有freegeoip.net,在注冊方面可能比MaxMind更省事,而且貌似也是開源的.

To do country detection on the client side, you'll also need a back end service in order to get the client IP address and access a database of IP/location mappings. Maxmind's GeoIP2 JavaScript client appears to wrap this all up in a client-side bundle for you, so you won't need your own server (although I'm sure it will use a remote jsonp service). There's also freegeoip.net, which is probably less hassle than MaxMind in terms of signing up, and it appears to be open source too.

這篇關于獲取訪問者語言 &amp;帶有javascript的國家代碼(客戶端)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 一区二区视频在线观看 | 狠狠干在线 | 亚洲高清视频在线观看 | 999精品在线| 综合色播 | 成人三级在线播放 | 日韩成人av在线播放 | 99精品国产一区二区三区 | 麻豆久久久久久久久久 | 91九色视频 | 亚洲精品乱码久久久久久久久 | 欧美一二三 | 欧美精品一区二区三区蜜桃视频 | 国产精品久久久久久婷婷天堂 | 久久黄色网 | 国产成人精品久久二区二区 | 免费观看成人av | 伦理一区二区 | 国产精品一区久久久 | 日韩免费激情视频 | avhd101在线成人播放 | av天天看| 狠狠做深爱婷婷综合一区 | 亚洲精品性视频 | 日本一区二区高清不卡 | 欧美a v在线 | 国产欧美精品一区二区色综合朱莉 | 亚洲精品电影在线观看 | 久久久观看| 国产在线观看一区二区三区 | 91精品国产综合久久精品 | 日本a v在线播放 | 国产一级片一区二区三区 | 一级黄色生活视频 | 四虎影院免费在线播放 | 午夜欧美一区二区三区在线播放 | 欧美性久久 | 亚洲高清免费视频 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 婷婷久久五月天 | 视频一二三区 |