Skip to content

Commit f846311

Browse files
committed
Adds option to download only core translations
1 parent 789d339 commit f846311

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ projects installed. It also runs on `composer require` command.
2525
You can manually download the localization files according to your configuration
2626
by using `composer drupal:l10n`.
2727

28+
If you only need Drupal core translations (for example so Drush can detect and
29+
install languages), use:
30+
31+
`composer drupal:l10n --core-only`
32+
2833
## Configuration
2934

3035
You can configure the plugin by providing some settings in the `extra` section

src/DrupalL10nCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected function configure() {
2424
->setDescription('Download Drupal translations files.')
2525
->setDefinition([
2626
new InputOption('no-dev', NULL, InputOption::VALUE_NONE, 'Disables download of translations of require-dev dependencies.'),
27+
new InputOption('core-only', NULL, InputOption::VALUE_NONE, 'Downloads translations for Drupal core only (skip contrib modules/themes/profiles).'),
2728
]);
2829
}
2930

@@ -32,7 +33,7 @@ protected function configure() {
3233
*/
3334
protected function execute(InputInterface $input, OutputInterface $output): int {
3435
$handler = new Handler($this->getComposer(), $this->getIO());
35-
$handler->downloadLocalization(!$input->getOption('no-dev'));
36+
$handler->downloadLocalization(!$input->getOption('no-dev'), [], $input->getOption('core-only'));
3637
return 0;
3738
}
3839

src/Handler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Handler {
2828
'drupal-theme',
2929
'drupal-profile',
3030
];
31+
const DRUPAL_CORE_PACKAGE_NAMES = [
32+
'drupal/core',
33+
'drupal/drupal',
34+
];
3135

3236
/**
3337
* The composer object.
@@ -132,8 +136,11 @@ public function onPostCmdEvent(Event $event) {
132136
* A list of Packages to download the localization. If empty, the
133137
* localization for all Drupal packages detected in the installation will be
134138
* downloaded.
139+
* @param bool $coreOnly
140+
* TRUE to download Drupal core localization only. FALSE to include contrib
141+
* projects.
135142
*/
136-
public function downloadLocalization($dev = TRUE, array $packages = []) {
143+
public function downloadLocalization($dev = TRUE, array $packages = [], $coreOnly = FALSE) {
137144
$drupal_core_package = $this->getDrupalCorePackage();
138145
// Ensure drupal core package is present.
139146
if (is_null($drupal_core_package)) {
@@ -149,6 +156,10 @@ public function downloadLocalization($dev = TRUE, array $packages = []) {
149156
$packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages();
150157
}
151158
foreach ($packages as $package) {
159+
if ($coreOnly && !in_array($package->getName(), self::DRUPAL_CORE_PACKAGE_NAMES)) {
160+
continue;
161+
}
162+
152163
// Filter by the type of package.
153164
if (in_array($package->getType(), $this::DRUPAL_L10N_PACKAGE_TYPES)) {
154165
// Include development project or not.

tests/PluginTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,34 @@ public function testContribmodules() {
143143
$this->assertFileExists($semver_fr_translation_file, 'French translations file should exist.');
144144
}
145145

146+
/**
147+
* Tests that core-only option skips contrib translations.
148+
*/
149+
public function testCoreOnlyOption() {
150+
$core_version = '9.5.3';
151+
$contrib_module = 'entity_share';
152+
$contrib_composer_version = '3.0.0-rc4';
153+
$contrib_drupal_version = '8.x-3.0-rc4';
154+
$translations_directory = $this->tmpDir . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR . 'contrib';
155+
$core_translation_file = $translations_directory . DIRECTORY_SEPARATOR . 'drupal-' . $core_version . '.fr.po';
156+
$contrib_translation_file = $translations_directory . DIRECTORY_SEPARATOR . $contrib_module . '-' . $contrib_drupal_version . '.fr.po';
157+
158+
$this->composer('install');
159+
$this->composer('require --update-with-dependencies drupal/core:"' . $core_version . '"');
160+
$this->composer('require drupal/' . $contrib_module . ':"' . $contrib_composer_version . '"');
161+
$this->assertFileExists($core_translation_file, 'Drupal core translation should exist after install.');
162+
$this->assertFileExists($contrib_translation_file, 'Contrib translation should exist after install.');
163+
164+
$this->fs->remove($core_translation_file);
165+
$this->fs->remove($contrib_translation_file);
166+
$this->assertFileDoesNotExist($core_translation_file, 'Drupal core translation should not exist after removal.');
167+
$this->assertFileDoesNotExist($contrib_translation_file, 'Contrib translation should not exist after removal.');
168+
169+
$this->composer('drupal:l10n --core-only');
170+
$this->assertFileExists($core_translation_file, 'Drupal core translation should exist after --core-only command.');
171+
$this->assertFileDoesNotExist($contrib_translation_file, 'Contrib translation should not exist after --core-only command.');
172+
}
173+
146174
/**
147175
* Tests that on Drupal 7, core and contrib modules are handled.
148176
*/

0 commit comments

Comments
 (0)