Fix lang() function is overriding locale

This commit is contained in:
Paulo Esteves 2021-08-15 00:27:10 +01:00 committed by John Paul E. Balandan, CPA
parent 5561492918
commit 68a9a353a3
2 changed files with 38 additions and 2 deletions

View File

@ -717,8 +717,25 @@ if (! function_exists('lang')) {
*/
function lang(string $line, array $args = [], ?string $locale = null)
{
return Services::language($locale)
->getLine($line, $args);
$language = Services::language();
//Get active locale
$activeLocale = $language->getLocale();
if ($locale && $locale != $activeLocale)
{
$language->setLocale($locale);
}
$line = $language->getLine($line, $args);
if ($locale && $locale != $activeLocale)
{
//Reset to active locale
$language->setLocale($activeLocale);
}
return $line;
}
}

View File

@ -271,6 +271,25 @@ final class LanguageTest extends CIUnitTestCase
$this->assertSame('More.shootMe', $this->lang->getLine('More.shootMe'));
}
/**
* Test if after using lang() with a locale the Language class keep the locale after return the $line
*/
public function testLangKeepLocale()
{
$this->lang = Services::language('en', true);
lang('Language.languageGetLineInvalidArgumentException');
$this->assertEquals('en', $this->lang->getLocale());
lang('Language.languageGetLineInvalidArgumentException', [], 'ru');
$this->assertEquals('en', $this->lang->getLocale());
lang('Language.languageGetLineInvalidArgumentException');
$this->assertEquals('en', $this->lang->getLocale());
}
//--------------------------------------------------------------------
/**
* Testing base locale vs variants, with fallback to English.
*