PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Internationalization » I18Nv2 » Bug #5256

No way to fetch all matching languages

Details

Request #5256No way to fetch all matching languages
Submitted2005-08-31 12:57 UTC
Fromo dot persson at gmail dot com
Assignedmike
StatusWont fix
PackageI18Nv2
PHP Version5.0.5
OSDebian 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);
}

/**