Extensions: Get Browser Languages in Symphony 2.2
This is an open discussion with 4 replies, filed under Extensions.
Search
Dear,
I’m experimenting with symphony2.2beta, but the Language redirect extension is not working anymore, error: Fatal error: Call to undefined method Lang::getBrowserLanguages() in D:Websitessymphony22extensionslanguageredirecteventsevent.languageredirect.php
After removing this line, the redirect give the 404 error…
Anyone?
Thanks
Wannes
Hi Wannes,
the ‘Lang::getBrowserLanguages()’-method doesn’t exist anymore in Symphony 2.2. Before I get the time to update the extension you can just copy the method from Symphony < 2.2 into the extension and modify the places where the function gets called to (probably) something like $this->getBrowserLanguages
depending on where you put it.
Jonas,
thanks for the fast reply, it’s almost working now: http://www.mysite.com/fr/home/ is working, but http://www.mysite.com/fr/ gives error 404, i tried with a <2.2 htaccess but same problem.
Thx Wannes
Just for quick reference for everyone Lang: getBrowserLanguage() method is below
public static function getBrowserLanguages() { static $languages; if(is_array($languages)) return $languages; $languages = array(); if(strlen(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])) < 1) return $languages; if(!preg_match_all('/(w+(?:-w+)?,?)+(?:;q=(?:d+.d+))?/', preg_replace('/s+/', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']), $matches)) return $languages; $priority = 1.0; $languages = array(); foreach($matches[0] as $def){ list($list, $q) = explode(';q=', $def); if(!empty($q)) $priority=floatval($q); $list = explode(',', $list); foreach($list as $lang){ $languages[$lang] = $priority; $priority -= 0.000000001; } } arsort($languages); $languages = array_keys($languages); // return list sorted by descending priority, e.g., array('en-gb','en'); return $languages; }
Create an account or sign in to comment.
In preparation of version 2.2 I reviewed Symphony’s language management. While I added new functions for date localization I also found two functions that are redundant or never used by the system,
Lang::add()
andLang::getBrowserLanguages()
. These functions have been removed in the currentintegration
branch on GitHub but I like to discuss this for a moment:I couldn’t find any public extension using
Lang::add()
and it was a simple alias toLang::load()
. If anyone is using this function, please simply replaceadd()
withload()
and everything will continue working in Symphony 2.2.The second function returns an array sorted by priority of all languages accepted by the visitor’s browser. This function was once created to allow automatic language selection during installation, but due to the fact that Symphony never bundled languages in the core but used extensions instead this functionality could never really been used. Furthermore the returned language codes do not necessarily match Symphony’s naming convention for languages (e. g.
lang.de.php
). So Symphony does not usegetBrowserLanguages()
anywhere in the system but there are at least to extensions I am aware of that call it inside their code: Language Redirect and Language Field (though I’m not sure if the latter is still developed actively).I’d like to propose that those extension add the function directly to their codebase as the function isn’t used in the core — which wouldn’t be a big deal if we are talking about just these two extensions I guess.
My question is: are there other extensions relying on
getBrowserLanguages()
?