Kai
Kai

Reputation: 3823

Symfony - Getting a Supported Languages List

I've been searching around, but I've yet to find a built-in way to get a list of language your particular symfony app supports (i.e., a list of languages that have translation files in the project). Pretty much every code sample I've seen has just hardcoded an array of supported languages, but I'd much prefer a dynamic solution. The only other way I can think of is to actually just look at the names of the messages.language.yml files, but I wanted to verify first that there isn't some built in way of doing this?

Upvotes: 2

Views: 1433

Answers (2)

Tac Tacelosky
Tac Tacelosky

Reputation: 3430

There is now a setting in framework for exactly this:

framework:
    enabled_locales: ['en', 'es']

In twig, you can get these from app variable

{% for supportedLocale in app.enabled_locales %}
    {{ supportedLocale }}
{% endfor %}

Inject it into a service via autowiring.

    public function __construct(
        #[Autowire('%kernel.enabled_locales%')] 
    ) {}

Upvotes: 0

Michael Sivolobov
Michael Sivolobov

Reputation: 13340

There is no built-in way.

The only way to discover all languages for which you have translation files is to look for all folders containing translations and grab the locales from files.

Upvotes: 3

Related Questions