Skip to content

Commit d34538c

Browse files
committed
Add Translator::setLocale() method
The Translator::setLocale() takes precedence over $GLOBALS['lang']. This makes possible to deprecate the usage of the $GLOBALS['lang']. Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent 0923ebf commit d34538c

File tree

4 files changed

+135
-10
lines changed

4 files changed

+135
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ LC_ALL=pl ./vendor/bin/lint-query --query "SELECT 1"
124124
```php
125125
require __DIR__ . '/vendor/autoload.php';
126126

127-
$GLOBALS['lang'] = 'pl';
127+
PhpMyAdmin\SqlParser\Translator::setLocale('pl');
128128

129129
$query1 = 'select * from a';
130130
$parser = new PhpMyAdmin\SqlParser\Parser($query1);

src/Translator.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace PhpMyAdmin\SqlParser;
66

77
use PhpMyAdmin\MoTranslator\Loader;
8+
use PhpMyAdmin\MoTranslator\Translator as MoTranslator;
89

10+
use function assert;
911
use function class_exists;
1012

1113
/**
@@ -16,32 +18,40 @@ class Translator
1618
/**
1719
* The MoTranslator loader object.
1820
*
19-
* @var Loader
21+
* @var Loader|null
2022
*/
2123
private static $loader;
2224

2325
/**
2426
* The MoTranslator translator object.
2527
*
26-
* @var \PhpMyAdmin\MoTranslator\Translator
28+
* @var MoTranslator|null
2729
*/
2830
private static $translator;
2931

32+
/** @var string */
33+
private static $locale = '';
34+
3035
/**
3136
* Loads translator.
3237
*
3338
* @return void
3439
*/
3540
public static function load()
3641
{
42+
if (! class_exists(Loader::class)) {
43+
return;
44+
}
45+
3746
if (self::$loader === null) {
3847
// Create loader object
3948
self::$loader = new Loader();
4049

41-
// Set locale
42-
self::$loader->setlocale(
43-
self::$loader->detectlocale()
44-
);
50+
if (self::$locale === '') {
51+
self::$locale = self::$loader->detectlocale();
52+
}
53+
54+
self::$loader->setlocale(self::$locale);
4555

4656
// Set default text domain
4757
self::$loader->textdomain('sqlparser');
@@ -67,12 +77,23 @@ public static function load()
6777
*/
6878
public static function gettext($msgid)
6979
{
70-
if (! class_exists('\PhpMyAdmin\MoTranslator\Loader', true)) {
80+
if (! class_exists(Loader::class)) {
7181
return $msgid;
7282
}
7383

7484
self::load();
85+
assert(self::$translator instanceof MoTranslator);
7586

7687
return self::$translator->gettext($msgid);
7788
}
89+
90+
public static function setLocale(string $locale): void
91+
{
92+
self::$locale = $locale;
93+
}
94+
95+
public static function getLocale(): string
96+
{
97+
return self::$locale;
98+
}
7899
}

tests/Misc/TranslatorTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Tests\Misc;
6+
7+
use PhpMyAdmin\MoTranslator\Loader;
8+
use PhpMyAdmin\MoTranslator\Translator as MoTranslator;
9+
use PhpMyAdmin\SqlParser\Translator;
10+
use PHPUnit\Framework\TestCase;
11+
use ReflectionClass;
12+
use ReflectionProperty;
13+
14+
use function realpath;
15+
16+
/** @covers \PhpMyAdmin\SqlParser\Translator */
17+
final class TranslatorTest extends TestCase
18+
{
19+
public static function tearDownAfterClass(): void
20+
{
21+
$loaderProperty = new ReflectionProperty(Translator::class, 'loader');
22+
$loaderProperty->setAccessible(true);
23+
$loaderProperty->setValue(null, null);
24+
$translatorProperty = new ReflectionProperty(Translator::class, 'translator');
25+
$translatorProperty->setAccessible(true);
26+
$translatorProperty->setValue(null, null);
27+
Translator::setLocale('en');
28+
}
29+
30+
public function testLocale(): void
31+
{
32+
Translator::setLocale('en');
33+
self::assertSame('en', Translator::getLocale());
34+
Translator::setLocale('fr');
35+
self::assertSame('fr', Translator::getLocale());
36+
Translator::setLocale('');
37+
self::assertSame('', Translator::getLocale());
38+
}
39+
40+
/**
41+
* @testWith [null, "en", "en"]
42+
* [null, "fr", "fr"]
43+
* ["en", "", "en"]
44+
* ["fr", "", "fr"]
45+
*/
46+
public function testLoad(?string $globalLang, string $locale, string $expectedLocale): void
47+
{
48+
$loaderProperty = new ReflectionProperty(Translator::class, 'loader');
49+
$loaderProperty->setAccessible(true);
50+
$loaderProperty->setValue(null, null);
51+
$translatorProperty = new ReflectionProperty(Translator::class, 'translator');
52+
$translatorProperty->setAccessible(true);
53+
$translatorProperty->setValue(null, null);
54+
$GLOBALS['lang'] = $globalLang;
55+
Translator::setLocale($locale);
56+
57+
Translator::load();
58+
59+
self::assertSame($expectedLocale, Translator::getLocale());
60+
self::assertInstanceOf(MoTranslator::class, $translatorProperty->getValue());
61+
$loader = $loaderProperty->getValue();
62+
self::assertInstanceOf(Loader::class, $loader);
63+
$loaderClass = new ReflectionClass(Loader::class);
64+
$localeProperty = $loaderClass->getProperty('locale');
65+
$localeProperty->setAccessible(true);
66+
self::assertSame($expectedLocale, $localeProperty->getValue($loader));
67+
$defaultDomainProperty = $loaderClass->getProperty('defaultDomain');
68+
$defaultDomainProperty->setAccessible(true);
69+
self::assertSame('sqlparser', $defaultDomainProperty->getValue($loader));
70+
$pathsProperty = $loaderClass->getProperty('paths');
71+
$pathsProperty->setAccessible(true);
72+
self::assertSame(
73+
['' => './', 'sqlparser' => realpath(__DIR__ . '/../../src/') . '/../locale/'],
74+
$pathsProperty->getValue($loader)
75+
);
76+
}
77+
78+
public function testGettext(): void
79+
{
80+
$loaderProperty = new ReflectionProperty(Translator::class, 'loader');
81+
$loaderProperty->setAccessible(true);
82+
$loaderProperty->setValue(null, null);
83+
$translatorProperty = new ReflectionProperty(Translator::class, 'translator');
84+
$translatorProperty->setAccessible(true);
85+
$translatorProperty->setValue(null, null);
86+
Translator::setLocale('pt_BR');
87+
self::assertSame(
88+
'Início de declaração inesperado.',
89+
Translator::gettext('Unexpected beginning of statement.')
90+
);
91+
92+
$loaderProperty = new ReflectionProperty(Translator::class, 'loader');
93+
$loaderProperty->setAccessible(true);
94+
$loaderProperty->setValue(null, null);
95+
$translatorProperty = new ReflectionProperty(Translator::class, 'translator');
96+
$translatorProperty->setAccessible(true);
97+
$translatorProperty->setValue(null, null);
98+
Translator::setLocale('en');
99+
self::assertSame(
100+
'Unexpected beginning of statement.',
101+
Translator::gettext('Unexpected beginning of statement.')
102+
);
103+
}
104+
}

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\SqlParser\Token;
1313
use PhpMyAdmin\SqlParser\TokensList;
1414
use PhpMyAdmin\SqlParser\Tools\CustomJsonSerializer;
15+
use PhpMyAdmin\SqlParser\Translator;
1516
use PHPUnit\Framework\TestCase as BaseTestCase;
1617

1718
use function file_get_contents;
@@ -26,13 +27,12 @@ abstract class TestCase extends BaseTestCase
2627
{
2728
public function setUp(): void
2829
{
29-
global $lang;
3030
// This line makes sure the test suite uses English so we can assert
3131
// on the error messages, if it is not here you will need to use
3232
// LC_ALL=C ./vendor/bin/phpunit
3333
// Users can have French language as default on their OS
3434
// That would make the assertions fail
35-
$lang = 'en';
35+
Translator::setLocale('en');
3636
Context::load();
3737
}
3838

0 commit comments

Comments
 (0)