diff --git a/common/config/bootstrap.php b/common/config/bootstrap.php index 64528ff..bce9d52 100644 --- a/common/config/bootstrap.php +++ b/common/config/bootstrap.php @@ -1,5 +1,4 @@ getFilesNames(); - if (in_array($value, $files)) { - return null; + $primary = Locale::getPrimaryLanguage($value); + $region = Locale::getRegion($value); + $locales = ResourceBundle::getLocales(''); // http://php.net/manual/ru/resourcebundle.locales.php#115965 + if (($region !== '' && strtolower($primary) !== strtolower($region)) && !in_array($value, $locales)) { + return [$this->message, []]; } - return [$this->message, []]; - } - - protected function getFilesNames() { - $files = array_values(array_filter(scandir($this->getFolderPath()), function(&$value) { - return $value !== '..' && $value !== '.'; - })); - - return array_map(function($value) { - return basename($value, '.json'); - }, $files); - } - - protected function getFolderPath() { - return Yii::getAlias('@frontend/src/i18n'); + return null; } } diff --git a/console/migrations/m180708_155425_extends_locale_field.php b/console/migrations/m180708_155425_extends_locale_field.php new file mode 100644 index 0000000..600442b --- /dev/null +++ b/console/migrations/m180708_155425_extends_locale_field.php @@ -0,0 +1,15 @@ +alterColumn('{{%accounts}}', 'lang', $this->string()->notNull()->defaultValue('en')); + } + + public function safeDown() { + $this->alterColumn('{{%accounts}}', 'lang', $this->string(5)->notNull()->defaultValue('en')); + } + +} diff --git a/tests/codeception/common/unit/fixtures/.gitkeep b/tests/codeception/common/unit/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/codeception/common/unit/fixtures/data/i18n/en.json b/tests/codeception/common/unit/fixtures/data/i18n/en.json deleted file mode 100644 index cacf5c2..0000000 --- a/tests/codeception/common/unit/fixtures/data/i18n/en.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "testString": "testValue" -} diff --git a/tests/codeception/common/unit/fixtures/data/i18n/ru.json b/tests/codeception/common/unit/fixtures/data/i18n/ru.json deleted file mode 100644 index 9734d39..0000000 --- a/tests/codeception/common/unit/fixtures/data/i18n/ru.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "testString": "тестовоеЗначение" -} diff --git a/tests/codeception/common/unit/validators/LanguageValidatorTest.php b/tests/codeception/common/unit/validators/LanguageValidatorTest.php index e40e72c..e6d077e 100644 --- a/tests/codeception/common/unit/validators/LanguageValidatorTest.php +++ b/tests/codeception/common/unit/validators/LanguageValidatorTest.php @@ -1,39 +1,50 @@ createModelWithFixturePath(); - $this->assertEquals(['en', 'ru'], $this->callProtected($model, 'getFilesNames')); - } - - public function testValidateValueSupportedLanguage() { - $model = $this->createModelWithFixturePath(); - $this->assertNull($this->callProtected($model, 'validateValue', 'ru')); - } - - public function testValidateNotSupportedLanguage() { - $model = $this->createModelWithFixturePath(); - $this->assertEquals([$model->message, []], $this->callProtected($model, 'validateValue', 'by')); - } /** - * @return LanguageValidator + * @param string $locale + * @param bool $shouldBeValid + * + * @dataProvider getTestCases */ - private function createModelWithFixturePath() { - return new class extends LanguageValidator { - public function getFolderPath() { - return __DIR__ . '/../fixtures/data/i18n'; - } - }; + public function testValidate(string $locale, bool $shouldBeValid): void { + $validator = new LanguageValidator(); + $result = $validator->validate($locale, $error); + $this->assertSame($shouldBeValid, $result, $locale); + if (!$shouldBeValid) { + $this->assertSame($validator->message, $error); + } + } + + public function getTestCases(): array { + return [ + // valid + ['de', true], + ['de_DE', true], + ['deu', true], + ['en', true], + ['en_US', true], + ['fil', true], + ['fil_PH', true], + ['zh', true], + ['zh_Hans_CN', true], + ['zh_Hant_HK', true], + // invalid + ['de_FR', false], + ['fr_US', false], + ['foo_bar', false], + ['foo_bar_baz', false], + ]; } }