TranslationString.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Drupal\locale;
  3. /**
  4. * Defines the locale translation string object.
  5. *
  6. * This class represents a translation of a source string to a given language,
  7. * thus it must have at least a 'language' which is the language code and a
  8. * 'translation' property which is the translated text of the source string
  9. * in the specified language.
  10. */
  11. class TranslationString extends StringBase {
  12. /**
  13. * The language code.
  14. *
  15. * @var string
  16. */
  17. public $language;
  18. /**
  19. * The string translation.
  20. *
  21. * @var string
  22. */
  23. public $translation;
  24. /**
  25. * Integer indicating whether this string is customized.
  26. *
  27. * @var int
  28. */
  29. public $customized;
  30. /**
  31. * Boolean indicating whether the string object is new.
  32. *
  33. * @var bool
  34. */
  35. protected $isNew;
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function __construct($values = []) {
  40. parent::__construct($values);
  41. if (!isset($this->isNew)) {
  42. // We mark the string as not new if it is a complete translation.
  43. // This will work when loading from database, otherwise the storage
  44. // controller that creates the string object must handle it.
  45. $this->isNew = !$this->isTranslation();
  46. }
  47. }
  48. /**
  49. * Sets the string as customized / not customized.
  50. *
  51. * @param bool $customized
  52. * (optional) Whether the string is customized or not. Defaults to TRUE.
  53. *
  54. * @return \Drupal\locale\TranslationString
  55. * The called object.
  56. */
  57. public function setCustomized($customized = TRUE) {
  58. $this->customized = $customized ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED;
  59. return $this;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function isSource() {
  65. return FALSE;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function isTranslation() {
  71. return !empty($this->lid) && !empty($this->language) && isset($this->translation);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getString() {
  77. return isset($this->translation) ? $this->translation : '';
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function setString($string) {
  83. $this->translation = $string;
  84. return $this;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function isNew() {
  90. return $this->isNew;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function save() {
  96. parent::save();
  97. $this->isNew = FALSE;
  98. return $this;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function delete() {
  104. parent::delete();
  105. $this->isNew = TRUE;
  106. return $this;
  107. }
  108. }