TranslationInterface.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Drupal\Core\StringTranslation;
  3. /**
  4. * Interface for the translation.manager translation service.
  5. *
  6. * @ingroup i18n
  7. */
  8. interface TranslationInterface {
  9. /**
  10. * Translates a string to the current language or to a given language.
  11. *
  12. * Never call this translate() method directly. In order for strings to be
  13. * localized, make them available in one of the ways supported by the
  14. * @link https://www.drupal.org/node/322729 Localization API @endlink. When
  15. * possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait
  16. * $this->t(). Otherwise create a new
  17. * \Drupal\Core\StringTranslation\TranslatableMarkup object.
  18. *
  19. * @param string $string
  20. * A string containing the English text to translate.
  21. * @param array $args
  22. * (optional) An associative array of replacements to make after
  23. * translation. Based on the first character of the key, the value is
  24. * escaped and/or themed. See
  25. * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
  26. * details.
  27. * @param array $options
  28. * (optional) An associative array of additional options, with the following
  29. * elements:
  30. * - 'langcode' (defaults to the current language): A language code, to
  31. * translate to a language other than what is used to display the page.
  32. * - 'context' (defaults to the empty context): The context the source
  33. * string belongs to. See the
  34. * @link i18n Internationalization topic @endlink for more information
  35. * about string contexts.
  36. *
  37. * @return \Drupal\Core\StringTranslation\TranslatableMarkup
  38. * An object that, when cast to a string, returns the translated string.
  39. *
  40. * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
  41. * @see \Drupal\Core\StringTranslation\TranslatableMarkup::__construct()
  42. *
  43. * @ingroup sanitization
  44. */
  45. public function translate($string, array $args = [], array $options = []);
  46. /**
  47. * Translates a TranslatableMarkup object to a string.
  48. *
  49. * @param \Drupal\Core\StringTranslation\TranslatableMarkup $translated_string
  50. * A TranslatableMarkup object.
  51. *
  52. * @return string
  53. * The translated string.
  54. */
  55. public function translateString(TranslatableMarkup $translated_string);
  56. /**
  57. * Formats a string containing a count of items.
  58. *
  59. * This function ensures that the string is pluralized correctly. Since
  60. * TranslationInterface::translate() is called by this function, make sure not
  61. * to pass already-localized strings to it. See
  62. * PluralTranslatableMarkup::createFromTranslatedString() for that.
  63. *
  64. * For example:
  65. * @code
  66. * $output = $string_translation->formatPlural($node->comment_count, '1 comment', '@count comments');
  67. * @endcode
  68. *
  69. * Example with additional replacements:
  70. * @code
  71. * $output = $string_translation->formatPlural($update_count,
  72. * 'Changed the content type of 1 post from %old-type to %new-type.',
  73. * 'Changed the content type of @count posts from %old-type to %new-type.',
  74. * array('%old-type' => $info->old_type, '%new-type' => $info->new_type));
  75. * @endcode
  76. *
  77. * @param int $count
  78. * The item count to display.
  79. * @param string $singular
  80. * The string for the singular case. Make sure it is clear this is singular,
  81. * to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not
  82. * use @count in the singular string.
  83. * @param string $plural
  84. * The string for the plural case. Make sure it is clear this is plural, to
  85. * ease translation. Use @count in place of the item count, as in
  86. * "@count new comments".
  87. * @param array $args
  88. * An associative array of replacements to make after translation. Instances
  89. * of any key in this array are replaced with the corresponding value.
  90. * Based on the first character of the key, the value is escaped and/or
  91. * themed. See \Drupal\Component\Render\FormattableMarkup. Note that you do
  92. * not need to include @count in this array; this replacement is done
  93. * automatically for the plural cases.
  94. * @param array $options
  95. * An associative array of additional options. See t() for allowed keys.
  96. *
  97. * @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
  98. * A translated string.
  99. *
  100. * @see \Drupal\Core\StringTranslation\TranslationInterface::translate()
  101. * @see t()
  102. * @see \Drupal\Component\Render\FormattableMarkup
  103. * @see \Drupal\Core\StringTranslation\PluralTranslatableMarkup::createFromTranslatedString()
  104. */
  105. public function formatPlural($count, $singular, $plural, array $args = [], array $options = []);
  106. }