trait InstallerLanguageTrait

A trait to install Drupal in a specific language.

Hierarchy

3 files declare their use of InstallerLanguageTrait
HelpTopicTranslatedTestBase.php in core/modules/help/tests/src/Functional/HelpTopicTranslatedTestBase.php
LocaleThemeInstallTest.php in core/modules/locale/tests/src/Functional/LocaleThemeInstallTest.php
NodeTypeTranslationTest.php in core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php

File

core/tests/Drupal/Tests/Traits/Core/Locale/InstallerLanguageTrait.php, line 10

Namespace

Drupal\Tests\Traits\Core\Locale
View source
trait InstallerLanguageTrait {
  
  /**
   * Gets the langcode to install Drupal with.
   *
   * @return string
   *   The langcode to install Drupal with.
   */
  abstract protected function getInstallLangcode() : string;
  
  /**
   * Returns the contents of the translation file.
   *
   * This method should be overridden by the test class to provide a translation
   * file that will be used during the installation.
   *
   * @return string
   *   The contents of the translation file.
   */
  protected function getTranslationFileContents() : string {
    return <<<PO
    msgid ""
    msgstr ""
    
    PO;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function installParameters() : array {
    $parameters = parent::installParameters();
    $parameters['parameters']['langcode'] = $this->getInstallLangcode();
    // Create a po file so we don't attempt to download one from
    // localize.drupal.org and to have a test translation that will not change.
    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
    file_put_contents($this->publicFilesDirectory . '/translations/drupal-' . \Drupal::VERSION . '.' . $this->getInstallLangcode() . '.po', $this->getTranslationFileContents());
    return $parameters;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.