Skip to content

[RFC] Symfony Translator assertValidLocale() #27853

@bogdanfinn

Description

@bogdanfinn

Description
When the translation service is created the default locale is injected in this service during creation. After injecting the service into a controller e.g. you can call the setLocale() Method on the injected service and pass null as an argument for the $locale parameter. The assertValidLocal() function does not throw the InvalidArgumentException, because pre_match() in this case returns 1 when passing null as the second argument. In my opinion this is not correct, because null is not a valid locale. For me the Translator should use then the fallback locale but this is a case that can be discussed because when i set the local manually and it is not correct somebody else might do not want the service to use a different locale. I think if null is passed as the second argument the function should throw the Exception.

Example

     * Asserts that the locale is valid, throws an Exception if not.
     *
     * @param string $locale Locale to tests
     *
     * @throws InvalidArgumentException If the locale contains invalid characters
     */
    protected function assertValidLocale($locale)
    {
        if ($locale === null || 1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
            throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
        }
    }```

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions