function LocaleUpdateBase::makePoFile

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

Creates a translation file and tests its timestamp.

Parameters

string $path: Path of the file relative to the public file path.

string $filename: Name of the file to create.

int $timestamp: (optional) Timestamp to set the file to. Defaults to current time.

array $translations: (optional) Array of source/target value translation strings. Only singular strings are supported, no plurals. No double quotes are allowed in source and translations strings.

2 calls to LocaleUpdateBase::makePoFile()
LocaleTranslationChangeProjectVersionTest::setUp in core/modules/locale/tests/src/Functional/LocaleTranslationChangeProjectVersionTest.php
LocaleUpdateBase::setTranslationFiles in core/modules/locale/tests/src/Functional/LocaleUpdateBase.php
Setup the environment containing local and remote translation files.

File

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

Class

LocaleUpdateBase
Base class for testing updates to string translations.

Namespace

Drupal\Tests\locale\Functional

Code

protected function makePoFile($path, $filename, $timestamp = NULL, array $translations = []) {
    $timestamp = $timestamp ? $timestamp : \Drupal::time()->getRequestTime();
    $path = 'public://' . $path;
    $text = '';
    $po_header = <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"

EOF;
    // Convert array of translations to Gettext source and translation strings.
    if ($translations) {
        foreach ($translations as $source => $target) {
            $text .= 'msgid "' . $source . '"' . "\n";
            $text .= 'msgstr "' . $target . '"' . "\n";
        }
    }
    \Drupal::service('file_system')->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
    $fileUri = $path . '/' . $filename;
    $file = File::create([
        'uid' => 1,
        'filename' => $filename,
        'uri' => $fileUri,
        'filemime' => 'text/x-gettext-translation',
        'timestamp' => $timestamp,
    ]);
    $file->setPermanent();
    file_put_contents($file->getFileUri(), $po_header . $text);
    touch(\Drupal::service('file_system')->realpath($file->getFileUri()), $timestamp);
    $file->save();
    $this->assertTrue(file_exists($fileUri));
    $this->assertEquals($timestamp, filemtime($fileUri));
}

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