Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 66 additions & 30 deletions CurrentLanguageCommands.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,83 @@
<?php

declare(strict_types=1);

namespace Drush\Commands\drush_current_language;

use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\LanguageNegotiatorInterface;
use Drush\Boot\BootstrapManager;
use Drush\Boot\DrupalBootLevels;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
* Ensures the current language is in use.
*/
class CurrentLanguageCommands extends DrushCommands {
final class CurrentLanguageCommands extends DrushCommands
{
use AutowireTrait;

/**
* Ensure current language is set.
*
* @hook pre-command *
*/
public function preCommand(CommandData $commandData) {
// Drupal must be fully bootstrapped in order to use this validation.
$boot_manager = Drush::bootstrapManager();
if (!$boot_manager->hasBootstrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
return;
public function __construct(
protected BootstrapManager $bootstrapManager,
protected LanguageManagerInterface $languageManager,
protected LanguageNegotiatorInterface $languageNegotiator,
protected TranslationManager $translationManager,
protected AccountProxyInterface $currentUser
) {
}
try {
/** @var \Drupal\Core\Language\LanguageManagerInterface $languageManager */
$languageManager = \Drupal::service('language_manager');
/** @var \Drupal\language\LanguageNegotiatorInterface $negotiator */
$negotiator = \Drupal::service('language_negotiator');

public function getBootstrapManager(): BootstrapManager
{
return $this->bootstrapManager;
}

public function getLanguageManager(): LanguageManagerInterface
{
return $this->languageManager;
}
catch (ServiceNotFoundException $exception) {
// If we do not have these services, this command is not really useful.
return;

public function getLanguageNegotiator(): LanguageNegotiatorInterface
{
return $this->languageNegotiator;
}
$negotiator->setCurrentUser(\Drupal::currentUser());

if ($languageManager instanceof ConfigurableLanguageManagerInterface) {
$languageManager->setNegotiator($negotiator);
$languageManager->setConfigOverrideLanguage($languageManager->getCurrentLanguage());
public function getTranslationManager(): TranslationManager
{
return $this->translationManager;
}
$translation = \Drupal::translation();
$translation->setDefaultLangcode($languageManager->getCurrentLanguage()->getId());
}

public function getCurrentUser(): AccountProxyInterface
{
return $this->currentUser;
}

/**
* Ensure current language is set.
*/
#[CLI\Hook(type: HookManager::PRE_COMMAND_HOOK, target: '*')]
public function preCommand(CommandData $commandData)
{
// Drupal must be fully bootstrapped in order to use this validation.
if (!$this->getBootstrapManager()->hasBootstrapped(DrupalBootLevels::FULL)) {
return;
}
try {
$languageManager = $this->getLanguageManager();
$negotiator = $this->getLanguageNegotiator();
} catch (ServiceNotFoundException $exception) {
// If we do not have these services, this command is not really useful.
return;
}
$negotiator->setCurrentUser($this->getCurrentUser());

if ($languageManager instanceof ConfigurableLanguageManagerInterface) {
$languageManager->setNegotiator($negotiator);
$languageManager->setConfigOverrideLanguage($languageManager->getCurrentLanguage());
}
$this->getTranslationManager()->setDefaultLangcode($languageManager->getCurrentLanguage()->getId());
}
}
17 changes: 13 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
"name": "zaporylie/drush-current-language",
"description": "Makes sure the current language is correctly resolved and translation_manager knows of it.",
"type": "drupal-drush",
"require": {
"drush/drush": "~9||~10||~11||~12"
},
"license": "GPL-2.0+",
"authors": [
{
"name": "Jakub Piasecki",
"email": "jakub@piaseccy.pl"
}
]
],
"config": {
"platform": {
"php": "8.2"
}
},
"require": {
"php": ">=8.2",
"drush/drush": "~13"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.7"
}
}
16 changes: 16 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="drush">
<description>Default PHP CodeSniffer configuration for Drush project.</description>
<file>src</file>
<file>examples</file>
<file>tests</file>
<rule ref="PSR12"/>

<!-- Keep us sane-->
<rule ref="Generic.ControlStructures.InlineControlStructure.NotAllowed"><severity>0</severity></rule>
<rule ref="PSR2.Methods.MethodDeclaration.Underscore"><severity>0</severity></rule>
<rule ref="PSR12.Files.FileHeader.IncorrectOrder"><severity>0</severity></rule>

<arg name="warning-severity" value="0"/>
<arg value="s" />
</ruleset>