[英]Redirection according to browser language
嗨,我有一個簡單的應用程序,用戶可以在英語和波蘭語之間選擇要在我的應用程序中使用的語言。
當用戶打開我的網站時,這是我想要的行為:
因為它是一個非常簡單的應用程序,所以我使用jQuery進行翻譯。
以下是我到目前為止的解決方案:
JSFIDDLE: 演示
//redirect $(document).ready(function() { var userLang = navigator.language || navigator.userLanguage; if (userLang == "pl-pl") { break; } else { userLang = "eng-gb" } } }); // translation // The default language is Polish var lang = "pl-pl"; // Check for localStorage support if ('localStorage' in window) { var usrLang = localStorage.getItem('uiLang'); if (usrLang) { lang = usrLang } } var arrLang = { "pl-pl": { "Valentine": "Do Walentynek zostało już tylko", }, "en-gb": { "valentine": "Remaining days to Valentines!", } } $(document).ready(function() { $(".lang").each(function(index, element) { $(this).text(arrLang[lang][$(this).attr("key")]); }); $(".lang").each(function(index, element) { $(this).text(arrLang[lang][$(this).attr("key")]); }); // get/set the selected language $(".translate").click(function() { var lang = $(this).attr("id"); // update localStorage key if ('localStorage' in window) { localStorage.setItem('uiLang', lang); } $(".lang").each(function(index, element) { $(this).text(arrLang[lang][$(this).attr("key")]); }); }); });
<div class="translate"> <ul class="language"> <li class="eng translate" id="en-gb">EN</li> <li class="pl translate" id="pl-pl">PL</li> </ul> </div> <div class="rss-feed"> <p class="lang" key="valentine">Do Walentynek zostało już tylko </p> <span id="mybenfit-timer" class="lang" key="days"> </span> </div>
不幸的是,我的解決方案無法正常工作。 我的代碼有什么問題?
userLang
返回en-GB
因此語言是兩個小寫字符,而國家/地區則是兩個大寫字符,因此if語句應為if(userLang == 'pl-PL')
而不是if(userLang == 'pl-pl')
總是用你的方式返回假
但是,您只需要語言對嗎? 您可以嘗試This ..並始終以兩個小寫字符使用該語言。.無需使用pl-pl
或en-GB
您可以使用pl
或en
//redirect $(document).ready(function() { var userLang = navigator.language || navigator.userLanguage; console.log(userLang); var userLang = userLang.split('-')[0]; console.log(userLang); var userLang = (userLang !== "pl")? 'en' : 'pl'; console.log(userLang); var arrLang = { "pl": { "Valentine": "Do Walentynek zostało już tylko", }, "en": { "valentine": "Remaining days to Valentines!", } } console.log(arrLang[userLang].valentine); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
請嘗試以下操作,希望對您有所幫助
$(document).ready(function() {
// translation
// The default language is English
var userLang = navigator.language || navigator.userLanguage;
var lang = "pl-pl";
// If Browser Language == English then switch to the Polish language.
// since there en-GB en-US ...
if(userLang.split('-')[0] == 'en'){
lang = "pl-pl";
}
// Check for localStorage support
if('localStorage' in window){
var usrLang = localStorage.getItem('uiLang');
if( typeof(usrLang) === 'string' && usrLang !== 'undefined') {
lang = usrLang
}
}
var arrLang = {
"pl-pl": {
"valentine": "Do Walentynek zostało już tylko",
"days": " 10 dni"
},
"en-gb": {
"valentine": "Remaining days to Valentines!",
"days": " 10 days"
}
}
$(".lang").each(function(index, element) {
$(this).text(arrLang[lang][$(this).attr("key")]);
});
// get/set the selected language
$(".translate").click(function(e) {
e.stopPropagation();
var lang = $(this).attr("id");
// update localStorage key
if('localStorage' in window){
localStorage.setItem('uiLang', lang);
}
$(".lang").each(function(index, element) {
$(this).text(arrLang[lang][$(this).attr("key")]);
});
});
});
屬性名稱區分大小寫。
您在arrLang
使用的波蘭語的財產名稱與英語的財產名稱不同。 pl-pl
是Valentine
而en-gb
是valentine
。 但是您的key="valentine"
在pl-pl
尋找后者(后者不存在)。
將它們都小寫,等等。
var arrLang = {
"pl-pl": {
"valentine": "Do Walentynek zostało już tylko",
},
"en-gb": {
"valentine": "Remaining days to Valentines!",
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.