You are here

potx_test_8.module.txt in Translation template extractor 8

Same filename and directory in other branches
  1. 7.3 tests/potx_test_8.module.txt

File used purely to test the parser in potx. This file has a .txt extension so that it does not fail linting tests run by continuous integration.

File

tests/potx_test_8.module.txt
View source
  1. // @codingStandardsIgnoreFile
  2. /**
  3. * @file
  4. * File used purely to test the parser in potx. This file has a .txt
  5. * extension so that it does not fail linting tests run by continuous
  6. * integration.
  7. */
  8. use Drupal\Core\StringTranslation\TranslatableMarkup;
  9. use Drupal\Core\StringTranslation\TranslationWrapper;
  10. use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
  11. /**
  12. * @TestAnnotation(
  13. * id = "test_annotation",
  14. * test_label = @Translation("Good translation annotation"),
  15. * test_label_context = @Translation("Translation in good context", context="Translation test")
  16. * )
  17. */
  18. function potx_test_8_example() {
  19. // Tested with long format array style.
  20. $a = new TranslationWrapper('TranslationWrapper string');
  21. $b = new TranslationWrapper('TranslationWrapper string with context', array(), array('context' => 'With context'));
  22. // Test with short format array styles mixed with other tokens.
  23. $a = new TranslatableMarkup('TranslatableMarkup string');
  24. $c = new TranslatableMarkup('TranslatableMarkup string without context', []);
  25. $d = new TranslatableMarkup('TranslatableMarkup string with long array context', [], array('context' => 'With context'));
  26. $d = new TranslatableMarkup('TranslatableMarkup string with short array context', [], ['context' => 'With context']);
  27. $e = new TranslatableMarkup('TranslatableMarkup string with long array followed by short array context', array(), ['context' => 'With context']);
  28. $f = new TranslatableMarkup('TranslatableMarkup string with complicated tokens', array([], 2, potx_test_8_sample(), array(), [[]]), ['context' => 'With context']);
  29. $g = new TranslatableMarkup('TranslatableMarkup string with complicated option tokens', [], ['foo' => potx_test_8_sample([4]), 'bar' => [12, 14, []], 'context' => 'With context', 'eek' => 'ook']);
  30. // Test with @placeholders and short array context
  31. $z = new TranslatableMarkup('TranslatableMarkup string with a @placeholder', array(@placeholder => 'foo'));
  32. $h = new TranslatableMarkup('TranslatableMarkup string with a @placeholder and context',
  33. array('@placeholder' => $foo),
  34. array('context' => 'Test context')
  35. );
  36. $i = new TranslatableMarkup('TranslatableMarkup string with a @placeholder and trailing comma and context',
  37. array('@placeholder' => $foo, ),
  38. array('context' => 'Test context')
  39. );
  40. $j = new TranslatableMarkup('TranslatableMarkup string with two @place @holders and context',
  41. array(
  42. '@place' => $foo,
  43. '@holders' => $foo,
  44. ),
  45. array('context' => 'Test context')
  46. );
  47. $k = new TranslatableMarkup('TranslatableMarkup string with a @placeholder and short array syntax context',
  48. ['@placeholder' => $foo],
  49. ['context' => 'Test context']
  50. );
  51. $l = new TranslatableMarkup('TranslatableMarkup string with a @placeholder and trailing comma and short array syntax context',
  52. ['@placeholder' => $foo, ],
  53. ['context' => 'Test context']
  54. );
  55. $m = new TranslatableMarkup('TranslatableMarkup string with two @place @holders and short array syntax context',
  56. [
  57. '@place' => $foo,
  58. '@holders' => $foo,
  59. ],
  60. ['context' => 'Test context']
  61. );
  62. $a->debug('Debug message');
  63. $b->info('Info message');
  64. $c->notice('Notice message');
  65. $d->warning('Warning message');
  66. $e->error('Error message');
  67. $f->critical('Critical message');
  68. $g->alert('Alert message');
  69. $h->emergency('Emergency message');
  70. $i->log(1, 'Log message');
  71. $j->log('false positive');
  72. $k->log($boo, 'Log message 2');
  73. $l->log($another_false_positive);
  74. $m->log(potx_test_8_sample(), 'Log message 3');
  75. $n->log();
  76. }
  77. /**
  78. * Let's see if we catch errors in @Translation() annotation.
  79. *
  80. * @TestAnnotation2(
  81. * test_1 = @Translation("Bad translation annotation),
  82. * test_2 = @Translation("Another good translation annotation"),
  83. * test_3 = @Translation Even worse,
  84. * test_4 = @Translation("Final good translation annotation"),
  85. * )
  86. */
  87. function potx_test_8_sample() {
  88. }
  89. /**
  90. * Empty annotations are not allowed either.
  91. *
  92. * @TestAnnotation3(
  93. * test = @Translation("")
  94. * )
  95. */
  96. function potx_test_8_empty() {
  97. }
  98. /**
  99. * Test parsing Drupal 8 $this->t and formatPlural function calls.
  100. */
  101. class PotxTestD8 {
  102. /**
  103. *
  104. */
  105. public function test_func() {
  106. // In the real world, we'd also need to define "function t", and inject
  107. // the translation service. please refer to https://drupal.org/node/2079611
  108. // for an example.
  109. $test_translation = $this->t('Using t inside D8 classes ($this->t)');
  110. }
  111. /**
  112. *
  113. */
  114. public function testFormatPlural() {
  115. $this->formatPlural($count, '1 formatPlural test string', '@count formatPlural test strings');
  116. $this->formatPlural($count, '1 formatPlural test string in context', '@count formatPlural test strings in context', array(), array('context' => 'Test context'));
  117. $this->formatPlural($count);
  118. $this->formatPlural();
  119. \Drupal::translation()->formatPlural($count, '1 translation->formatPlural test string in context', '@count translation->formatPlural test strings in context', array(), array('context' => 'Test context'));
  120. \Drupal::translation()->formatPlural($count, '1 translation->formatPlural test string', '@count translation->formatPlural test strings');
  121. $a = new PluralTranslatableMarkup($count, '1 PluralTranslatableMarkup test string', '@count PluralTranslatableMarkup test strings');
  122. $b = new PluralTranslatableMarkup($count, '1 PluralTranslatableMarkup test string with context', '@count PluralTranslatableMarkup test strings with context', array(), array('context' => 'Test context'));
  123. $c = new PluralTranslatableMarkup(
  124. $count,
  125. '1 PluralTranslatableMarkup test string in context with two @place @holders, short array syntax',
  126. '@count PluralTranslatableMarkup test strings in context with two @place @holders, short array sytanx',
  127. [
  128. '@place' => $foo,
  129. '@holders' => $foo,
  130. ],
  131. ['context' => 'Test context']
  132. );
  133. }
  134. }
  135. /**
  136. * Test parsing inline templates inside PHP files in Drupal 8.
  137. */
  138. function potx_test_inline_templates() {
  139. $render_array = array(
  140. '#type' => 'inline_template',
  141. '#template' => "Here is a {% trans 'Test translatable string inside an inline template' %}.",
  142. );
  143. $render_array['#template'] = 'Here is {% trans %}Another test translatable string inside an inline template{% endtrans %}';
  144. $render_array["#template"] = 'Here is {% trans %}A translatable string inside an inline template, with double-quoted "#template" key{% endtrans %}';
  145. }
  146. /**
  147. * Test that potx ignores translation keywords in non-translation situations, without causing notices.
  148. */
  149. class TestWrapper extends TranslatableMarkup {}