{"record":{"id":"3535cf2b74af79b5","repo":"languagetool-org/languagetool","slug":"invalid-preferredvariants-no-such-language-vari","errorCode":null,"errorMessage":"Invalid 'preferredVariants', no such language/variant found: '{preferredVariant}'","messagePattern":"Invalid 'preferredVariants', no such language/variant found: '(.+?)'","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"languagetool-server/src/main/java/org/languagetool/server/TextChecker.java","lineNumber":1023,"sourceCode":"    } else {\n      lang = detected.getDetectedLanguage();\n    }\n    //String mode;\n    //long t1 = System.nanoTime();\n    //long t2 = System.nanoTime();\n    //float runTime = (t2-t1)/1000.0f/1000.0f;\n    //System.out.printf(Locale.ENGLISH, \"detected \" + detected + \" using \" + mode + \" in %.2fms for %d chars\\n\", runTime, text.length());\n    \n    if (preferredVariants.size() > 0) {\n      for (String preferredVariant : preferredVariants) {\n        if (!preferredVariant.contains(\"-\")) {\n          throw new BadRequestException(\"Invalid format for 'preferredVariants', expected a dash as in 'en-GB': '\" + preferredVariant + \"'\");\n        }\n        String preferredVariantLang = preferredVariant.split(\"-\")[0];\n        if (preferredVariantLang.equals(lang.getShortCode())) {\n          lang = parseLanguage(preferredVariant);\n          if (lang == null) {\n            throw new BadRequestException(\"Invalid 'preferredVariants', no such language/variant found: '\" + preferredVariant + \"'\");\n          }\n        }\n      }\n    } else {\n      if (lang.getDefaultLanguageVariant() != null) {\n        lang = lang.getDefaultLanguageVariant();\n      }\n    }\n    return new DetectedLanguage(null, lang, detected != null ? detected.getDetectionConfidence() : 0f,\n      detected != null ? detected.getDetectionSource() : null);\n  }\n\n  static class QueryParams {\n    final List<Language> altLanguages;\n    final List<String> enabledRules;\n    final List<String> disabledRules;\n    final List<CategoryId> enabledCategories;\n    final List<CategoryId> disabledCategories;","sourceCodeStart":1005,"sourceCodeEnd":1041,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-server/src/main/java/org/languagetool/server/TextChecker.java#L1005-L1041","documentation":"The preferredVariants entry has a valid 'xx-YY' shape but LanguageTool has no registered language/variant matching it. parseLanguage() returns null for unknown combinations, so the server throws this BadRequestException. The language part must match the request's short code for this branch to apply.","triggerScenarios":"Calling /v2/check with e.g. preferredVariants=en-XX, xx-YY or zz-FAKE, or a variant that exists for another language than the current lang short code path resolves; typos like 'en-GBB'.","commonSituations":"Misspelled variant codes; inventing variants that don't exist (en-UK — the correct code is en-GB); stale hardcoded lists after LanguageTool upgrades; user-supplied locale strings passed through unvalidated.","solutions":["Verify the exact variant exists, e.g. use en-GB not en-UK","List available variants via the /v2/languages endpoint and pick from it","Validate user input against the supported language list before sending","Check the variant's language prefix matches your 'language' parameter"],"exampleFix":"// before\nparams.put(\"preferredVariants\", \"en-UK\");\n// after\nparams.put(\"preferredVariants\", \"en-GB\");","handlingStrategy":"validation","validationCode":"const supported = await fetch(`${LT_URL}/v2/languages`).then(r => r.json());\nconst valid = supported.some(l => `${l.code}-${l.code.toUpperCase()}` === variant || l.name === variant);","typeGuard":"const isKnownVariant = (v, list) => list.includes(v);","tryCatchPattern":"try { const res = await check(params); } catch (e) { if (e.status === 400 && /no such language\\/variant/.test(e.message)) { /* fall back to default variant */ } }","preventionTips":["Use en-GB not en-UK","Derive choices from /v2/languages rather than hardcoding","Pin the server version when hardcoding variant lists"],"tags":["http","api","validation","languagetool"],"backgroundTag":"invalid-enum-value","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}