languagetool-org/languagetool · error · RuntimeException

Spanish language expected, got " + language

Error message

Spanish language expected, got " + language

What it means

Spanish.getInstance() resolves the language for the Spanish short code and returns it only if it is actually an instance of Spanish. If Languages.getLanguageForShortCode returns a different Language implementation for that code (variant or override registered elsewhere), the invariant is broken and a RuntimeException is thrown naming the returned language.

Source

Thrown at languagetool-language-modules/es/src/main/java/org/languagetool/language/Spanish.java:406

          ruleMatch.setShortMessage("Mayúsculas y minúsculas");
          ruleMatch.getRule().setLocQualityIssueType(ITSIssueType.Typographical);
          ruleMatch.getRule().setCategory(Categories.CASING.getCategory(ResourceBundleTools.getMessageBundle(this)));
          ruleMatch.setSpecificRuleId(ruleMatch.getRule().getId().replace("ORTHOGRAPHY", "CASING"));
          //ruleMatch.getRule().setTags(Arrays.asList(Tag.picky));
        }
      }
      results.add(ruleMatch);
    }

    return results;
  }

  public static @NotNull Spanish getInstance() {
    Language language = Objects.requireNonNull(Languages.getLanguageForShortCode(LANGUAGE_SHORT_CODE));
    if (language instanceof Spanish spanish) {
      return spanish;
    }
    throw new RuntimeException("Spanish language expected, got " + language);
  }
}

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Check which language is registered for "es" via Languages.getLanguageForShortCode("es") and remove conflicting registrations.
  2. Avoid registering custom languages under the built-in "es" short code.
  3. Use the returned Language object directly instead of downcasting when a variant is intentional.

Example fix

// before
Spanish es = Spanish.getInstance(); // throws if "es" overridden
// after
Language lang = Languages.getLanguageForShortCode("es");
Spanish es = lang instanceof Spanish s ? s : null;
Defensive patterns

Strategy: type-guard

Validate before calling

Language l = Languages.getLanguageForShortCode("es");
if (!(l instanceof Spanish)) { /* handle variant/override before using getInstance */ }

Type guard

if (language instanceof Spanish spanish) { return spanish; } else { /* not the built-in Spanish class */ }

Try / catch

try {
  Spanish es = Spanish.getInstance();
} catch (RuntimeException e) {
  if (e.getMessage().startsWith("Spanish language expected")) { /* fall back to the Language interface */ }
}

Prevention

When it happens

Trigger: Calling Spanish.getInstance() when the registered language for "es" resolves to a non-Spanish Language object — e.g. another variant of Spanish registered under the same short code, or a custom language registered as "es".

Common situations: Environments where a custom/contrib language overrides the built-in "es" code; test setups registering mock languages; running with a language variant list where Spanish is represented by a subclass-free different class.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/70bcc083eef58e3e. Report an issue: GitHub.