Revision: 2978
https://sourceforge.net/p/mrbs/code/2978/
Author: cimorrison
Date: 2015-01-22 10:13:40 +0000 (Thu, 22 Jan 2015)
Log Message:
-----------
Moved locale_lookup() out into a class
Modified Paths:
--------------
mrbs/trunk/web/language.inc
Added Paths:
-----------
mrbs/trunk/web/lib/Locale.php
Modified: mrbs/trunk/web/language.inc
===================================================================
--- mrbs/trunk/web/language.inc 2015-01-22 09:50:33 UTC (rev 2977)
+++ mrbs/trunk/web/language.inc 2015-01-22 10:13:40 UTC (rev 2978)
@@ -852,61 +852,6 @@
}
-if (!function_exists('locale_lookup'))
-{
- // Emulates the PHP locale_lookup() function
-
- // Searches the items in the array $langtag for the best match to the
language range
- // specified in $locale according to RFC 4647's lookup algorithm. The
langtags and
- // locale can have subtags separated by '-' or '_' and the search is case
insensitive.
- // Charsets (eg '.UTF-8') are stripped off $locale
- //
- // Returns the best match, or else an empty string if no match
- function locale_lookup($langtag, $locale, $canonicalize = FALSE)
- {
- if (!empty($canonicalize))
- {
- throw new Exception('MRBS: the MRBS version of Locale::lookup() does not
yet support $canonicalize = TRUE');
- }
-
- if (func_num_args() > 3)
- {
- throw new Exception('MRBS: optional fourth parameter to Locale::lookup()
not yet supported');
- }
-
- // Get the langtags and locale in the same format, ie separated by '-' and
- // all lower case
- $standard_langtags = locale_standardise($langtag);
- // Strip off any charset (eg '.UTF-8');
- $locale = preg_replace('/\..*$/', '', $locale);
- $standard_locale = locale_standardise($locale);
-
- // Look for a match. If there isn't one remove the last subtag from the
end
- // of the locale and try again.
- while (FALSE === ($index = array_search($standard_locale,
$standard_langtags)))
- {
- if (FALSE === ($pos = strrpos($standard_locale, '-')))
- {
- return '';
- }
- $standard_locale = substr($standard_locale, 0, $pos);
- }
-
- return $langtag[$index]; // Return the match in its original format
- }
-
- // Converts $langtag, which can be a string or an array, into a standard
form with
- // subtags all in lower case and separated by '-';
- function locale_standardise($langtag)
- {
- $glue = ',';
- $result = (is_array($langtag)) ? implode($glue, $langtag) : $langtag;
- $result = strtolower(str_replace('_', '-', $result));
- return (is_array($langtag)) ? explode($glue, $result) : $result;
- }
-}
-
-
// Get the best fit language file from $dir given the set of language
preferences
// $lang_preferences (an associative array language => quality_value). If
set,
// $default_langtags is an array of languages that are already built into the
system
@@ -970,10 +915,10 @@
// Find the best fit.
foreach ($lang_preferences as $lang => $qual)
{
- // For some reason the third parameter to locale_lookup() is mandatory in
5.3.0.
+ // For some reason the third parameter to Locale::lookup() is mandatory in
5.3.0.
// Can't see anything about it in the manual - must be a bug that was
fixed in
// later versions.
- $locale = locale_lookup($available_langtags, $lang, $canonicalize = FALSE);
+ $locale = Locale::lookup($available_langtags, $lang, $canonicalize =
FALSE);
if (!empty($locale))
{
if (in_array($locale, $default_langtags))
Added: mrbs/trunk/web/lib/Locale.php
===================================================================
--- mrbs/trunk/web/lib/Locale.php (rev 0)
+++ mrbs/trunk/web/lib/Locale.php 2015-01-22 10:13:40 UTC (rev 2978)
@@ -0,0 +1,57 @@
+<?php
+// *$Id*
+
+// Emulates the PHP Locale class, for those sites that do not have the Intl
extension installed.
+// The class will only be found by the autoloader if the global Locale class
doesn't exist.
+class Locale
+{
+
+ // Searches the items in the array $langtag for the best match to the
language range
+ // specified in $locale according to RFC 4647's lookup algorithm. The
langtags and
+ // locale can have subtags separated by '-' or '_' and the search is case
insensitive.
+ // Charsets (eg '.UTF-8') are stripped off $locale
+ //
+ // Returns the best match, or else an empty string if no match
+ public static function lookup($langtag, $locale, $canonicalize = FALSE)
+ {
+ if (!empty($canonicalize))
+ {
+ throw new Exception('MRBS: the MRBS version of Locale::lookup() does not
yet support $canonicalize = TRUE');
+ }
+
+ if (func_num_args() > 3)
+ {
+ throw new Exception('MRBS: optional fourth parameter to Locale::lookup()
not yet supported');
+ }
+
+ // Get the langtags and locale in the same format, ie separated by '-' and
+ // all lower case
+ $standard_langtags = self::standardise($langtag);
+ // Strip off any charset (eg '.UTF-8');
+ $locale = preg_replace('/\..*$/', '', $locale);
+ $standard_locale = self::standardise($locale);
+
+ // Look for a match. If there isn't one remove the last subtag from the
end
+ // of the locale and try again.
+ while (FALSE === ($index = array_search($standard_locale,
$standard_langtags)))
+ {
+ if (FALSE === ($pos = strrpos($standard_locale, '-')))
+ {
+ return '';
+ }
+ $standard_locale = substr($standard_locale, 0, $pos);
+ }
+
+ return $langtag[$index]; // Return the match in its original format
+ }
+
+ // Converts $langtag, which can be a string or an array, into a standard
form with
+ // subtags all in lower case and separated by '-';
+ private static function standardise($langtag)
+ {
+ $glue = ',';
+ $result = (is_array($langtag)) ? implode($glue, $langtag) : $langtag;
+ $result = strtolower(str_replace('_', '-', $result));
+ return (is_array($langtag)) ? explode($glue, $result) : $result;
+ }
+}
\ No newline at end of file
Property changes on: mrbs/trunk/web/lib/Locale.php
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits