Javascript: How to extract language from <html lang="...">
If your have a HTML page with the lang attribute set:
example.html
<html lang="de">
<!-- ... -->
</html>you can extract the language code using Javascript like this:
extract_lang.js
const lang = document.documentElement.lang;In case you want to specify a default language, use this variant:
extract_lang_with_default.js
const lang = document.documentElement.lang || 'en';This will default to 'en' if the lang attribute is not set.
Check out similar posts by category:
Javascript, Internationalization
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow