twigphp/Twig · error · Twig\Error\RuntimeError

SpanishInflector is not available.

Error message

SpanishInflector is not available.

What it means

StringExtension's plural()/singular() inflects strings using a locale-specific inflector. For Spanish it relies on Symfony's SpanishInflector, which only exists when symfony/string is installed with adequate PHP/polyfill support. The extension guards with class_exists() and throws this RuntimeError when the class is missing, so the user knows to install the required package instead of getting a fatal class-not-found.

Solutions

  1. Run composer require symfony/string to install/upgrade the package providing SpanishInflector.
  2. Run composer update symfony/string to move to a version that includes SpanishInflector.
  3. Use locale 'en' or 'fr' instead of 'es' if Spanish inflection is not required.
  4. Wrap the plural()/singular() call in try/catch (RuntimeError) and fall back to the uninflected word.

Example fix

// before (composer.json missing the dependency)
$ composer show symfony/string # not installed
// after
$ composer require symfony/string
Defensive patterns

Strategy: validation

When it happens

Trigger: Calling StringExtension's plural($word, 'es') or singular($word, 'es') when class_exists(SpanishInflector::class) is false — i.e. symfony/string (or its polyfill providing SpanishInflector) is not installed.

Common situations: Projects that installed twig/string-extra without symfony/string satisfying the version constraint; deployments where composer dependencies were pruned or --no-dev removed transitive packages; older symfony/string versions that lacked SpanishInflector (it requires a sufficiently recent symfony/string / symfony/polyfill-php85-backed build).

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/5386069d960fa276. Report an issue: GitHub.

Appendix: source

Thrown at extra/string-extra/StringExtension.php:89

     * @return array|string
     */
    public function singular(string $value, string $locale = 'en', bool $all = false)
    {
        if ($all) {
            return $this->getInflector($locale)->singularize($value);
        }

        return $this->getInflector($locale)->singularize($value)[0];
    }

    private function getInflector(string $locale): InflectorInterface
    {
        switch ($locale) {
            case 'en':
                return $this->englishInflector ?? $this->englishInflector = new EnglishInflector();
            case 'es':
                if (!class_exists(SpanishInflector::class)) {
                    throw new RuntimeError('SpanishInflector is not available.');
                }

                return $this->spanishInflector ?? $this->spanishInflector = new SpanishInflector();
            case 'fr':
                return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
            default:
                throw new \InvalidArgumentException(\sprintf('Locale "%s" is not supported.', $locale));
        }
    }
}

View on GitHub (pinned to a414c3a491)