From 22bf840f4b74c040296566871e253d0bb29eaf75 Mon Sep 17 00:00:00 2001 From: Sem Date: Tue, 30 Oct 2012 04:45:52 +0100 Subject: Added useragent language support. --- start.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'start.php') diff --git a/start.php b/start.php index 2b9c4e543..a481c8877 100644 --- a/start.php +++ b/start.php @@ -4,6 +4,27 @@ * */ +function languages_get_useragent_language() { + global $CONFIG; + if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) { + + register_translations($CONFIG->path . "languages/", true); + + $available_languages = array_keys($CONFIG->translations); + $accepted_languages = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]); + + foreach ($accepted_languages as $i => $accepted_language) { + $accepted_languages[$i] = trim(array_shift(preg_split("/[-;]/", $accepted_language))); + } + + $langs = array_intersect($accepted_languages, $available_languages); + if (count($langs) > 0) { + return array_shift($langs); + } + return false; + } +} + register_translations(elgg_get_plugins_path() . "languages/languages/es", true); register_translations(elgg_get_plugins_path() . "languages/languages/nl", true); register_translations(elgg_get_plugins_path() . "languages/languages/de", true); @@ -18,3 +39,10 @@ register_translations(elgg_get_plugins_path() . "languages/languages/ja", true); register_translations(elgg_get_plugins_path() . "languages/languages/sr", true); register_translations(elgg_get_plugins_path() . "languages/languages/th", true); register_translations(elgg_get_plugins_path() . "languages/languages/zh", true); + +if (!elgg_is_logged_in()) { + global $CONFIG; + if ($useragent_language = languages_get_useragent_language()) { + $CONFIG->language = $useragent_language; + } +} \ No newline at end of file -- cgit v1.2.3