By default there is no way to access the language through JS in Drupal 7. You could of course read the lang
attribute of the <html>
tag, but that is far from being an optimal solution.
Luckily, this is pretty easy to achieve through a custom module. Here's what you need to do:
- Hook into every page load - both front and back end.
- Get the current language code.
- Make it available as JS variable.
This code would go in YOUR_MODULE.module
file:
You can then access this in JS the following way:
Drupal.settings.YOUR_MODULE.language
Drupal 8 already includes this and you can use it the following way:
drupalSettings.path.currentLanguage;