mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Allow any valid locale for account lang
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
Yii::setAlias('common', dirname(__DIR__));
|
||||
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');
|
||||
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
|
||||
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
|
||||
Yii::setAlias('api', dirname(__DIR__, 2) . '/api');
|
||||
Yii::setAlias('console', dirname(__DIR__, 2) . '/console');
|
||||
|
@@ -1,39 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\validators;
|
||||
|
||||
use common\helpers\Error as E;
|
||||
use Yii;
|
||||
use Locale;
|
||||
use ResourceBundle;
|
||||
use yii\validators\Validator;
|
||||
|
||||
class LanguageValidator extends Validator {
|
||||
|
||||
public $message = E::UNSUPPORTED_LANGUAGE;
|
||||
|
||||
protected function validateValue($value) {
|
||||
/**
|
||||
* The idea of this validator belongs to
|
||||
* https://github.com/lunetics/LocaleBundle/blob/1f5ee7f1802/Validator/LocaleValidator.php#L82-L88
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return array|null
|
||||
*/
|
||||
protected function validateValue($value): ?array {
|
||||
if (empty($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$files = $this->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user