Reputation: 5566
Hi I have a simple app where the user can select the language to use in my app between English and Polish.
When the user opens my website, this is the behavior I want:
Because it's a very simple app, I'm using jQuery for translation.
Below is my solution so far:
JSFIDDLE: demo
//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>
Unfortunately, my solution is not working. What is wrong with my code?
Upvotes: 0
Views: 884
Reputation: 24001
the userLang
return en-GB
so the language is two lowercase characters and the country two uppercase characters so the if statement should be if(userLang == 'pl-PL')
not if(userLang == 'pl-pl')
it will always return false by your way
BUT while you just need the language right? you can try This .. and always use the language in two lowercase characters .. no need to use pl-pl
or en-GB
you can use pl
or 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>
Upvotes: 1
Reputation: 2759
Property names are case sensitive.
Your property name for Polish in arrLang
is different to that of English. pl-pl
is Valentine
while en-gb
is valentine
. But your key="valentine"
looks for the latter in pl-pl
which does not exist.
Lowercase them both et voila.
var arrLang = {
"pl-pl": {
"valentine": "Do Walentynek zostało już tylko",
},
"en-gb": {
"valentine": "Remaining days to Valentines!",
}
}
Upvotes: 0
Reputation: 494
Try the following I hope it helps
$(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")]);
});
});
});
Upvotes: 1