Home » Internationalization » I18Nv2 » Bug #5256
No way to fetch all matching languages
Details
| Request #5256 | No way to fetch all matching languages |
|---|---|
| Submitted | 2005-08-31 12:57 UTC |
| From | o dot persson at gmail dot com |
| Assigned | mike |
| Status | Wont fix |
| Package | I18Nv2 |
| PHP Version | 5.0.5 |
| OS | Debian GNU/Linux |
| Roadmaps | (Not assigned) |
Comments
[2005-08-31 12: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);
}
/**