Skip to content

Commit

Permalink
feat: add INI setting for default_locale
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Oct 12, 2023
1 parent c872743 commit 18c577c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/php/ecma_intl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@
#include "php/classes/supported_locales_options.h"

#include <ext/standard/info.h>
#include <php_ini.h>
#include <unicode/ucal.h>

PHP_INI_BEGIN()
PHP_INI_ENTRY(PHP_ECMA_INI_DEFAULT_LOCALE, "", PHP_INI_ALL, NULL)
PHP_INI_END()

zend_module_entry ecma_intl_module_entry = {STANDARD_MODULE_HEADER,
"ecma_intl",
NULL,
PHP_MINIT(ecma_intl_all),
NULL,
PHP_MSHUTDOWN(ecma_intl),
PHP_RINIT(ecma_intl),
NULL,
PHP_MINFO(ecma_intl),
Expand All @@ -50,6 +55,8 @@ ZEND_GET_MODULE(ecma_intl)

PHP_MINIT_FUNCTION(ecma_intl_all)
{
REGISTER_INI_ENTRIES();

PHP_MINIT(ecma_intl)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(ecma_intl_category)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(ecma_intl_collator)(INIT_FUNC_ARGS_PASSTHRU);
Expand All @@ -64,6 +71,13 @@ PHP_MINIT_FUNCTION(ecma_intl_all)
return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(ecma_intl)
{
UNREGISTER_INI_ENTRIES();

return SUCCESS;
}

PHP_RINIT_FUNCTION(ecma_intl)
{
#if defined(ZTS) && defined(COMPILE_DL_ECMA_INTL)
Expand Down Expand Up @@ -91,4 +105,6 @@ PHP_MINFO_FUNCTION(ecma_intl)
php_info_print_table_row(2, "ICU TZData version", timeZoneDataVersion);
php_info_print_table_row(2, "ICU Unicode version", U_UNICODE_VERSION);
php_info_print_table_end();

DISPLAY_INI_ENTRIES();
}
2 changes: 2 additions & 0 deletions src/php/ecma_intl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ extern zend_module_entry ecma_intl_module_entry;
#define phpext_ecma_intl_ptr &ecma_intl_module_entry

#define PHP_ECMA_INTL_VERSION "0.3.0-dev"
#define PHP_ECMA_INI_DEFAULT_LOCALE "ecma_intl.default_locale"

#if defined(ZTS) && defined(COMPILE_DL_ECMA_INTL)
ZEND_TSRMLS_CACHE_EXTERN()
#endif

PHP_MINIT_FUNCTION(ecma_intl_all);
PHP_MSHUTDOWN_FUNCTION(ecma_intl);
PHP_RINIT_FUNCTION(ecma_intl);
PHP_MINFO_FUNCTION(ecma_intl);

Expand Down
14 changes: 14 additions & 0 deletions tests/phpt/ini-default_locale-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
ecma_intl.default_locale has an empty string when set to empty
--EXTENSIONS--
ecma_intl
--INI--
ecma_intl.default_locale=
--FILE--
<?php
declare(strict_types=1);

var_dump(ini_get('ecma_intl.default_locale'));

--EXPECT--
string(0) ""
14 changes: 14 additions & 0 deletions tests/phpt/ini-default_locale-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
ecma_intl.default_locale has a value when set to a valid value
--EXTENSIONS--
ecma_intl
--INI--
ecma_intl.default_locale=en-US
--FILE--
<?php
declare(strict_types=1);

var_dump(ini_get('ecma_intl.default_locale'));

--EXPECT--
string(5) "en-US"
14 changes: 14 additions & 0 deletions tests/phpt/ini-default_locale-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
ecma_intl.default_locale can be set to en-US-POSIX
--EXTENSIONS--
ecma_intl
--INI--
ecma_intl.default_locale=en-US-POSIX
--FILE--
<?php
declare(strict_types=1);

var_dump(ini_get('ecma_intl.default_locale'));

--EXPECT--
string(11) "en-US-POSIX"
14 changes: 14 additions & 0 deletions tests/phpt/ini-default_locale-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
ecma_intl.default_locale can recognize underscores in locale IDs
--EXTENSIONS--
ecma_intl
--INI--
ecma_intl.default_locale=en_US_POSIX
--FILE--
<?php
declare(strict_types=1);

var_dump(ini_get('ecma_intl.default_locale'));

--EXPECT--
string(11) "en_US_POSIX"
26 changes: 26 additions & 0 deletions tests/phpt/ini-default_locale-005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
ecma_intl.default_locale can be set from the application
--EXTENSIONS--
ecma_intl
--INI--
ecma_intl.default_locale=
--FILE--
<?php
declare(strict_types=1);

var_dump(ini_set('ecma_intl.default_locale', 'en-US'));
var_dump(ini_set('ecma_intl.default_locale', 'en_US'));
var_dump(ini_set('ecma_intl.default_locale', 'en-Latn-US'));
var_dump(ini_set('ecma_intl.default_locale', 'en-US-POSIX'));
var_dump(ini_set('ecma_intl.default_locale', 'en_US_POSIX'));
var_dump(ini_set('ecma_intl.default_locale', 'de'));
var_dump(ini_get('ecma_intl.default_locale'));

--EXPECT--
string(0) ""
string(5) "en-US"
string(5) "en_US"
string(10) "en-Latn-US"
string(11) "en-US-POSIX"
string(11) "en_US_POSIX"
string(2) "de"

0 comments on commit 18c577c

Please sign in to comment.