Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setLanguage throws error with empty strings. #70

Open
adjenks opened this issue May 8, 2019 · 3 comments
Open

setLanguage throws error with empty strings. #70

adjenks opened this issue May 8, 2019 · 3 comments

Comments

@adjenks
Copy link

adjenks commented May 8, 2019

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
Copy link
Author

adjenks commented May 8, 2019

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

@Ocramius
Copy link
Member

Ocramius commented May 8, 2019

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.

@adjenks
Copy link
Author

adjenks commented May 9, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants