Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Translation] Symfony Contracts' TranslatorTrait Does not Handle the en_US_POSIX Locale Pluralization Correctly #42036

Closed
@chrisguitarguy

Description

@chrisguitarguy

Symfony version(s) affected: 4.4, 5.X

Description
When the default locale ends up as en_US_POSIX (which happens on Alpine), it seems that plurarlization stuff does not get correctly handled.

Seems to be traced to this line:

switch ('pt_BR' !== $locale && \strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) {

Specifically: substr($locale, 0, strrpos($locale, '_')) which is going to yield en_US instead of just en when the local is en_US_POSIX

How to reproduce

<?php
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Translation\IdentityTranslator;

foreach (['en_US', 'en_US_POSIX'] as $locale) {
    Locale::setDefault($locale);
    $translator = new IdentityTranslator();
    // or can fake it with $translator->setLocale($locale);

    echo $locale, PHP_EOL;
    echo '----------', PHP_EOL;
    foreach (range(1, 2) as $i) {
        echo $translator->trans('singular|plural', ['%count%' => $i]), PHP_EOL;
    }
}

Possible Solution
Split on underscores instead of using strrpos on this line

Additional context

I'm not even sure this is a bug or if it matters, but I hit this when a locale environment (OSX) had a default locale of en_US but prod (alpine linux) had a locale of en_US_POSIX. A custom constraint with a pluralized message was correctly rendering on my locale but not on production/staging.

Of note: the translator component is disabled on my app, but I would bet that the real translator does not have these issues? Possible that this only impacts IdentityTranslator and places that use the TranslatorTrait from contracts. Symfony 4.4 might be impacted more broadly.

Seems like en_US_POSIX is an alias for LC_ALL=C.

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