Hello all, I know this is an old post, but I needed a solution for my automatic language detection, so I made a little (probably terrible) change to the function. Could you please tell me if there is something better, or if there are errors? Now it seems to work for me:
public function getPreferredLanguage(array $locales = null) { $preferredLanguages = $this->getLanguages(); // this is not correct because id locales is empty it returns (i.e.) "en_US", not "en" and for browser is a problem if (empty($locales)) { return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null; } if (!$preferredLanguages) { return $locales[0]; } $preferredLanguagesIntersect = array_values(array_intersect($preferredLanguages, $locales)); $preferredLanguage = reset($preferredLanguagesIntersect); // get the first value resetting the pointer // seen that a browser could return an array with only (i.e.) "en_US" element, without "en" // we try to find the first two_character of the browser array element inside the locales array if (empty($preferredLanguagesIntersect)) { $firstLangChr = function($lang) { return substr($lang,0,2); }; $preferredLanguagesIntersect = array_values(array_intersect(array_map($firstLangChr,$preferredLanguages), $locales)); $preferredLanguage = reset($preferredLanguagesIntersect); } return isset($preferredLanguage) ? $preferredLanguage : $locales[0]; } Thank you very much, hope it could be a better solution Il giorno venerdì 8 ottobre 2010 22:03:48 UTC+2, halk ha scritto: > > Hello all, > > Fabien told us in his keynote on symfony day 2010 in Cologne today, > that Symfony2's new Translation component has a fallback for the > culture/locale built-in. Fabien gave this example: "fr_FR", "fr", > "en". If there is no translation "fr_FR", Symfony2 looks for "fr", if > not then "en" (as set in configuration). After Fabien's keynote > someone asked him whether the user can define the fallback order. > Fabien answered that it is not implemented yet. > > Does Symfony2 read the user's preferred languages from browser? In > Zend Framework I can tell ZF to read the browser preference: > > $translate = new Zend_Translate( > 'gettext', > APPLICATION_PATH . '/langs', > $locale->getLanguage(), > array( > 'scan' => Zend_Translate::LOCALE_FILENAME, > 'disableNotices' => true > ) > ); > > $locale->getLanguage() parses the $_SERVER['HTTP_ACCEPT_LANGUAGE'] > variable and returns an array of preferred languages. I believe that > if the customer/project asks for it and the developer tells Symfony2 > to do (f.e. as a parameter), Symfony2 should read this setting and > use it for selecting the translation and for fallback. > > F.e. if "de_DE", "tr_TR" and "en_GB" are the preferred languages set > in the user's browser, Symfony2 should first look for the translations > "de_DE", then "de". If still no match, it should try to find a > translation catalogue for the next preferred language, in this case > "tr_TR" and if not "tr" etc. If non of the preferred languages > matches, Symfony2 will take the culture defined in configuration. > > What do you think? I could write a patch if the idea sounds good for > you. -- If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com You received this message because you are subscribed to the Google Groups "symfony developers" group. To post to this group, send email to symfony-devs@googlegroups.com To unsubscribe from this group, send email to symfony-devs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/symfony-devs?hl=en