mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Fix lang() function is overriding locale
This commit is contained in:
parent
5561492918
commit
68a9a353a3
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user