Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.11.4

Request #5256 No way to fetch all matching languages
Submitted: 2005-08-31 07:57 UTC Modified: 2005-10-03 05:54 UTC
From: o dot persson at gmail dot com Assigned: mike
Status: Assigned Package: I18Nv2
PHP Version: 5.0.5 OS: Debian GNU/Linux
Roadmaps: (Not assigned)    
Subscription  
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes. If this is not your bug, you can add a comment by following this link. If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: o dot persson at gmail dot com
New email:
PHP Version: Package Version: OS:

 

 [2005-08-31 07:57 UTC] o dot persson at gmail dot com
Description: ------------ Today you can fetch the best matched language. But there's no way to fetch the rest of the languages. This would be very interesting in some cases -- eg. when there's no translation available for the best matched language one can fall back to the second best. Attached is a small patch (against the current CVS version). Feel free to do whatever you like. I think it would be a great feature. ;) Test script: --------------- Index: ../components/PEAR/I18Nv2/Negotiator.php =================================================================== --- ../components/PEAR/I18Nv2/Negotiator.php (revision 59) +++ ../components/PEAR/I18Nv2/Negotiator.php (working copy) @@ -274,6 +274,20 @@ } /** + * Find Language matches + * + * @access public + * @return string + * @param array $langs + */ + function getLanguageMatches($langs = null) + { + $languages = $this->_getMatches($langs, $this->_acceptLanguage, + $this->_defaultLanguage); + return array_unique($languages); + } + + /** * Find locale match * * @access public @@ -303,13 +317,24 @@ if (!$haystack) { return $default; } + $matches = $this->_getMatches($needle, $haystack); + return (count($matches) > 0) ? array_shift($matches) : $default; + } + + /** + * Return an array of matched values from first and second parameter. + * + * @access private + * @return string + * @param array $needle + * @param array $haystack + */ + function _getMatches($needle, $haystack) + { if (!$needle) { - return array_shift($haystack); + return $haystack; } - if ($result = array_shift($a = array_intersect($haystack, $needle))) { - return $result; - } - return $default; + return array_intersect($haystack, $needle); } /**

Comments