How to make the site remember the choice? - html

I made a simple website that includes two language versions, one Turkish and one English, my index page is a page that offers Turkish | English link directing into index-tr and index-en.
My question is how to make the index page remember what did the certain IPs selected when they first accessed the site. I mean, if I selected Turkish for the first time when I access the site another time it should remember that I've selected Turkish and should not direct me to the index page but instead, it should open index-tr.
I've used HTML and CSS only, no Javascript included.
I hope I made it clear.
Thanks in advance.

You can't store properties in HTML or CSS to be remembered for a period of time.
You have to use JavaScript or something server side, like PHP.
With JavaScript you could use localStorage.
The localStorage property allows you to access a local Storage object.
localStorage is similar to sessionStorage. The only difference is
that, while data stored in localStorage has no expiration time, data
stored in sessionStorage gets cleared when the browsing session
ends—that is, when the browser is closed.
You can write something like this
localStorage.setItem('language', 'tr') // Turkish language
You can overwrite this value by just overwriting the language with setItem()
localStorage.setItem('language', 'en') // English language
Now you can do something with it.
if(localStorage.getItem('language') == 'tr') {
// Language it Turkish, redirect or what ever JS code here.
} else {
// Language is not Turkish, but something other then 'tr'
}
More documentation and example's you can find here

I don't think this is possible with HTML CSS only, you have to use JavaScript,
Use localStorage
if (typeof(Storage) !== "undefined"){
localStorage.setItem('lang', 'tur');
} else localStorage.getItem('lang')
MDN

You can't do it with html and css, you have to do it with or at the server
(cookies in php)

Related

React and multilingual site: good pratices / SEO

I have a site done with React. On this site, two languages are available: 'en' and 'fr'. Here is how I implement it:
When the user comes on my site for the first time, I determine the language from its browser and I redirect it to www.mysite.com/fr or www.mysite.com/en.
The user is also free to change the language or directly access to the URL containing the language (example: www.mysite.com/fr).
Each time the language change, I store it in the local storage. This way, the user can comeback to www.mysite.com and I will redirect him to the correct URL containing the language (example: www.mysite.com/fr).
I don't specify 'lang' attribute on 'html' tag in my 'index.html' but when I know the language, I use Javascript to add this attribute dynamically.
I defined two 'hreflang' in the 'index.html'.
I already noticed two small problems with this implementation:
When I analyze my site with https://validator.w3.org/, I've got a warning telling me that 'lang' attribute is missing. I guess, this validator doesn't execute the Javascript. Can I considered it as a false positive ?
The browser extension 'Google Translate' seems to consider www.mysite.com/fr as an English page. I guess the extension doesn't wait enough to see the dynamic update of 'lang' attribute.
Is there is a way to avoid these problems ? Do you see other problems in my implementation ?

HTML only way to add a new query string to an existing one

If I start out on the page example.com/example and click and a link <a href="?this">, I get sent to example.com/example?this. But if I start out on example.com/example?that and click <a href="?this">, ?this overwrites ?that and I get sent to example.com/example?this.
Is there an HTML only (no javascript) way to have a link send me to example.com/example?this&that if I start out on example.com/example?that but send me to example.com/example?this if I start out on example.com/example?
No, there isn't. You will have to use a server-side language or JavaScript to build the hrefs in order to achieve this.
The better question is whether it is really a good idea to heavily rely on query parameters or whether you could make use of some .htaccess URL rewriting (that is internally mapped to query parameters, but they will always be available as long as the URL structure is intact). You could also make use of session storage in your server-side language.
No, html urls are relative to the path you are currently on, not including query strings. You will need javascript.

Select country and language in HTML

I'm currently building my site, I want to know how to do this...
Basically, when your press that country, it ask for the language available to you, and then it will redirect to that language page.
How could I do that? like airlines website, you choose a country, then language and then it directs to that language page?
This will usually require some server-side processing like PHP. When the user selects a language from the form, the language choice is stored in the session and recalled on each page load.
Then you would typically have internationalization strings in your web application. The chosen language is used to select the correct i18n strings from the index.
Using plain html only, with no server-side processing, the language selection form would simply (possibly via javascript) redirect to a document root for the language. This requires you to have a full site coded in each language.
http://www.example.com
http://www.example.com/en/
http://www.example.com/de/
You could do this in many different ways.
Here's a simple way to do it:
Have a bunch of links, each a different country. When you click on one, such as:
<a href="#" onclick="getLanguages('france')" >France</a>
This will call the JavaScript Function 'getLanguages()' shown here:
<script>
function getLanguages(country){
if(country == "france"){
document.getElementById("languages").innerHTML = "<a href='/french.html'>French</a><a href='/english.html'>English</a><a href='/german.html'>German</a>";
}else if(country == "china"){
document.getElementById("languages").innerHTML = "<a href='/chinese.html'>Chinese</a><a href='/english.html'>English</a><a href='/japanese.html'>Japanese</a>";
}
}
</script>
Obviously you'd have many more if statements for each country represented. This will basically modify an element with the ID of "languages":
<div id="languages"></div>
to include the possible languages for that country. Then when a user clicks on one of those links, it will take them to the correct page!
Hope this helps and good luck!

