]> granicus.if.org Git - php/commitdiff
add locale_accept_from_http
authorStanislav Malyshev <stas@php.net>
Wed, 23 Jul 2008 18:45:10 +0000 (18:45 +0000)
committerStanislav Malyshev <stas@php.net>
Wed, 23 Jul 2008 18:45:10 +0000 (18:45 +0000)
ext/intl/intl_data.h
ext/intl/locale/locale_class.c
ext/intl/locale/locale_methods.c
ext/intl/locale/locale_methods.h
ext/intl/php_intl.c
ext/intl/tests/ut_common.inc

index b3d729a5dde4704deb8e2e20191c396cc6368fa8..40b1d21dad9e3064ea3a08cc3a1b597150d357ea 100755 (executable)
@@ -45,6 +45,15 @@ typedef struct _intl_data {
        obj = (oclass##_object *) zend_object_store_get_object( object TSRMLS_CC );     \
     intl_error_reset( INTL_DATA_ERROR_P(obj) TSRMLS_CC );                                              \
 
+// Check status by error code, if error - exit
+#define INTL_CHECK_STATUS(err, msg)                                                                                     \
+    intl_error_set_code( NULL, (err) TSRMLS_CC );                                                       \
+    if( U_FAILURE((err)) )                                                                                                      \
+    {                                                                                                                                           \
+        intl_errors_set_custom_msg( NULL, msg, 0 TSRMLS_CC );                           \
+        RETURN_FALSE;                                                                                                           \
+    }
+
 #define INTL_METHOD_CHECK_STATUS(obj, msg)                                                                                     \
     intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) TSRMLS_CC );                                \
     if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) )                                                                     \
index 329ce739dcf5826e4ab8682718ffb278b9e3ef7a..6b9ba90ff1ab3829b56e70c346ac1b935b96b881 100755 (executable)
@@ -89,6 +89,7 @@ function_entry Locale_class_functions[] = {
        ZEND_FENTRY( filterMatches, ZEND_FN( locale_filter_matches ), locale_3_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
        ZEND_FENTRY( lookup, ZEND_FN( locale_lookup ), locale_4_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
        ZEND_FENTRY( canonicalize, ZEND_FN( locale_canonicalize ), locale_1_arg , ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
+       ZEND_FENTRY( acceptFromHttp, ZEND_FN( locale_accept_from_http ), locale_1_arg , ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
        { NULL, NULL, NULL }
 };
 /* }}} */
index fa4de5ad834d19e92b26bbf28fe15528bb9610f4..7428af1eff6bce53aa41fc809e48f11761ce49f6 100755 (executable)
 #include <unicode/ustring.h>
 #include <unicode/udata.h>
 #include <unicode/putil.h>
+#include <unicode/ures.h>
 
 #include "php_intl.h"
 #include "locale.h"
 #include "locale_class.h"
 #include "locale_methods.h"
 #include "intl_convert.h"
+#include "intl_data.h"
 
 #include <zend_API.h>
 #include <zend.h>
@@ -420,12 +422,10 @@ static void get_icu_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAMETERS)
 }
 /* }}} */
 
-/* {{{
- * proto public static string Locale::getScript($locale) 
+/* {{{ proto static string Locale::getScript($locale) 
  * gets the script for the $locale 
  }}} */
-/* {{{
- * proto public static string locale_get_script($locale) 
+/* {{{ proto static string locale_get_script($locale) 
  * gets the script for the $locale 
  */
 PHP_FUNCTION( locale_get_script ) 
@@ -434,12 +434,10 @@ PHP_FUNCTION( locale_get_script )
 }
 /* }}} */
 
-/* {{{
- * proto public static string Locale::getRegion($locale) 
+/* {{{ proto static string Locale::getRegion($locale) 
  * gets the region for the $locale 
  }}} */
-/* {{{
- * proto public static string locale_get_region($locale) 
+/* {{{ proto static string locale_get_region($locale) 
  * gets the region for the $locale 
  */
 PHP_FUNCTION( locale_get_region ) 
@@ -448,12 +446,10 @@ PHP_FUNCTION( locale_get_region )
 }
 /* }}} */
 
