Skip to content

feat(libSceIcu): implement uloc_toLanguageTag - #20189

Merged
oneandonlydean merged 7 commits into
boykopovar:mainfrom
MatteoSid:feat/icu-uloc-to-language-tag
Nov 18, 2026
Merged

oneandonlydean merged 7 commits into
boykopovar:mainfrom
MatteoSid:feat/icu-uloc-to-language-tag

Conversation

@MatteoSid

@MatteoSid MatteoSid commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

uloc_toLanguageTag (zmMWRKwedmw) is a parameterless stub in libSceIcu that throws. The debt file says so in the BCP 47 entry. This PR gives it the ICU signature, int32_t (const char* localeID, char* langtag, int32_t capacity, UBool strict, UErrorCode* status). It writes a BCP 47 tag from an ICU locale ID, the reverse of uloc_forLanguageTag (#16872, on main). It follows ICU 68.2, the version the debt file already assigns to the 3.20 module and to uloc_forLanguageTag: ulocimp_toLanguageTag and its helpers in uloc_tag.cpp. The code goes into LanguageTag.cpp, which already carries the ICU licence notice; I added a Derived from line above it. The PR removes the stub and types the inventory declaration. Its fragment docs/dev/debt/changes/feat-icu-uloc-to-language-tag.md (#17217) removes the debt table row; the throwing count in the debt file goes from main's 50 to 49.

  • Same pipeline as ICU. The ID is canonicalized, then language, script, region, variants, keywords and private use are appended in ICU's order. Canonicalization and the language, script and country lookups call this module's own uloc_canonicalize, uloc_getLanguage, uloc_getScript and uloc_getCountry, with ICU's buffer sizes (12, 6, 4). The variant and the keyword list have no export on main. They follow ICU 68.2's uloc_getVariant and ulocimp_getKeywords/ulocimp_getKeywordValue (code).
  • Output rules. Deprecated languages (iw to he, …) and regions (DD to DE, …) use ICU 68.2's tables. Variants are lower-cased and sorted. A whole POSIX variant becomes -u-va-posix. A variant that is not a BCP 47 variant moves to -x-lvariant-. attribute keywords become sorted -u- attributes, and one-letter keywords become extensions, ordered as ICU's _addExtensionToList orders them. The private-use-only ID @x=… gives x-…. Strict mode fails with U_ILLEGAL_ARGUMENT_ERROR where ICU's does, and leaves the partial tag ICU leaves.
  • BCP 47 input. As in ICU 68.2's canonicalization, an ID with no @ whose shortest subtag has one character is first read with uloc_forLanguageTag, into a buffer sized from the length it reports (code), so en-x-elmer gives en-x-elmer.
  • What throws. A keyword other than attribute or a one-letter extension (calendar, va, …) throws through NotImplemented_nid_no_patch before any output is written. ICU maps those through the keyTypeData resource, which uloc_forLanguageTag already throws for. A null locale and a null buffer with a positive capacity throw an invalid-argument exception, as uloc_forLanguageTag does; a null status throws.

Reachability. Every argument is a plain value a title builds: a C string locale ID such as "en_US", a buffer, its size and a flag. A locale ID can also come from uloc_forLanguageTag, uloc_canonicalize or uloc_getBaseName, all on main, and the test round-trips tags through uloc_forLanguageTag. uloc_toLanguageTag is how ICU callers turn a locale into the tag that HTTP Accept-Language, JavaScript Intl and web APIs use. For example, JavaScriptCore's languageTagForLocaleID calls it with strict false for every Intl locale and for the default locale.

The fragment adds a debt entry for the remaining gaps:

Overlap:

Tested

Linux only, in Docker ubuntu:24.04 (GCC 13.3.0, CMake 3.28.3, Ninja 1.11.1), Release, -DBUILD_TESTING=ON; based on main dcb5aabff0db8f625ec6007bbcd9db304ae6cca8, tested head 8149e5b774d080375da9329fe8520862fca64f3f. Head be5332b33067bbde65a948d28e3b0f8b7627c06a only adds a clean merge of main 6a86d4d1a9c and moves the debt changes to the fragment; tools/debt_fold.py --check passes on it. Head 61b8caed8aa558de3f737f8528bf5211345e78e1 merges main 1bd3e160c60 (including #18344 and #20148), resolves the #18344 conflict and adds two three-letter-code vectors; the build, the nm -D check and the ctest run below were repeated on it. Head cdcb24749012ce8b395545e94d1a5ded9be1c124 merges main e4caa07950f862269fbe664e3502b15544161e1f (including #20642 and #21242) and resolves the InventoryExports.hpp conflict with #20642, keeping its uloc_setDefault_nid_postfix declaration next to the typed uloc_toLanguageTag one; the build, the nm -D check, the ctest run, tools/debt_fold.py --check and the convention checks were repeated on it.

  • Built libSceIcu.prx and every guest_icu_* test. nm -D on the patched libSceIcu.prx shows zmMWRKwedmw, which matches tools/nid_names.py compute_nid("uloc_toLanguageTag").
  • ctest -R '^guest_icu|^public_headers': 122/122 passed (57 ICU, 65 public headers) on head cdcb247490, including guest_icu_language_tag, which now also covers uloc_toLanguageTag (tests):
    • 49 vectors, each in lenient and strict mode. They include the ICU 68.2 locale_to_langtag cases that need no keyTypeData mapping, with ICU's expected tags and strict failures, plus deprecated regions, duplicate attributes, POSIX with attributes and private use, and BCP 47 input.
    • A round trip of seven tags through uloc_forLanguageTag.
    • Preflight, exact, terminated, short and negative capacities.
    • A valid 244-byte tag, en-x-ab-…-ab with 80 private-use subtags, whose locale form en@x=ab-… is also 244 bytes, so well over the 157-byte buffer the pre-conversion first tries. Given either the tag or the locale form, it comes back unchanged with length 244 in lenient and strict mode. A preflight, an exact buffer and a 156-byte buffer give overflow, not-terminated and overflow with the 156-byte prefix. The case fails on 04c28dd3cb7dac1ee27745211bddd69dbe920b94 (unexpected status) and passes on this head.
    • A failed incoming status that leaves the output alone; the partial en-US of a strict failure; U_INVALID_FORMAT_ERROR for a keyword without a value; the null-argument throws.
  • Mutations: 11 single-line changes. Ten fail the test: dropping the language or region replacement, the POSIX flag, the variant sort, the first-variant skip in -x-lvariant-, the private-use-only branch or the BCP 47 pre-conversion; strict-mode variant failures off; private-use order and attribute order (right side) flipped. The survivor flips the attribute order when attribute is the key being inserted. That case only happens after an earlier multi-letter key, and every such key throws, so it cannot be reached here.
  • python3 core/libs/tests/test_public_headers.py --audit-only (413 libraries, 34,557 exports) passes. python3 tools/check_conventions.py --base origin/main and git diff --check pass.

Windows, macOS and console or title execution were not run.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Follow ICU 68.2 uloc_tag.cpp to write a BCP 47 tag from a locale ID
over this module's canonicalize and component getters: deprecated
language and region replacement, sorted variants, the POSIX variant as
-u-va-posix, attributes, one-letter extensions, private use and the
x-lvariant form, in lenient and strict mode. Keywords that need the
keyTypeData mapping throw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: correct the 157-byte BCP 47 input handling so valid longer tags are not reported as illegal arguments.

What I ran

Reviewed head 04c28dd3cb7dac1ee27745211bddd69dbe920b94. I inspected the prepared description, commit list, manifest and diff, plus the changed implementation, its declarations, tests and debt entry. I did not run commands or tests; reported test results below are from the PR description.

Results

Check Result Note
Source review failed Long valid BCP 47 input is rejected
Linux tests not run 44/44 reported by author
Other platforms and console not run Windows, macOS and console execution unverified

Findings

Blocking

  • core/libs/prx/libSceIcu/LanguageTag.cpp:1064: The BCP 47 pre-conversion uses a fixed 157-byte buffer, then treats Icu::NotTerminated like a conversion error at lines 1065–1067. A valid input tag whose locale representation needs 157 bytes or more therefore returns U_ILLEGAL_ARGUMENT_ERROR, rather than converting the tag. This is reachable for long private-use subtags: the existing parser accepts a sequence of 1–8 character subtags without a total-length limit (LanguageTag.cpp:199–200, 392–415). Use a capacity retry or otherwise handle buffer overflow separately from invalid input.

Non-blocking

None.

Not verified

  • No tests or build commands were run during this review. The PR description reports Linux tests and checks; those results were not independently verified.
  • Windows, macOS, console behavior, and the exact console ICU implementation were not verified.
  • Files read: core/libs/prx/libSceIcu/LanguageTag.cpp, core/libs/prx/libSceIcu/Export.cpp, core/libs/prx/libSceIcu/include/InventoryExports.hpp, core/libs/prx/libSceIcu/Identified.hpp, core/libs/prx/libSceIcu/tests/LanguageTag.cpp, docs/dev/debt/libSceIcu.md, and prepared review materials description.md, commits.md, manifest.json, and diff.patch. No exclusions were listed in the manifest; files.json was unavailable through the reader.

To get this to a good state

Handle overflow from uloc_forLanguageTag by retrying with sufficient capacity, and add a regression case for a valid BCP 47 input whose locale representation exceeds 156 bytes. Re-run the reported Linux build and ICU tests on the updated head.

The BCP 47 pre-conversion read uloc_forLanguageTag into a fixed
157-byte buffer and reported a longer result as an illegal argument,
so a valid tag with a long private-use sequence failed. Retry with the
length the conversion reports, keeping rejected tags as errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MatteoSid

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks, fixed in 8149e5b774d080375da9329fe8520862fca64f3f.

What changed. SourceLocale still tries the 157-byte buffer first. When uloc_forLanguageTag reports U_BUFFER_OVERFLOW_ERROR or U_STRING_NOT_TERMINATED_WARNING, it now retries once into a buffer of the reported length plus the terminator. Only a real failure from the conversion, such as a tag it rejects, still returns U_ILLEGAL_ARGUMENT_ERROR. The debt entry no longer mentions the 157-byte limit.

New test. CheckLongTag in tests/LanguageTag.cpp uses a valid 244-byte tag: en-x- followed by 80 ab private-use subtags. Its locale form en@x=ab-… is also 244 bytes.

  • Given either form, in lenient and in strict mode, the call returns the tag unchanged with length 244.
  • A preflight with a null buffer gives U_BUFFER_OVERFLOW_ERROR.
  • An exact 244-byte buffer gives U_STRING_NOT_TERMINATED_WARNING.
  • A 156-byte buffer gives U_BUFFER_OVERFLOW_ERROR, with the 156-byte prefix written.

It fails on the previous head 04c28dd3 with uloc_toLanguageTag "long": unexpected status, and passes now.

Run. Linux, Docker ubuntu:24.04, GCC 13.3.0, Release, on 8149e5b7:

$ ./tests/guest_icu_language_tag_tests
ICU language tag tests passed
$ ctest -R '^guest_icu|^public_headers'
100% tests passed, 0 tests failed out of 109
$ nm -D core/libs/libs/libSceIcu.prx | grep zmMWRKwedmw
000000000009a660 T zmMWRKwedmw

tools/check_conventions.py --base origin/main and git diff --check pass. The PR description's Tested section now points at this head.

@MatteoSid

Copy link
Copy Markdown
Contributor Author

@fogydes, a note on ordering with #18344. This PR removes the uloc_toLanguageTag stub in core/libs/prx/libSceIcu/Export.cpp and include/InventoryExports.hpp. Your PR removes the uloc_setKeywordValue stub on the lines just above it. We don't share an export, but the hunks are adjacent, so whichever PR merges second will get a conflict.

I'm happy for #18344 to go first. If it merges before this one, I'll rebase this branch on it and resolve the conflict here. If this one merges first, the fix on your side is keeping both removals. Does that work for you?

@MatteoSid

Copy link
Copy Markdown
Contributor Author

Moved the debt changes to docs/dev/debt/changes/feat-icu-uloc-to-language-tag.md per #17217; merged main 6a86d4d1a9c; debt_fold.py --check passes; no code changes. The "Additional callable exports" count stays edited in place in libSceIcu.md: main had moved it to 50 (#19663), so it is now 49, and the sentence keeps both main's u_getIntPropertyMap and this PR's uloc_toLanguageTag. The merge itself had no conflicts.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 14123 core/libs/prx/libSceIcu/Export.cpp
docs/dev/debt/libSceIcu.md
PR 14144 docs/dev/debt/libSceIcu.md
PR 14340 docs/dev/debt/libSceIcu.md
PR 14443 docs/dev/debt/libSceIcu.md
PR 16250 core/libs/prx/libSceIcu/Export.cpp
docs/dev/debt/libSceIcu.md
PR 16971 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
PR 17297 docs/dev/debt/libSceIcu.md
PR 17852 docs/dev/debt/libSceIcu.md
PR 17916 docs/dev/debt/libSceIcu.md
PR 18208 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/libSceIcu.md
PR 18210 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/libSceIcu.md
PR 18400 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
PR 18557 core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/libSceIcu.md
PR 19435 docs/dev/debt/libSceIcu.md
PR 19437 docs/dev/debt/libSceIcu.md
PR 19562 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 19628 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 19635 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 19645 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 19670 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 19720 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 19951 docs/dev/debt/libSceIcu.md
PR 19972 docs/dev/debt/libSceIcu.md
PR 19985 docs/dev/debt/libSceIcu.md
PR 19998 docs/dev/debt/libSceIcu.md
PR 21222 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/changes/libSceIcu-locale-syntax.md
PR 21959 core/libs/prx/libSceIcu/Export.cpp
core/libs/prx/libSceIcu/include/InventoryExports.hpp
docs/dev/debt/libSceIcu.md
PR 22718 core/libs/prx/libSceIcu/Export.cpp

@JJenkx

JJenkx commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Windows check of head be5332b330, merged onto main ee25ae607b (717746df08, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_icu_language_tag passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Implements the typed uloc_toLanguageTag export to convert ICU locale IDs into BCP 47 tags, and updates its debt documentation. Adds tests for conversion behavior and edge cases.

What I ran

No commands were run. The review read 6 of 6 changed files, 824 of 824 added lines, through the repository reader at head be5332b33067bbde65a948d28e3b0f8b7627c06a, in 2 parts.

Results

Check Result Note
Change review passed Every added line was read.
Automated checks not run Author-reported results were not independently run.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

  • Console behavior: BCP 47 output, keyword mapping, and differences inherited from locale getters have not been compared against console behavior.

To get this to a good state

Nothing is required.

…uage-tag

Resolve the adjacent stub removals with boykopovar#18344 by keeping both: the
uloc_setKeywordValue and uloc_toLanguageTag stubs are both gone from
Export.cpp and InventoryExports.hpp, and uloc_toLanguageTag keeps its
typed declaration. The boykopovar#18344 debt fragment no longer lists
uloc_toLanguageTag as throwing. The locale getters now shorten
three-letter codes as ICU 68.2 does, so the uloc_toLanguageTag debt
entry drops that difference and the test pins it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MatteoSid

Copy link
Copy Markdown
Contributor Author

#18344 merged first, so as promised I resolved the conflict here: merged main 1bd3e160c60 (which includes #18344) into this branch, no rebase.

New head 61b8caed8aa. ctest -R '^guest_icu|^public_headers': 114/114 passed (49 ICU, 65 public headers). nm -D still shows zmMWRKwedmw. check_conventions.py and git diff --check pass.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no blocking findings

The implementation and its added test cases were reviewed at head 61b8caed8aa558de3f737f8528bf5211345e78e1; no independent checks were run, so a formal approval is not justified.

What I ran

Read the prepared description, commits, manifest and diff, then inspected the changed implementation, declarations, tests and debt entries through repo_read.py. No build or tests were run in this review.

Results

Check Result Note
Source review passed Changed code and declarations inspected
Build and tests not run Author reports successful Linux checks
Console behavior not run Also listed as unverified in the debt entry

Negative control

Not run; no test environment was available in this review.

Findings

Blocking

None.

Non-blocking

None.

Sources

Reviewed core/libs/prx/libSceIcu/LanguageTag.cpp, core/libs/prx/libSceIcu/Export.cpp, core/libs/prx/libSceIcu/include/InventoryExports.hpp, core/libs/prx/libSceIcu/tests/LanguageTag.cpp, docs/dev/debt/changes/feat-icu-uloc-to-language-tag.md, docs/dev/debt/changes/libSceIcu-locale-syntax.md, and docs/dev/debt/libSceIcu.md at the pinned head. The prepared bundle listed all seven changed files as included; files.json was unavailable through the reader.

Not verified

I did not independently reproduce the reported build, test, mutation, or audit results. Windows, macOS, console execution, and title behavior were not verified.

To get this to a good state

Nothing is required. The reported test results and remaining console verification are optional review context.

@ycbuzi

ycbuzi commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Tested 61b8caed8a merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 2 tests failed out of 5750; 99% tests passed, 3 tests failed out of 5750. 2 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 5750; 100% tests passed, 0 tests failed out of 5750. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 16, 2026
…uage-tag

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MatteoSid

Copy link
Copy Markdown
Contributor Author

Merged main e4caa07950 into the branch; new head cdcb247490.

The conflict came from #20642 (default locale). It typed the uloc_setDefault_nid_postfix declaration in include/InventoryExports.hpp on the line next to uloc_toLanguageTag. I kept its declaration and the typed uloc_toLanguageTag one. Export.cpp and the debt fragments merged cleanly. The new ICU fragments (#20642, #20996, #21177, #21242) do not touch uloc_toLanguageTag. The count line stays at main's 50 minus 1 = 49. tools/debt_fold.py --check passes once main's failing docs-nkwebkit-drop-readded-count-lines.md is set aside; that file is unrelated to this PR.

Rebuilt libSceIcu.prx and all guest_icu_* tests. nm -D shows zmMWRKwedmw. ctest -R '^guest_icu|^public_headers': 122/122 passed (57 ICU, 65 public headers). check_conventions.py and git diff --check pass.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 16, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The reviewed implementation and its added regression coverage support merging at head cdcb24749012ce8b395545e94d1a5ded9be1c124; the author reports passing build, tests, and convention checks, which I did not run.

What I ran

No commands were run. I read the prepared review bundle and the changed implementation, API declaration, tests, and debt documentation at the pinned head. The author reports Linux Docker build and test checks in description.md; these were not independently run. No merged-preview check was run.

Results

Check Result Note
Source review passed Implementation and tests read
Build and test checks not run Reported by author only
Console behavior not run Not verified on hardware

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and test output, convention checks, merged-preview behavior, Windows and macOS behavior, and console behavior were not independently verified. All seven changed files marked included in the manifest were read: Export.cpp, LanguageTag.cpp, InventoryExports.hpp, tests/LanguageTag.cpp, the new feature debt fragment, the locale syntax debt fragment, and libSceIcu.md. No changed file was excluded in the manifest.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 18, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 18, 2026
@oneandonlydean
oneandonlydean merged commit 3e4de1d into boykopovar:main Nov 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants