function LocaleUpdateBase::setCurrentTranslations

Same name in other branches
  1. 8.9.x core/modules/locale/tests/src/Functional/LocaleUpdateBase.php \Drupal\Tests\locale\Functional\LocaleUpdateBase::setCurrentTranslations()
  2. 10 core/modules/locale/tests/src/Functional/LocaleUpdateBase.php \Drupal\Tests\locale\Functional\LocaleUpdateBase::setCurrentTranslations()
  3. 11.x core/modules/locale/tests/src/Functional/LocaleUpdateBase.php \Drupal\Tests\locale\Functional\LocaleUpdateBase::setCurrentTranslations()

Sets up existing translations and their statuses in the database.

4 calls to LocaleUpdateBase::setCurrentTranslations()
LocaleUpdateTest::testUpdateImportModeNonCustomized in core/modules/locale/tests/src/Functional/LocaleUpdateTest.php
Tests translation import and only overwrite non-customized translations.
LocaleUpdateTest::testUpdateImportModeNone in core/modules/locale/tests/src/Functional/LocaleUpdateTest.php
Tests translation import and don't overwrite any translation.
LocaleUpdateTest::testUpdateImportSourceLocal in core/modules/locale/tests/src/Functional/LocaleUpdateTest.php
Tests translation import from local sources.
LocaleUpdateTest::testUpdateImportSourceRemote in core/modules/locale/tests/src/Functional/LocaleUpdateTest.php
Tests translation import from remote sources.

File

core/modules/locale/tests/src/Functional/LocaleUpdateBase.php, line 214

Class

LocaleUpdateBase
Base class for testing updates to string translations.

Namespace

Drupal\Tests\locale\Functional

Code

protected function setCurrentTranslations() {
    // Add non customized translations to the database.
    $langcode = 'de';
    $context = '';
    $non_customized_translations = [
        'March' => 'Marz',
        'June' => 'Juni',
    ];
    foreach ($non_customized_translations as $source => $translation) {
        $string = $this->container
            ->get('locale.storage')
            ->createString([
            'source' => $source,
            'context' => $context,
        ])
            ->save();
        $this->container
            ->get('locale.storage')
            ->createTranslation([
            'lid' => $string->getId(),
            'language' => $langcode,
            'translation' => $translation,
            'customized' => LOCALE_NOT_CUSTOMIZED,
        ])
            ->save();
    }
    // Add customized translations to the database.
    $customized_translations = [
        'January' => 'Januar_customized',
        'February' => 'Februar_customized',
        'May' => 'Mai_customized',
    ];
    foreach ($customized_translations as $source => $translation) {
        $string = $this->container
            ->get('locale.storage')
            ->createString([
            'source' => $source,
            'context' => $context,
        ])
            ->save();
        $this->container
            ->get('locale.storage')
            ->createTranslation([
            'lid' => $string->getId(),
            'language' => $langcode,
            'translation' => $translation,
            'customized' => LOCALE_CUSTOMIZED,
        ])
            ->save();
    }
    // Add a state of current translations in locale_files.
    $default = [
        'langcode' => $langcode,
        'uri' => '',
        'timestamp' => $this->timestampMedium,
        'last_checked' => $this->timestampMedium,
    ];
    $data[] = [
        'project' => 'contrib_module_one',
        'filename' => 'contrib_module_one-8.x-1.1.de._po',
        'version' => '8.x-1.1',
    ];
    $data[] = [
        'project' => 'contrib_module_two',
        'filename' => 'contrib_module_two-8.x-2.0-beta4.de._po',
        'version' => '8.x-2.0-beta4',
    ];
    $data[] = [
        'project' => 'contrib_module_three',
        'filename' => 'contrib_module_three-8.x-1.0.de._po',
        'version' => '8.x-1.0',
    ];
    $data[] = [
        'project' => 'custom_module_one',
        'filename' => 'custom_module_one.de.po',
        'version' => '',
    ];
    $connection = Database::getConnection();
    foreach ($data as $file) {
        $file = array_merge($default, $file);
        $connection->insert('locale_file')
            ->fields($file)
            ->execute();
    }
}

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