function LocaleImportFunctionalTest::testLanguageContext

Same name and namespace in other branches
  1. 7.x modules/locale/locale.test \LocaleImportFunctionalTest::testLanguageContext()
  2. 9 core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testLanguageContext()
  3. 8.9.x core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testLanguageContext()
  4. 10 core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testLanguageContext()

Tests msgctxt context support.

File

core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php, line 264

Class

LocaleImportFunctionalTest
Tests the import of locale files.

Namespace

Drupal\Tests\locale\Functional

Code

public function testLanguageContext() : void {
  // Try importing a .po file.
  $this->importPoFile($this->getPoFileWithContext(), [
    'langcode' => 'hr',
  ]);
  // We cast the return value of t() to string so as to retrieve the
  // translated value, rendered as a string.
  $this->assertSame('Svibanj', (string) $this->t('May', [], [
    'langcode' => 'hr',
    'context' => 'Long month name',
  ]), 'Long month name context is working.');
  $this->assertSame('Svib.', (string) $this->t('May', [], [
    'langcode' => 'hr',
    'context' => 'Abbreviated month name',
  ]), 'Abbreviated month name context is working.');
  $this->assertSame('Svi.', (string) $this->t('May', [], [
    'langcode' => 'hr',
  ]), 'Default context is working.');
  $this->assertSame('sv', (string) $this->t('st', [], [
    'langcode' => 'hr',
  ]), 'Default context for "saint" is working.');
  $this->assertSame('.', (string) $this->t('st', [], [
    'langcode' => 'hr',
    'context' => 'Day ordinal suffix',
  ]), 'Day ordinal suffix context is working.');
  // Ensure that the date formatter applies the right translation context.
  $formatted_date = $this->container
    ->get('date.formatter')
    ->format(483820620, 'custom', 'jS F Y', 'America/New_York', 'hr');
  $this->assertEquals('1. Svibanj 1985', $formatted_date, 'Got the right formatted date using the date format translation pattern.');
}

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