Localizing a Google Chrome Web App

I'm trying to add localization support to a Google Chrome Web App and, while it is easy to define strings for manifest and CSS files, it is somewhat more difficult for HTML pages.
In the manifest and in CSS files I can simply define localization strings like so:
__MSG_name__
but this doesn't work with HTML pages.
I can make a JavaScript function to fire onload that does the job like so:
document.title = chrome.i18n.getMessage("name");
document.querySelector("span.name").innerHTML = chrome.i18n.getMessage("name");
but this seems awfully ineffecient. Furthermore, I would like to be able to specify the page metadata; application-name and description, pulling the values from the localization files. What would be the best way of doing all this?
Thanks for your help.
Please refer to this documentation:
http://code.google.com/chrome/extensions/i18n.html
If you want to add localized content within HTML, you would need to do it via JavaScript as you mentioned before. That is the only way you can do it.
chrome.i18n.getMessage("name")
It isn't inefficient to do that, you can place your JavaScript at the end of the document (right before the end body tag) and it will fill up the text with respect to the locale.
Dunno if i understand exactly what you are trying to do but you could dynamically retrieve the LANG attribute (using .getAttribute("lang") or .lang) of the targeted tag and serve accordingly the proper values.

REST/Ajax deep linking compatibility - Anchor tags vs query string

So I'm working on a web app, and I want to filter search results.
A nice restful implementation might look like this:
1. mysite.com/clothes/men/hats+scarfs
But lets say we want to ajax up the filtering, like the cool kids, and we want to retain deep linking, we might use the anchor tag and parse that with Javascript to show the correct listings:
2. mysite.com/clothes#/men/hats+scarfs
However, if someone clicks the first link with JS enabled, and then changes filters, we might get:
3. mysite.com/clothes/men/hats+scarfs#/women/shoes
Urk.
Similarly, if someone does not have JS enabled, and clicks link 2 - JS will not parse the options and the correct listings will not be shown.
Are Ajax deep links and non-Ajax links incompatible? It would seem so, as servers cannot parse the # part of a url, since it is not sent to the server.
There's a monkeywrench being thrown into this issue by Google: A proposal for making Ajax crawlable. Google is including recommendations for url structure there that may give you ideas for your own application.
Here's the wrapup:
In summary, starting with a stateful
URL such as
http://example.com/dictionary.html#AJAX
, it could be available to both
crawlers and users as
http://example.com/dictionary.html#!AJAX
which could be crawled as
http://example.com/dictionary.html?_escaped_fragment_=AJAX
which in turn would be shown to users
and accessed as
http://example.com/dictionary.html#!AJAX
View Google's Presentation here (note: google docs presentation)
In general I think it's useful to simply turn off JavaScript and CSS entirely and browse your website and web application and see what ends up getting exposed. Once you get a sense of what's visible, you will understand what most search engines see and that in turn will show you what is and is not getting spidered.
If you go to mysite.com/clothes/men/hats+scarfs with JavaScript enabled then your JavaScript should automatically rewrite that to mysite.com/clothes#men/hats+scarfs - when you click on a filter, they should be controlled by JavaScript meaning you'll only change the hashtag rather than the entire URL (as you're going to have return false anyway).
The problem you have is for non-JS users going to your JS enabled deeplinks as the server can't determine that stuff. Unfortunately, the only thing you can do is take them to mysite.com/clothes and make them start their journey again (as far as I'm aware). You'll need to try and ensure that when people link to the site, they use the hardcoded deeplink rather than the hashed deeplink
I don't recommend ever using the query string as you are sending data back to the server without direct relevance to the prior specified destination. That is a corruptible security hole as malicious code can be manually added to the query string to cause a XSS or buffer overflow attack at your webserver.
I believe REST was intended to work with absolute URIs without a query string, because then your specifying only a location of a resource and it is that location that is descriptive and semantically relevant in addition to the possibility of the resource being so equally relevant. Even if there is no resource at the specified path you have still instantiated a potentially unique and descriptive location that can be processed accordingly.
Users entering the site via deep links
Nonsensical links (like /clothes/men/hats#women/shoes) can be avoided if you construct your Ajax initialisation code in such a way that users who enter the site on filtered pages (e.g. /clothes/women/shoes) are taken to the /clothes page before any Ajax filtering happens. For example, you might do something like this (using jQuery):
$("a.filter")
.each(function() {
var href = $(this).attr("href").replace("/clothes/", "/clothes#");
$(this).attr("href", href);
})
.click(function() {
update_filter($(this).attr("href").split("#")[1]);
});
Users without JavaScript
As you said in the question, there's no way for the server to know about the URL fragment so filtering would not be applied for users without JavaScript enabled if they were given a link to /clothes#filter.
However, even without filtering, these links could be made more meaningful for non-JS users by using the filter strings as IDs in your /clothes page. To prevent this messing with the Ajax experience the IDs would need to be changed (or the elements removed) with JavaScript before the Ajax links were initialised.
How practical this is depends on how many categories you have and what your /clothes page contains.