comparison core/modules/locale/src/TranslationString.php @ 0:4c8ae668cc8c

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