-/* {{{
- * proto public static string Locale::getPrimaryLanguage($locale) 
+/* {{{ proto static string Locale::getPrimaryLanguage($locale) 
  * gets the primary language for the $locale 
  }}} */
-/* {{{
- * proto public static string locale_get_primary_language($locale) 
+/* {{{ proto static string locale_get_primary_language($locale) 
  * gets the primary language for the $locale 
  */
 PHP_FUNCTION(locale_get_primary_language ) 
@@ -589,12 +585,10 @@ static void get_icu_disp_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAME
 }
 /* }}} */
 
-/* {{{
-* public static string Locale::getDisplayName($locale, $in_locale = null)
+/* {{{ proto static string Locale::getDisplayName($locale[, $in_locale = null])
 * gets the name for the $locale in $in_locale or default_locale
  }}} */
-/* {{{
-* public static string get_display_name($locale, $in_locale = null)
+/* {{{ proto static string get_display_name($locale[, $in_locale = null])
 * gets the name for the $locale in $in_locale or default_locale
 */
 PHP_FUNCTION(locale_get_display_name) 
@@ -603,12 +597,10 @@ PHP_FUNCTION(locale_get_display_name)
 }
 /* }}} */
 
-/* {{{
-* public static string Locale::getDisplayLanguage($locale, $in_locale = null)
+/* {{{ proto static string Locale::getDisplayLanguage($locale[, $in_locale = null])
 * gets the language for the $locale in $in_locale or default_locale
  }}} */
-/* {{{
-* public static string get_display_language($locale, $in_locale = null)
+/* {{{ proto static string get_display_language($locale[, $in_locale = null])
 * gets the language for the $locale in $in_locale or default_locale
 */
 PHP_FUNCTION(locale_get_display_language) 
@@ -617,12 +609,10 @@ PHP_FUNCTION(locale_get_display_language)
 }
 /* }}} */
 
-/* {{{
-* public static string Locale::getDisplayScript($locale, $in_locale = null)
+/* {{{ proto static string Locale::getDisplayScript($locale, $in_locale = null)
 * gets the script for the $locale in $in_locale or default_locale
  }}} */
-/* {{{
-* public static string get_display_script($locale, $in_locale = null)
+/* {{{ proto static string get_display_script($locale, $in_locale = null)
 * gets the script for the $locale in $in_locale or default_locale
 */
 PHP_FUNCTION(locale_get_display_script) 
@@ -631,12 +621,10 @@ PHP_FUNCTION(locale_get_display_script)
 }
 /* }}} */
 
-/* {{{
-* public static string Locale::getDisplayRegion($locale, $in_locale = null)
+/* {{{ proto static string Locale::getDisplayRegion($locale, $in_locale = null)
 * gets the region for the $locale in $in_locale or default_locale
  }}} */
-/* {{{
-* public static string get_display_region($locale, $in_locale = null)
+/* {{{ proto static string get_display_region($locale, $in_locale = null)
 * gets the region for the $locale in $in_locale or default_locale
 */
 PHP_FUNCTION(locale_get_display_region) 
@@ -646,11 +634,11 @@ PHP_FUNCTION(locale_get_display_region)
 /* }}} */
 
 /* {{{
-* public static string Locale::getDisplayVariant($locale, $in_locale = null)
+* proto static string Locale::getDisplayVariant($locale, $in_locale = null)
 * gets the variant for the $locale in $in_locale or default_locale
  }}} */
 /* {{{
-* public static string get_display_variant($locale, $in_locale = null)
+* proto static string get_display_variant($locale, $in_locale = null)
 * gets the variant for the $locale in $in_locale or default_locale
 */
 PHP_FUNCTION(locale_get_display_variant) 
@@ -1334,8 +1322,12 @@ static int strToMatch(char* str ,char *retstr)
 }
 /* }}} */
 
