From 6021c286a63d3741fa946ba45779a53e11ac42be Mon Sep 17 00:00:00 2001 From: Jeremy Gonyea Date: Mon, 4 Oct 2021 14:25:11 -0400 Subject: [PATCH] Issue #277 Add cli option for language --- classes/Login.php | 8 ++++++++ cli/NewUserCommand.php | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/classes/Login.php b/classes/Login.php index 80cc170..fb9dd44 100755 --- a/classes/Login.php +++ b/classes/Login.php @@ -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; @@ -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; diff --git a/cli/NewUserCommand.php b/cli/NewUserCommand.php index da1b30d..1960f5e 100644 --- a/cli/NewUserCommand.php +++ b/cli/NewUserCommand.php @@ -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', @@ -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'), @@ -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 language abbreviation [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(