Skip to content

Commit 17167da

Browse files
committedNov 19, 2024
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 <mauricio@mfauth.net>
1 parent 0923ebf commit 17167da

File tree

4 files changed

+141
-10
lines changed

4 files changed

+141
-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: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace PhpMyAdmin\SqlParser;
66

77
use PhpMyAdmin\MoTranslator\Loader;
8+
use PhpMyAdmin\MoTranslator\Translator as MoTranslator;
9+
use RuntimeException;
810

11+
use function assert;
912
use function class_exists;
1013

1114
/**
@@ -16,32 +19,42 @@ class Translator
1619
/**
1720
* The MoTranslator loader object.
1821
*
19-
* @var Loader
22+
* @var Loader|null
2023
*/
2124
private static $loader;
2225

2326
/**
2427
* The MoTranslator translator object.
2528
*
26-
* @var \PhpMyAdmin\MoTranslator\Translator
29+
* @var MoTranslator|null
2730
*/
2831
private static $translator;
2932

33+
/** @var string */
34+
private static $locale = '';
35+
3036
/**
3137
* Loads translator.
3238
*
39+
* @internal This method is not covered by the backward compatibility promise for SQL-Parser
40+
*
3341
* @return void
3442
*/
3543
public static function load()
3644
{
45+
if (! class_exists(Loader::class)) {
46+
throw new RuntimeException('The phpmyadmin/motranslator package is missing.');
47+
}
48+
3749
if (self::$loader === null) {
3850
// Create loader object
3951
self::$loader = new Loader();
4052

41-
// Set locale
42-
self::$loader->setlocale(
43-
self::$loader->detectlocale()
44-
);
53+
if (self::$locale === '') {
54+
self::$locale = self::$loader->detectlocale();
55+
}
56+
57+
self::$loader->setlocale(self::$locale);
4558

4659
// Set default text domain
4760
self::$loader->textdomain('sqlparser');
@@ -67,12 +80,23 @@ public static function load()
6780
*/
6881
public static function gettext($msgid)
6982
{
70-
if (! class_exists('\PhpMyAdmin\MoTranslator\Loader', true)) {
83+
if (! class_exists(Loader::class)) {
7184
return $msgid;
7285
}
7386

7487
self::load();
88+
assert(self::$translator instanceof MoTranslator);
7589

7690
return self::$translator->gettext($msgid);
7791
}
92+
93+
public static function setLocale(string $locale): void
94+
{
95+
self::$locale = $locale;
96+
}
97+
98+
public static function getLocale(): string
99+
{
100+
return self::$locale;
101+
}
78102
}

‎tests/Misc/TranslatorTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
// Compatibility with MoTranslator < 5
68+
$defaultDomainProperty = $loaderClass->hasProperty('default_domain')
69+
? $loaderClass->getProperty('default_domain')
70+
: $loaderClass->getProperty('defaultDomain');
71+
$defaultDomainProperty->setAccessible(true);
72+
self::assertSame('sqlparser', $defaultDomainProperty->getValue($loader));
73+
$pathsProperty = $loaderClass->getProperty('paths');
74+
$pathsProperty->setAccessible(true);
75+
self::assertSame(
76+
['' => './', 'sqlparser' => realpath(__DIR__ . '/../../src/') . '/../locale/'],
77+
$pathsProperty->getValue($loader)
78+
);
79+
}
80+
81+
public function testGettext(): void
82+
{
83+
$loaderProperty = new ReflectionProperty(Translator::class, 'loader');
84+
$loaderProperty->setAccessible(true);
85+
$loaderProperty->setValue(null, null);
86+
$translatorProperty = new ReflectionProperty(Translator::class, 'translator');
87+
$translatorProperty->setAccessible(true);
88+
$translatorProperty->setValue(null, null);
89+
Translator::setLocale('pt_BR');
90+
self::assertSame(
91+
'Início de declaração inesperado.',
92+
Translator::gettext('Unexpected beginning of statement.')
93+
);
94+
95+
$loaderProperty = new ReflectionProperty(Translator::class, 'loader');
96+
$loaderProperty->setAccessible(true);
97+
$loaderProperty->setValue(null, null);
98+
$translatorProperty = new ReflectionProperty(Translator::class, 'translator');
99+
$translatorProperty->setAccessible(true);
100+
$translatorProperty->setValue(null, null);
101+
Translator::setLocale('en');
102+
self::assertSame(
103+
'Unexpected beginning of statement.',
104+
Translator::gettext('Unexpected beginning of statement.')
105+
);
106+
}
107+
}

‎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)