Last active
August 14, 2019 00:03
-
-
Save gregoriopellegrino/c94c46933d660a3db233e7a1c9c6f5e6 to your computer and use it in GitHub Desktop.
Very simple script in jQuery to find all the languages present in an HTML document. Useful for controlling the accessibility of documents. Improvements are welcome. #a11y Made to run from the browser development console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main lang | |
if ($("html").get(0).hasAttribute("lang")) { | |
var main_lang = $("html").attr("lang"); | |
} else { | |
var main_lang = $("body").attr("lang"); | |
} | |
console.log("Main language is "+main_lang); | |
// children lang | |
var langs = []; | |
$("*[lang]").each(function() { | |
var lang = $(this).attr("lang"); | |
if ($.inArray(lang, langs) == -1) { | |
langs.push(lang) | |
} | |
}); | |
console.log("Other languages found: "+langs); |
Also, if you want to check the validity of the BCP47 language tag (pattern) → https://github.com/SafetyCulture/bcp47
This has gone so far that I’m wondering whether it shouldn’t be a module (with a GitHub repo). I could even imagine having/finding some use-cases in the future.
My idea was simply to save a code that I use from time to time to check the accessibility of EPUB and PDF :) , but the work it's becoming interesting...
I’ve just pushed an invite – started working on completing this a little bit in a private repo – so it’s fair to enable access for you :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here you go, this one will check if language tag is valid, and whether values of
xml:lang
andlang
match.I refrained from refactoring too much so that the process be kinda clearer/commented but turned it into functions so that it can be refactored into a JS module more easily – so anyone please feel free to do that if re-using the logic (npm, etc.) and share it.