public function DrushLanguageCliService::add in Drush Language Commands 8
Add and import one or more new language definitions.
Parameters
\Symfony\Component\Console\Style\StyleInterface|\Drupal\drush_language\Drush8Io $io: The $io interface of the cli tool calling.
callable $t: The translation function akin to t().
array $langcodes: A list of langcodes for which a definition will be added.
File
- src/
Service/ DrushLanguageCliService.php, line 106
Class
- DrushLanguageCliService
- Class DrushLanguageCliService.
Namespace
Drupal\drush_language\ServiceCode
public function add($io, callable $t, array $langcodes) {
if (empty($langcodes)) {
$io
->error($t('Please provide one or more comma-separated language codes as arguments.'));
return;
}
foreach ($langcodes as $langcode) {
$messageArgs = [
'@langcode' => $langcode,
];
// In the foreach loop because the list changes on successful iterations.
$languages = $this->languageManager
->getLanguages();
// Do not re-add existing languages.
if (isset($languages[$langcode])) {
$io
->warning($t('The language with code @langcode already exists.', $messageArgs));
continue;
}
// Only allow adding languages for predefined langcodes.
// In the foreach loop because the list changes on successful iterations.
$predefined = $this->languageManager
->getStandardLanguageListWithoutConfigured();
if (!isset($predefined[$langcode])) {
$io
->warning($t('Invalid language code: @langcode', $messageArgs));
continue;
}
// Add the language definition.
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->save();
// Download and import translations for the newly added language if
// interface translation is enabled.
if ($this->moduleHandler
->moduleExists('locale')) {
module_load_include('fetch.inc', 'locale');
$options = _locale_translation_default_update_options();
if ($batch = locale_translation_batch_update_build([], [
$langcode,
], $options)) {
batch_set($batch);
$batch =& batch_get();
$batch['progressive'] = FALSE;
// Process the batch.
drush_backend_batch_process();
}
}
$io
->text($t('Added language: @langcode', $messageArgs));
}
}