-/*{{{
-* code used by locale_filter_maatches
+/* {{{ proto static boolean Locale::filterMatches(string $langtag, string $locale)
+* Checks if a $langtag filter matches with $locale according to RFC 4647's basic filtering algorithm 
+*/
+/* }}} */
+/* {{{ proto boolean locale_filter_matches(string $langtag, string $locale)
+* Checks if a $langtag filter matches with $locale according to RFC 4647's basic filtering algorithm 
 */
 PHP_FUNCTION(locale_filter_matches)
 {
@@ -1500,6 +1492,7 @@ PHP_FUNCTION(locale_filter_matches)
 
        }
 }
+/* }}} */
 
 static void array_cleanup( char* arr[] , int arr_size)
 {
@@ -1736,8 +1729,44 @@ PHP_FUNCTION(locale_lookup)
                efree( result);
        }
 }
+/* }}} */
 
-
+/* {{{ proto string Locale::acceptFromHttp(string $http_accept)
+* Tries to find out best available locale based on HTTP \93Accept-Language\94 header
+*/
+/* }}} */
+/* {{{ proto string locale_accept_from_http(string $http_accept)
+* Tries to find out best available locale based on HTTP \93Accept-Language\94 header
+*/
+PHP_FUNCTION(locale_accept_from_http)
+{
+       UEnumeration *available;
+       char *http_accept = NULL;
+       int http_accept_len;
+       UErrorCode status = 0;
+       int len;
+       char resultLocale[INTL_MAX_LOCALE_LEN+1];
+       UAcceptResult outResult;
+
+       if(zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &http_accept, &http_accept_len) == FAILURE)
+       {
+               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
+               "locale_accept_from_http: unable to parse input parameters", 0 TSRMLS_CC );
+               RETURN_NULL();
+       }
+       
+       available = ures_openAvailableLocales(NULL, &status);
+       INTL_CHECK_STATUS(status, "locale_accept_from_http: failed to retrieve locale list");
+       len = uloc_acceptLanguageFromHTTP(resultLocale, INTL_MAX_LOCALE_LEN, 
+                                               &outResult, http_accept, available, &status);
+       uenum_close(available);
+       INTL_CHECK_STATUS(status, "locale_accept_from_http: failed to find acceptable locale");
+       if(outResult == ULOC_ACCEPT_FAILED) {
+               RETURN_FALSE;
+       }
+       RETURN_STRINGL(resultLocale, len, 1);
+}
+/* }}} */
 
 /*
  * Local variables:
index 0b2db50f6c41f2d8b324860d351efc96530c26c9..3fc959b59f013dcd55bc67c17a603a5100f37d67 100755 (executable)
@@ -43,5 +43,6 @@ PHP_FUNCTION( locale_parse);
 PHP_FUNCTION( locale_filter_matches);
 PHP_FUNCTION( locale_lookup);
 PHP_FUNCTION( locale_canonicalize);
+PHP_FUNCTION( locale_accept_from_http);
 
 #endif // LOCALE_METHODS_H
index e601f3f819321149b5c9a78730f55981e5b3e74e..82984c47ca3a810aa656b1fff8e88c230725d9e1 100755 (executable)
@@ -272,6 +272,7 @@ zend_function_entry intl_functions[] = {
        PHP_FE( locale_parse, locale_1_arg )
        PHP_FE( locale_filter_matches, locale_filter_args )
        PHP_FE( locale_lookup, locale_lookup_args )
+       PHP_FE( locale_accept_from_http, locale_1_arg )
 
        // MessageFormatter functions
        PHP_FE( msgfmt_create, NULL )
index 2eaff1e165ebc1b8d277c5883656c1410e8c6aa6..74ad0a1ea81a0c0f09b896b072037a1ac07e3ecb 100755 (executable)
@@ -351,6 +351,8 @@ function ut_loc_locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_lo
 {
     return $GLOBALS['oo-mode'] ? Locale::lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc ) : locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc );
 }
+function ut_loc_accept_http($http) {
+       return $GLOBALS['oo-mode'] ? Locale::acceptFromHttp($http):locale_accept_from_http($http);^
 
 /* MessageFormatter functions */
 function ut_msgfmt_create( $locale, $pattern)