a2lix / TranslationFormBundle

Ease translations with some dedicated Symfony form types
https://a2lix.fr/bundles/translation-form
MIT License
330 stars 140 forks source link

Define config from env file #349

Closed CharlyPoppins closed 2 years ago

CharlyPoppins commented 4 years ago

Environment

PHP version

$ php -v
PHP 7.4.5 (cli) (built: Apr 23 2020 02:25:56) ( NTS )

Subject

Is it possible to get config from .env file ?

Im trying to set a2lix_translation_form.locales from the .env file

Steps to reproduce

# config/packages/a2lix.yaml
a2lix_translation_form:
  ...
  locales: '%env(json:APP_LOCALES)%'
  ...
#.env
APP_LOCALES='["fr", "en"]'

Expected results

🎉

Actual results

Invalid type for path "a2lix_translation_form.locales.0". Expected one of "bool", "int", "float", "string", but got "array".
ray-001 commented 4 years ago

Hi CharlyPoppins,

You can use a comma separated string.

# config/packages/a2lix.yaml
a2lix_translation_form:
  ...
  locales: '%env(APP_LOCALES)%'
  ...
#.env
APP_LOCALES='fr,en'

If you want to use the same env variable in other places in your application you could consider creating a custom EnvVarProcessor

# app\Components\EnvVarProcessor\StringToArrayEnvVarProcessor

namespace App\Components\EnvVarProcessor;

use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;

class StringToArrayEnvVarProcessor implements EnvVarProcessorInterface
{
    public function getEnv($prefix, $name, \Closure $getEnv)
    {
        $env = $getEnv($name);

        return preg_split('/\s*,\s*/', $env);
    }

    public static function getProvidedTypes()
    {
        return [
            'stringToArray' => 'array',
        ];
    }
}
#config/services 

services:
    # default configuration for services in *this* file
    _defaults:
        ....
        bind:
            ...
            $appLocales: '%env(stringToArray:APP_LOCALES)%'
            ...

https://symfony.com/doc/current/configuration/env_var_processors.html

touhami-hiit commented 4 years ago

Hello @ray-001

First thank you very much for your effort on this bundle and your prompt answer.

However, even with your solution or the Symfony's native csv processor, the result does not change. I still encounter the same issue.

Changing the bundles configuration loader and setting locales to be a variableNode solves the issue.

CharlyPoppins commented 3 years ago

@ray-001 thanks for your answer, I'm so sorry I only saw it now. I was giving another try today.

I tried both comma separated string and with the env var processor but i did not manage...

a2lix_translation_form:
  locales: '%env(APP_LOCALES)%'

APP_LOCALES=de,fr,en
Default locale `fr` not found within the configured locales `[de,fr,en]`. Perhaps you need to add it to your `a2lix_translation_form.locales` bundle configuration?

APP_LOCALES='de,fr,en'
Default locale `fr` not found within the configured locales `[de,fr,en]`. Perhaps you need to add it to your `a2lix_translation_form.locales` bundle configuration?

APP_LOCALES="de","fr","en"
Default locale `fr` not found within the configured locales `[de,fr,en]`. Perhaps you need to add it to your `a2lix_translation_form.locales` bundle configuration?

After analyse of debug:config I think it's because conf is converted this way :

# config/packages/a2lix.yaml
a2lix_translation_form:
  locales: '%env(APP_LOCALES)%'

# dump
a2lix_translation_form:
    locales:
        - '%env(APP_LOCALES)%'

I also have the same error with env var processor, seems logic according to my previous statement with debug:config

a2lix_translation_form:
  locales: '%env(stringToArray:APP_LOCALES)%'

Invalid type for path "a2lix_translation_form.locales.0". Expected one of "bool", "int", "float", "string", but got "array".
tchapi commented 2 years ago

Hi all,

This is an issue in Symfony itself (see https://github.com/symfony/symfony/issues/40906 mainly, and https://github.com/symfony/symfony/pull/29270 also).

To date, there is no workaround, except using individual env vars for each language (which is clearly not ideal), or using an hardcoded array in the configuration file.

Closing in favor of following the upstream issue.