{"record":{"id":"6d53a51a07a3dfe4","repo":"nodejs/node","slug":"could-not-canonicalize-the-locale-id-s-error","errorCode":null,"errorMessage":"Could not canonicalize the locale ID: %s. Error: %s","messagePattern":"Could not canonicalize the locale ID: (.+?)\\. Error: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"deps/icu-small/source/tools/genrb/wrtxml.cpp","lineNumber":212,"sourceCode":"    int canonLen = 0;\n    /*int i;*/\n    UErrorCode status = U_ZERO_ERROR;\n    const char *ext = uprv_strchr(id, '.');\n\n    if(ext != nullptr){\n        pos = static_cast<int>(ext - id);\n    } else {\n        pos = idLen;\n    }\n    uprv_memcpy(localeID, id, pos);\n    localeID[pos]=0; /* NUL terminate the string */\n\n    canonCapacity =pos*3;\n    canon = static_cast<char*>(uprv_malloc(canonCapacity));\n    canonLen = uloc_canonicalize(localeID, canon, canonCapacity, &status);\n\n    if(U_FAILURE(status)){\n        fprintf(stderr, \"Could not canonicalize the locale ID: %s. Error: %s\\n\", localeID, u_errorName(status));\n        exit(status);\n    }\n    strnrepchr(canon, canonLen, '_', '-');\n    return canon;\n}\n\nstatic const char* xmlHeader = \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n\";\n#if 0\nstatic const char* bundleStart = \"<xliff version = \\\"1.2\\\" \"\n                                        \"xmlns='urn:oasis:names:tc:xliff:document:1.2' \"\n                                        \"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \"\n                                        \"xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd'>\\n\";\n#else\nstatic const char* bundleStart = \"<xliff version = \\\"1.1\\\" \"\n                                        \"xmlns='urn:oasis:names:tc:xliff:document:1.1' \"\n                                        \"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \"\n                                        \"xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.1 http://www.oasis-open.org/committees/xliff/documents/xliff-core-1.1.xsd'>\\n\";\n#endif","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/icu-small/source/tools/genrb/wrtxml.cpp#L194-L230","documentation":"During XLIFF XML output generation, genrb calls uloc_canonicalize() to normalize the locale ID extracted from the resource bundle filename. If canonicalization fails (returns a UErrorCode failure), the locale ID is malformed or unsupported, and the tool exits the process. Canonicalization normalizes locale ID variants, keyword ordering, and alias resolution.","triggerScenarios":"The locale ID derived from the resource bundle filename or the -l option fails uloc_canonicalize(). This can happen with completely malformed locale strings, locale IDs with invalid keyword syntax, or locale IDs containing characters that the canonicalization parser cannot handle. The tool calls exit(status) — this is fatal.","commonSituations":"Filename contains a non-standard locale code (e.g. 'zz_test.txt' where 'zz' is not a valid language); the -l option is set to a malformed locale string; locale ID with invalid extension keyword syntax (e.g. '@invalid' instead of '@key=value'); locale aliases that resolve to nothing in this ICU version.","solutions":["Verify the locale ID follows BCP 47 / ISO 639 format: language_Script_REGION_VARIANT","Check the filename or -l option value for typos or non-standard codes: `locale -a | grep <prefix>`","Remove invalid extension keywords from the locale ID","Test the locale ID with ICU's uloc_getName() in a small C program to isolate the canonicalization failure","Use a known-valid locale ID from the IANA language subtag registry"],"exampleFix":"// before: invalid locale ID\nfile: test_XX_invalid.txt\n// genrb -l 'test@bad' test.txt\n// after: valid locale ID\nfile: en_US.txt\n// genrb -l 'en_US' en_US.txt","handlingStrategy":"validation","validationCode":"# Validate locale IDs before running genrb with --write-xliff\nLOCALE=\"en_US\"\n# Check against ICU's known locale list (if icuinfo/uconv is available)\nuconv -l 2>/dev/null | grep -q \"^${LOCALE%%_*}$\" || echo \"WARNING: language code '${LOCALE%%_*}' may not be valid\"\n# Basic format check: language[_Script][_REGION][_VARIANT]\necho \"$LOCALE\" | grep -qP '^[a-z]{2,3}(_[A-Z][a-z]{3})?(_[A-Z]{2})?(_[A-Za-z0-9]{4,})?$' || echo \"ERROR: malformed locale ID: $LOCALE\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use standard BCP 47 / ISO 639 locale IDs in resource bundle filenames","Validate locale IDs before passing them to genrb's -l option","Avoid non-standard locale codes or invalid extension keyword syntax","When in doubt, test the locale ID with ICU's uloc_getName() API"],"tags":["icu","genrb","locale","xliff","canonicalization"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}