{"record":{"id":"3294ff31c6685819","repo":"PHPOffice/PHPWord","slug":"is-not-a-valid-language-code","errorCode":null,"errorMessage":" is not a valid language code","messagePattern":" is not a valid language code","errorType":"validation","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Style/Language.php","lineNumber":256,"sourceCode":"     *\n     * @param string $locale\n     *\n     * @return string\n     */\n    private function validateLocale($locale)\n    {\n        if ($locale !== null) {\n            $locale = str_replace('_', '-', $locale);\n        }\n\n        if ($locale !== null && strlen($locale) === 2) {\n            return strtolower($locale) . '-' . strtoupper($locale);\n        }\n        if ($locale === 'und') {\n            return 'en-EN';\n        }\n        if ($locale !== null && $locale !== 'zxx' && strstr($locale, '-') === false) {\n            throw new InvalidArgumentException($locale . ' is not a valid language code');\n        }\n\n        return $locale;\n    }\n}\n","sourceCodeStart":238,"sourceCodeEnd":262,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Style/Language.php#L238-L262","documentation":"PhpWord\\Style\\Language::validateLocale checks that a language/locale code passed to setLatin, setEastAsia or setBidirectional is either null, 'zxx' (no language), 'und' (mapped to en-EN), or of the form xx or xx-YY. A non-null string without a hyphen that is not a known special code throws InvalidArgumentException saying it is not a valid language code.","triggerScenarios":"Constructing a Language style with values like Language::setLatin('english') or a bare made-up code ('foo') instead of an ISO 639 code such as 'en' or 'en-US'.","commonSituations":"Passing a human-readable language name ('German', 'english') instead of an ISO code; passing a 3-letter code not in the expected form; internationalization code copied from a different locale framework.","solutions":["Pass an ISO 639-1 two-letter code, optionally with region: 'en', 'de', 'en-US'","Use 'zxx' for 'no language' and 'und' for undetermined instead of other placeholders","Validate input with preg_match('/^[a-z]{2}(-[A-Z]{2})?$/') before setting"],"exampleFix":"// before\n$language = new Language(['latin' => 'english']);\n// after\n$language = new Language(['latin' => 'en-US']);","handlingStrategy":"validation","validationCode":"function isValidLocale(?string $locale): bool {\n    return $locale === null || in_array($locale, ['zxx','und'], true)\n        || preg_match('/^[a-zA-Z]{2}(-[a-zA-Z]{2})?$/', $locale) === 1;\n}\n// use: if (!isValidLocale($lang)) { $lang = 'en-US'; }","typeGuard":"function normalizeLocale(?string $locale): ?string {\n    if ($locale === null || in_array($locale, ['zxx','und'], true)) return $locale;\n    return preg_match('/^[a-zA-Z]{2,3}(-[a-zA-Z0-9]{2,8})?$/', $locale) === 1 ? $locale : 'en-US';\n}","tryCatchPattern":"try {\n    $language->setLatin($locale);\n} catch (\\InvalidArgumentException $e) {\n    $language->setLatin('en-US'); // safe fallback\n}","preventionTips":["Map human-readable language names to ISO codes before use","Always use xx or xx-YY form (e.g. 'en-US'), never names like 'english'","Use 'zxx' / 'und' for the special no-language cases"],"tags":["validation","locale","i18n"],"backgroundTag":"invalid-argument-format","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}