How to detect web page language with no lang attribute - javascript

I have an extension and need to detect the language of webpage. I learned I can obtain "lang" to get the answer. However, lots of webpage don't have "lang" attribute in .
In this case, how can I know what language of the webpage is?
Thank you.

The language is set by the user's browser in the navigator object's language property. Here's an explanation from W3Schools.
var lang = navigator.language;

Related

How to make website be in the correct language while being opened

I'm creating different copies of my website and each one is in a different language. How to make the website open in the right language according depending on the language of the user? Is it possible in javascript?
To find the language with JS and redirect to that website try this:
var language = (window.navigator.userLanguage || window.navigator.language).split("-")[0];
window.location.href = language + ".example.com";
For me that would output en.example.com, while for others it may say es.example.com (Spanish), ko.example.com (Korean), etc.

Change input language according to user specified language

I am trying to type into the language specified by the user according to the language selected. I got the following URL for the same but it supports only Farsi.
Change input to Farsi.
Please can anyone suggest me how to get this done or are there any online sources by which the languages be converted to js files to be referred ?

Set language variable using html/js

I am currently creating on online version of a magazine. The magazine is made up of a series of html5 pages. On each page, the user is provided the choice to choose language,"En/Fr".
I would like to know how i can set the language to the chosen one for the whole magazine. For example, suppose I am on page1 and 1 choose lang 'Fr', the other pages too should be loading in Fr. And now, on page20 I choose lang 'En', all the pages should load in 'En'.
Is that possible using html/js??
Could you provide me with a solution.
Thanks
This solution can't be achieved using pure javascript and html, as the variables will be lost when the page is reloaded.
You may like to investigate a solution using cookies.
For your example.
To set language:
$.cookie("language", "fr");
To read language from cookie:
$.cookie("language")
To delete language:
$.cookie("language", null);
There are a few options: using a cookie, using sessionStorage or localStorage, and including language identifier in URLs (path part or query part). There is insufficient information (no info about server-side technology used etc.) to suggest a particular approach. In any case, if you want the pages to be found via search engines, each language version should have a URL of its own.

Various script type attributes and vanilla js, what makes difference in user agents

I usually write script tag without type attribute. However I saw around several different types of script. So I have tested with different types and all looked the same to me except I put wrong type intentionally.
<script>alert(1)</script>
<script type="text/javascript">alert(2);</script>
<script type="text/ecmascript">alert(3);</script>
<script type="application/ecmascript">alert(4);</script>
<script type="application/javascript">alert(5);</script>
<script type="foo/javascript">alert(6);</script>
<script type="text/html">alert(7);</script>
<script type="application/x-javascript">alert(8);</script>
Questions:
If script given without type attribute, as I assume, does it uses browser default type?
It looks like text/* is used for browsers and application/* is used for mobile applications. Am I right about it?
In script type attribute, what makes difference between javascript and ecmascript?
I asked question about between vanilla js and pure js, but the question has been removed. So my guess is they are the same(too obvious maybe). Are the really same?, then why use different names(Javascript, Vanilla Javascript)? By using vanilla script as an external resource, can we do browser independent javascripting?
1. If script given without type attribute, as I assume, does it uses browser default type?
Yes. All browsers always assumed it to be the case and it has now been normalized in HTML5. According to the w3.org the type attribute defaults to "text/javascript".
Relevant extract from the spec :
The type attribute gives the language of the script or format of the
data. If the attribute is present, its value must be a valid MIME
type. The charset parameter must not be specified. The default, which
is used if the attribute is absent, is "text/javascript".
So the best way to put your script, for all browsers, is this :
<script>alert(1);</script>
2. It looks like text/* is used for browsers and application/* is used for mobile applications. Am I right about it?
If the script element is included in an HTML document then the value of the type attribute should be omitted or "text/javascript", even in a mobile application. There were various cases in the past where other types were suggested but now the standard is "text/javascript".
3.In script type attribute, what makes difference between javascript and ecmascript?
There is no difference. As you might know ECMAScript is the name of the normalized standard upon which is based JavaScript. Not only is "text/javascript" the standard way of referring to javascript in the script element but your browser has only one JS engine. It can't choose to try and select another one depending of a flavor you might specify in the type attribute.
4. I asked question about between vanilla js and pure js, but the question has been removed. So my guess is they are the same(too obvious maybe). Are the really same?, then why use different names(Javascript, Vanilla Javascript)? By using vanilla script as an external resource, can we do browser independent javascripting?
"Vanilla something", in English, refers to the basic flavor of something. In computing it commonly refers to a language without additions or plugins. Here it means JavaScript without additional libraries (for example jQuery). This expression is often meant as a way to reassert many things are possible without libraries even while people often think the library is needed. The vanilla-js site humorously supports this argument and that's one more reason you may have often read the "vanilla js" expression. So, of course, "vanilla js" is just JavaScript, that is "pure js".
Note that "vanilla js" isn't especially "browser independent" as many js libraries have as primary goal to provide an uniform layer hiding differences between browsers (most often features available in all browsers except IE).

jQuery 18n plugin

I've got a jQuery i18n plugin init:
jQuery.i18n.properties({
name : 'appsConstants',
path : '/gadgets/',
mode : 'both',
language : 'en'
});
How to take out the language variable into browser line like a parameter?
You have several options here:
Read the language from the URL path or an URL query parameter by parsing window.location
Set the language in a cookie
Generate your init Javascript dynamically on the server and set the language there (storing it in the session on the server).
This article from Scott Hanselman talks about globalization (cultures/locale)
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx
I've used that for making my jQuery aware of the users culture (for use with a datepicker amongst others) I also downloaded the i18n jQuery plugin and registered that.
I went with the "Slightly Less Cheesy - Meta Tag" solution for "detecting" locale and registered a
<meta name="accept-language" content="en-US">
in the head section, then I could utilize that tag in my jQuery to figure out what locale/language to use.
The article is slightly outdated so some of the javascript needs an update here and there as an example Globalization.js has changed from $.global. to Globalize.
Bits and pieces like that you need to update to get it all working.

Categories