Skip to content

Commit c9355a8

Browse files
authored
Merge pull request #75 from ADmad/feat/no-langs-error
Show error if no languages list is available
2 parents f861e91 + ed31080 commit c9355a8

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/Command/I18nExtractCommand.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class I18nExtractCommand extends CakeI18nExtractCommand
2828
/**
2929
* App languages.
3030
*
31-
* @var array
31+
* @var list<string>
3232
*/
3333
protected array $_languages = [];
3434

@@ -62,6 +62,15 @@ public static function defaultName(): string
6262
*/
6363
public function execute(Arguments $args, ConsoleIo $io): ?int
6464
{
65+
$this->_languages = $this->_getLanguages($args);
66+
if ($this->_languages === []) {
67+
$io->err(
68+
'You must specify the languages list using the `I18n.languages` config or the `--languages` option.'
69+
);
70+
71+
return static::CODE_ERROR;
72+
}
73+
6574
$plugin = '';
6675
if ($args->hasOption('exclude')) {
6776
$this->_exclude = explode(',', (string)$args->getOption('exclude'));
@@ -148,7 +157,6 @@ protected function _extract(Arguments $args, ConsoleIo $io): void
148157
$io->hr();
149158
$this->_extractTokens($args, $io);
150159

151-
$this->_getLanguages($args);
152160
$this->_saveMessages($args, $io);
153161

154162
$this->_paths = $this->_files = $this->_storage = [];
@@ -166,30 +174,31 @@ protected function _extract(Arguments $args, ConsoleIo $io): void
166174
* Get app languages.
167175
*
168176
* @param \Cake\Console\Arguments $args The Arguments instance
169-
* @return void
177+
* @return list<string>
170178
*/
171-
protected function _getLanguages(Arguments $args): void
179+
protected function _getLanguages(Arguments $args): array
172180
{
173181
$langs = (string)$args->getOption('languages');
174182
if ($langs) {
175-
$this->_languages = explode(',', $langs);
176-
177-
return;
183+
return explode(',', $langs);
178184
}
179185

180-
$langs = Configure::read('I18n.languages');
181-
if (empty($langs)) {
182-
return;
186+
$langs = Configure::read('I18n.languages', []);
187+
if ($langs === []) {
188+
return [];
183189
}
184190

191+
$return = [];
185192
$langs = Hash::normalize($langs);
186193
foreach ($langs as $key => $value) {
187194
if (isset($value['locale'])) {
188-
$this->_languages[] = $value['locale'];
195+
$return[] = $value['locale'];
189196
} else {
190-
$this->_languages[] = $key;
197+
$return[] = $key;
191198
}
192199
}
200+
201+
return $return;
193202
}
194203

195204
/**

tests/TestCase/Command/I18nExtractCommandTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ public function testExecute()
8585
$this->assertEquals('You have %d new messages (domain).', $result['plural']);
8686
}
8787

88+
public function testExecuteError()
89+
{
90+
Configure::delete('I18n.languages');
91+
92+
$this->exec('i18n extract');
93+
$this->assertExitError();
94+
$this->assertErrorContains('You must specify the languages list using the `I18n.languages` config or the `--languages` option.');
95+
}
96+
8897
/**
8998
* testExecute with merging on method.
9099
*

0 commit comments

Comments
 (0)