1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | This source file is subject to version 3.01 of the PHP license, |
6 : | that is bundled with this package in the file LICENSE, and is |
7 : | available through the world-wide-web at the following url: |
8 : | http://www.php.net/license/3_01.txt |
9 : | If you did not receive a copy of the PHP license and are unable to |
10 : | obtain it through the world-wide-web, please send a note to |
11 : | license@php.net so we can mail you a copy immediately. |
12 : +----------------------------------------------------------------------+
13 : | Authors: Andrei Zmievski <andrei@php.net> |
14 : +----------------------------------------------------------------------+
15 : */
16 :
17 : /* $Id: locale.c 253924 2008-02-28 14:16:25Z felipe $ */
18 :
19 : #include "php_unicode.h"
20 : #include "unicode/ubrk.h"
21 :
22 : static void php_canonicalize_locale_id(char **target, int32_t *target_len, char *locale, UErrorCode *status)
23 24 : {
24 24 : char *canonicalized = NULL;
25 24 : int32_t canonicalized_len = 128;
26 :
27 : while (1) {
28 24 : *status = U_ZERO_ERROR;
29 24 : canonicalized = erealloc(canonicalized, canonicalized_len + 1);
30 24 : canonicalized_len = uloc_canonicalize(locale, canonicalized, canonicalized_len, status);
31 24 : if (*status != U_BUFFER_OVERFLOW_ERROR) {
32 24 : break;
33 : }
34 0 : }
35 :
36 24 : canonicalized[canonicalized_len] = 0;
37 24 : *target = canonicalized;
38 24 : *target_len = canonicalized_len;
39 24 : }
40 :
41 : /* {{{ proto string locale_get_default(void) U
42 : Returns default locale */
43 : PHP_FUNCTION(locale_get_default)
44 0 : {
45 0 : if (zend_parse_parameters_none() == FAILURE) {
46 0 : return;
47 : }
48 :
49 0 : RETURN_STRING(UG(default_locale), 1);
50 : }
51 : /* }}} */
52 :
53 : /* {{{ proto bool locale_set_default(string locale) U
54 : Sets default locale */
55 : PHP_FUNCTION(locale_set_default)
56 24 : {
57 : char *locale;
58 : int locale_len;
59 24 : char *canonicalized = NULL;
60 24 : UErrorCode status = U_ZERO_ERROR;
61 :
62 24 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &locale, &locale_len) == FAILURE) {
63 0 : return;
64 : }
65 :
66 24 : php_canonicalize_locale_id(&canonicalized, &locale_len, locale, &status);
67 : /*
68 : * UTODO: is this right? canonicalization does not seem to perform locale
69 : * validation. See this for possible solution:
70 : * http://sourceforge.net/mailarchive/message.php?msg_id=11953411
71 : */
72 24 : if (U_FAILURE(status)) {
73 0 : php_error(E_WARNING, "Invalid locale: %s", locale);
74 0 : RETURN_FALSE;
75 : }
76 : /* don't bother if locales are identical */
77 24 : if (!strcmp(UG(default_locale), canonicalized)) {
78 0 : efree(canonicalized);
79 0 : RETURN_FALSE;
80 : }
81 24 : efree(UG(default_locale));
82 24 : UG(default_locale) = canonicalized;
83 24 : zend_reset_locale_deps(TSRMLS_C);
84 24 : RETURN_TRUE;
85 : }
86 : /* }}} */
87 :
88 :
89 : /*
90 : * Local variables:
91 : * tab-width: 4
92 : * c-basic-offset: 4
93 : * End:
94 : * vim600: noet sw=4 ts=4 fdm=marker
95 : * vim<600: noet sw=4 ts=4
96 : */
|