[php-src] PR #25914: Implement GH-20393: Locale::composeLocale() option to keep keywords
| From: | ChrisAbner | Date: | Mon, 30 Nov 2026 00:22:04 +0000 |
| Subject: | [php-src] PR #25914: Implement GH-20393: Locale::composeLocale() option to keep keywords | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41223@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25914
Author: ChrisAbner
Implements GH-20393.
**The problem**
Locale::composeLocale() only reads the subtag keys (language, script, region, variant,
extlang, private, grandfathered) and drops everything else. So the keywords returned by
Locale::getKeywords() cannot be composed back. There is no way to change the language
or region of a locale and keep its keywords:
```php
$locale = 'en_US@timezone=Europe/Amsterdam';
$parts = [...Locale::parseLocale($locale), ...Locale::getKeywords($locale),
...Locale::parseLocale('nl_NL')];
var_dump(Locale::composeLocale($parts));
// string(5) "nl_NL"
```
**The fix**
This adds an optional $flags argument to Locale::composeLocale() and
locale_compose(), with one flag, Locale::COMPOSE_PRESERVE_SUBTAGS. With
it, every string key that is not a subtag key is added as a keyword:
```php
var_dump(Locale::composeLocale($parts, Locale::COMPOSE_PRESERVE_SUBTAGS));
// string(31) "nl_NL@timezone=Europe/Amsterdam"
```
- The keywords are added with uloc_setKeywordValue(), so ICU validates and sorts them.
There is no fixed list of keyword names.
- Keys and values in BCP 47 form are converted to the legacy form that getKeywords()
returns, with uloc_toLegacyKey() and uloc_toLegacyType().
'tz' => 'nlams' gives @timezone=Europe/Amsterdam.
Legacy values keep their case (EUR, Buddhist), as in
getKeywords() and canonicalize().
- For a BCP 47 tag with a private use part, getKeywords() returns it as an
x keyword and parseLocale() returns it as private0. The
x keyword is skipped when the array has private keys, so the private part is not
written twice. Without private keys it is added as private subtags.
- An invalid keyword name or value returns false with an intl error, as the other parts do today. An
unknown flag throws a ValueError.
- Without the flag nothing changes.
@LamentXU123 suggested a flag in the issue, so that the current behavior of
locale_compose() stays as it is. I kept that name. I made it a class constant because
every constant added to intl since the move to stubs is a class constant, and the procedural
functions of the other classes take class constants too (collator_sort() with
Collator::SORT_REGULAR). I can make it a global
LOCALE_COMPOSE_PRESERVE_SUBTAGS if you prefer that. The draft by @devnexen, #20422,
adds the timezone keyword alone. This version handles any keyword, because ICU already knows how to
validate and sort them. If you prefer to continue with #20422, I can close this one.
**The other part of the issue**
getKeywords() already reads the BCP 47 extensions.
Locale::getKeywords('en-US-u-tz-nlams') returns ['timezone'
=> 'Europe/Amsterdam'] on master. The example in the issue,
en_US-u-tz-nlams, mixes _ and -. For that string
uloc_openKeywords() stops at the _ and only sees en, while
uloc_canonicalize() turns _ into - first. This is
[ICU-8395](https://unicode-org.atlassian.net/browse/ICU-8395), so I left getKeywords()
as it is. Writing the tag with -, or calling Locale::canonicalize() first,
works.
**Branch**
master, since this is a new feature.
**Verification**
| Input | master | This PR, with the flag |
|---|---|---|
| The parts from the issue | nl_NL | nl_NL@timezone=Europe/Amsterdam |
| ['language' => 'en', 'region' => 'US',
'tz' => 'nlams'] | en_US |
en_US@timezone=Europe/Amsterdam |
| Parts of de-DE-1996-u-co-phonebk | de_DE_1996 |
de_DE_1996@collation=phonebook |
| Parts of ja-JP-u-ca-japanese-x-foo | ja_JP_x_foo |
ja_JP_x_foo@calendar=japanese |
| ['language' => 'en', 'cal endar' =>
'x'] | en | false, with an intl error |
- New test ext/intl/tests/locale_compose_preserve_subtags.phpt. It fails on master and
passes with the change.
- locale_bug74439.phpt counts the parameters by reflection, so it now expects 2 for
composeLocale(), as in #24091.
- ext/intl tests in a debug build with ASan and UBSan (ICU 74.2): 552 passed, 0 failed.
**Not covered**
- Not tested on 32-bit or with an old ICU. uloc_toLegacyKey() and
uloc_toLegacyType() exist since ICU 54, and master needs 57.1.
- A grandfathered tag is still returned as it is, without keywords.
- The private part keeps the _x_ form that composeLocale() already writes.
ICU reads that form as variants (Locale::canonicalize('ja_JP_x_foo') gives
ja_JP_X_FOO). That is the current behavior and this PR does not change it.
- parseLocale() drops the private subtags of one character after the first one.
Locale::parseLocale('en-US-x-a-b') returns only private0 =>
'a'. It is an older bug in add_array_entry(), so I left it for a
separate fix.