Skip to content

Commit

Permalink
Issue getgrav#277 Add cli option for language
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonyea committed Oct 4, 2021
1 parent fe0b25b commit 6021c28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions classes/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Grav\Common\Debugger;
use Grav\Common\Grav;
use Grav\Common\Language\Language;
use Grav\Common\Language\LanguageCodes;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
Expand Down Expand Up @@ -418,6 +419,13 @@ public function validateField($type, $value, $extra = '')

break;

case 'language':
$languages = new LanguageCodes();
if ($value !== null && !array_key_exists($value, $languages->getList())) {
throw new \RuntimeException('Language code is not valid');
}

break;
}

return $value;
Expand Down
22 changes: 22 additions & 0 deletions cli/NewUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'The user email'
)
->addOption(
'language',
'l',
InputOption::VALUE_OPTIONAL,
'The default language of the account. [default: "en"]'
)
->addOption(
'permissions',
'P',
Expand Down Expand Up @@ -101,6 +107,7 @@ protected function serve()
'user' => $this->input->getOption('user'),
'password1' => $this->input->getOption('password'),
'email' => $this->input->getOption('email'),
'language' => $this->input->getOption('language'),
'permissions' => $this->input->getOption('permissions'),
'fullname' => $this->input->getOption('fullname'),
'title' => $this->input->getOption('title'),
Expand Down Expand Up @@ -170,6 +177,21 @@ protected function serve()
$user->set('email', $this->options['email']);
}

if (!$this->options['language']) {
// Get language and validate.
$question = new Question('Enter a <yellow>language abbreviation</yellow> [en]: ');
$question->setValidator(function ($value) {
return $this->validate('language', $value);
});

$user->set('language', $helper->ask($this->input, $this->output, $question));
if ($user->get('language') == null) {
$user->set('language', 'en');
}
} else {
$user->set('language', $this->options['language']);
}

if (!$this->options['permissions']) {
// Choose permissions
$question = new ChoiceQuestion(
Expand Down

0 comments on commit 6021c28

Please sign in to comment.