PHPTAL icon indicating copy to clipboard operation
PHPTAL copied to clipboard

setLanguage throws error with empty strings.

Open adjenks opened this issue 6 years ago • 3 comments

This code:

$TALTranslator = new \PHPTAL_GetTextTranslator();
$TALTranslator->addDomain('mydomain', './locale');
$TALTranslator->useDomain('mydomain');
$TALTranslator->setLanguage('', 'en_US'); // Empty string as first argument

Will throw this exception:

Language(s) code(s) ", en_US" not supported by your system.

Looks like some sort of strange string concatenation problem as indicated by the leading comma in the list of languages in the error.

adjenks avatar May 08 '19 20:05 adjenks

BTW, thank you, to all of the maintainers of this library. I appreciate your work.

adjenks avatar May 08 '19 20:05 adjenks

I'd say that this is the correct behaviour, since '' is not a language. UX for the exception can be improved, maybe, but an empty string should very much lead to a thrown error.

Ocramius avatar May 08 '19 22:05 Ocramius

I think it should just fallback to 'en_US' because if I write: $TALTranslator->setLanguage('potato', 'en_US'); it still works, because it falls back to 'en_US'.

However when setLanguage() calls trySettingLanguages(LC_ALL, ['','en_US']), trySettingLanguages() returns an empty string and setLanguage() casts the empty string to false here:

$langCode = $this->trySettingLanguages(LC_MESSAGES, $langs);
if ($langCode) return $langCode;

trySettingLanguages() returns en empty string because it returns the original string, not the one output by setlocale(), as seen below:

    private function trySettingLanguages($category, array $langs)
    {
        foreach ($langs as $langCode) {
            putenv("LANG=$langCode");
            putenv("LC_ALL=$langCode");
            putenv("LANGUAGE=$langCode");
            if (setlocale($category, $langCode)) {
                return $langCode;
            }
        }
        return null;
    }

On my system, var_export(setlocale(LC_ALL,'')); outputs "en_CA", so an empty string appears to return the currently set locale, as does passing it null as the second argument.

If you read the php docs, it states:

The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns.

So it may vary. Personally I would like PHPTAL to not throw an error, because setlocale does not throw an error.

adjenks avatar May 09 '19 01:05 adjenks