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