Translate Button on website - Saving the users language preference
up vote
-1
down vote
favorite
So with my limited JavaScript skills I have created a simple translate button. When you click the button it changes the text on the webpage you are on to either English or Chinese.
How would I go about making these changes stay when someone goes to different webpages or even refreshes the page? I have tried experimenting with the localStorage technique but to no luck.
Here is my JS code for the translate button:
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
For local storage I have added in this right under the var = aLangKeys = new array.
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
Thanks!
javascript html css
add a comment |
up vote
-1
down vote
favorite
So with my limited JavaScript skills I have created a simple translate button. When you click the button it changes the text on the webpage you are on to either English or Chinese.
How would I go about making these changes stay when someone goes to different webpages or even refreshes the page? I have tried experimenting with the localStorage technique but to no luck.
Here is my JS code for the translate button:
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
For local storage I have added in this right under the var = aLangKeys = new array.
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
Thanks!
javascript html css
Can you please include your attempt with usingLocalStorage
as this will give myself and others something to work with rather than having to write a full solution for you. Thank you.
– NewToJS
Nov 18 at 23:56
Sorry! I have added in what I have done for LocalStorage
– Brock Erwin
Nov 19 at 0:01
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So with my limited JavaScript skills I have created a simple translate button. When you click the button it changes the text on the webpage you are on to either English or Chinese.
How would I go about making these changes stay when someone goes to different webpages or even refreshes the page? I have tried experimenting with the localStorage technique but to no luck.
Here is my JS code for the translate button:
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
For local storage I have added in this right under the var = aLangKeys = new array.
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
Thanks!
javascript html css
So with my limited JavaScript skills I have created a simple translate button. When you click the button it changes the text on the webpage you are on to either English or Chinese.
How would I go about making these changes stay when someone goes to different webpages or even refreshes the page? I have tried experimenting with the localStorage technique but to no luck.
Here is my JS code for the translate button:
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
For local storage I have added in this right under the var = aLangKeys = new array.
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
Thanks!
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
javascript html css
javascript html css
edited Nov 19 at 0:00
asked Nov 18 at 23:53
Brock Erwin
44
44
Can you please include your attempt with usingLocalStorage
as this will give myself and others something to work with rather than having to write a full solution for you. Thank you.
– NewToJS
Nov 18 at 23:56
Sorry! I have added in what I have done for LocalStorage
– Brock Erwin
Nov 19 at 0:01
add a comment |
Can you please include your attempt with usingLocalStorage
as this will give myself and others something to work with rather than having to write a full solution for you. Thank you.
– NewToJS
Nov 18 at 23:56
Sorry! I have added in what I have done for LocalStorage
– Brock Erwin
Nov 19 at 0:01
Can you please include your attempt with using
LocalStorage
as this will give myself and others something to work with rather than having to write a full solution for you. Thank you.– NewToJS
Nov 18 at 23:56
Can you please include your attempt with using
LocalStorage
as this will give myself and others something to work with rather than having to write a full solution for you. Thank you.– NewToJS
Nov 18 at 23:56
Sorry! I have added in what I have done for LocalStorage
– Brock Erwin
Nov 19 at 0:01
Sorry! I have added in what I have done for LocalStorage
– Brock Erwin
Nov 19 at 0:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
LocalStorage
only allows storing strings so you need to use JSON.stringify()
and JSON.parse()
when getting and setting.
But first you shouldn't be using new Array()
for non numeric keys. What you are really doing is making an array like object when you do that
Change to:
var aLangKeys = {
en :{},
ch: {}
};
// .... other definitions
To set
localStorage.setItem("aLangKeys", JSON.stringify(aLangKeys));
To get
var someVarName = JSON.parse(localStorage.getItem("aLangKeys"));
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
LocalStorage
only allows storing strings so you need to use JSON.stringify()
and JSON.parse()
when getting and setting.
But first you shouldn't be using new Array()
for non numeric keys. What you are really doing is making an array like object when you do that
Change to:
var aLangKeys = {
en :{},
ch: {}
};
// .... other definitions
To set
localStorage.setItem("aLangKeys", JSON.stringify(aLangKeys));
To get
var someVarName = JSON.parse(localStorage.getItem("aLangKeys"));
add a comment |
up vote
1
down vote
LocalStorage
only allows storing strings so you need to use JSON.stringify()
and JSON.parse()
when getting and setting.
But first you shouldn't be using new Array()
for non numeric keys. What you are really doing is making an array like object when you do that
Change to:
var aLangKeys = {
en :{},
ch: {}
};
// .... other definitions
To set
localStorage.setItem("aLangKeys", JSON.stringify(aLangKeys));
To get
var someVarName = JSON.parse(localStorage.getItem("aLangKeys"));
add a comment |
up vote
1
down vote
up vote
1
down vote
LocalStorage
only allows storing strings so you need to use JSON.stringify()
and JSON.parse()
when getting and setting.
But first you shouldn't be using new Array()
for non numeric keys. What you are really doing is making an array like object when you do that
Change to:
var aLangKeys = {
en :{},
ch: {}
};
// .... other definitions
To set
localStorage.setItem("aLangKeys", JSON.stringify(aLangKeys));
To get
var someVarName = JSON.parse(localStorage.getItem("aLangKeys"));
LocalStorage
only allows storing strings so you need to use JSON.stringify()
and JSON.parse()
when getting and setting.
But first you shouldn't be using new Array()
for non numeric keys. What you are really doing is making an array like object when you do that
Change to:
var aLangKeys = {
en :{},
ch: {}
};
// .... other definitions
To set
localStorage.setItem("aLangKeys", JSON.stringify(aLangKeys));
To get
var someVarName = JSON.parse(localStorage.getItem("aLangKeys"));
answered Nov 19 at 0:23
charlietfl
137k1285118
137k1285118
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366652%2ftranslate-button-on-website-saving-the-users-language-preference%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you please include your attempt with using
LocalStorage
as this will give myself and others something to work with rather than having to write a full solution for you. Thank you.– NewToJS
Nov 18 at 23:56
Sorry! I have added in what I have done for LocalStorage
– Brock Erwin
Nov 19 at 0:01