To get the currently selected language on a Drupal 8 site you need to run the following.
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
This will return the 2 character ISO language code for the language. So for English this would be "en".
To get the language name instead of the language code use this.
$language = \Drupal::languageManager()->getCurrentLanguage()->getName();
Comments
Short, cool and informative. Thank you!
Small remark,
To get the currently selected language for content :
$language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
Thank you Philip. I did not have the interface translation enabled so your snipped was always returning the default language. The snipped posted by "tty11" worked. Thanks tty11 :), however I will make a small change to it. Drupal couldn't find "LanguageInterface" in my theme's preprocess function, I had to provide namespace too as follows
$language = \Drupal::languageManager()->getCurrentLanguage(\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT)->getId();
Thanks Ejay. I wrote this post during my first few weeks of Drupal 8 development. I've since learned a lot about how the language manager works so there are probably a few contextual issues with my original snippet :)