-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi18n_object.inc
258 lines (228 loc) · 5.57 KB
/
i18n_object.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* @file
* I18n Object Class.
*/
/**
* Object wrapper.
*/
class i18n_object_wrapper {
protected $type;
protected $key;
protected $object;
/**
* Object translations, static cache.
*/
protected $translations;
/**
* Class constructor.
*/
public function __construct($type, $key, $object) {
$this->type = $type;
$this->key = $key;
$this->object = $object ? $object : $this->load_object($key);
}
/**
* Get edit path for object.
*/
public function get_edit_path() {
return $this->path_replace($this->get_info('edit path'));
}
/**
* Get field value from object/array.
*/
public function get_field($field, $default = NULL) {
return i18n_object_field($this->object, $field, $default);
}
/**
* Set field value to object/array.
*/
public function set_field($field, $value) {
if (is_object($this->object)) {
$this->object->$field = $value;
}
elseif (is_array($this->object)) {
$this->object[$field] = $value;
}
return $this;
}
/**
* Get string numeric key for indexing.
*/
public function get_index() {
$key = $this->get_key();
return is_array($key) ? implode(':', $key) : $key;
}
/**
* Get key value from object/array.
*/
public function get_key($default = NULL) {
if ($field = $this->get_info('key')) {
return $this->get_field($field, $default);
}
else {
return $default;
}
}
/**
* Get language code.
*/
public function get_langcode() {
return i18n_object_langcode($this->object);
}
/**
* Get real object or array.
*/
public function get_object() {
return $this->object;
}
/**
* Load real object or array.
*
* @param object|array $object
* Not sure when this can ever be an array, though.
*/
public function load_object($object) {
if ($callback = $this->get_info('load callback', NULL)) {
$this->object = call_user_func($callback, $object);
}
elseif ($entity_type = $this->get_info('entity', NULL)) {
$entity = entity_load($entity_type, array($object));
$this->object = $entity ? reset($entity) : FALSE;
}
return $this->get_object();
}
/**
* Get menu placehoders for object.
*/
protected function get_placeholders() {
$placeholders = $this->get_info('placeholders', array());
foreach ($placeholders as $name => $field) {
$placeholders[$name] = $this->get_field($field);
}
return $placeholders;
}
/**
* Get link for item.
*/
public function get_path() {
if ($uri = entity_uri($this->type, $this->object)) {
return $uri['path'];
}
}
/**
* Get title from item.
*/
public function get_title() {
return entity_label($this->type, $this->object);
}
/**
* Get object type.
*/
public function get_type() {
return $this->type;
}
/**
* Menu access callback for mixed translation tab.
*/
public function get_translate_access() {
switch ($this->get_translate_mode()) {
case I18N_MODE_TRANSLATE:
return $this->translate_access();
case I18N_MODE_LOCALIZE:
return $this->localize_access();
default:
return FALSE;
}
}
/**
* Get translate or localize mode for object.
*/
public function get_translate_mode() {
return I18N_MODE_NONE;
}
/**
* Get translation set id if any.
*/
public function get_tsid() {
return $this->get_field($this->get_translation_info('field', 'i18n_tsid'));
}
/**
* Set translation set id.
*/
public function set_tsid($tsid) {
return $this->set_field($this->get_translation_info('field', 'i18n_tsid'), $tsid);
}
/**
* Localize object if localizable.
*/
public function localize($langcode, $options = array()) {
if ($this->get_translate_mode() == I18N_MODE_LOCALIZE) {
return $this->translate($langcode, $options);
}
else {
return $this->object;
}
}
/**
* Translate object if translatable.
*/
public function translate($langcode, $options = array()) {
if (isset($this->translations[$langcode])) {
return $this->translations[$langcode];
}
else {
return $this->object;
}
}
/**
* Translate access (translation sets).
*/
protected function translate_access() {
return FALSE;
}
/**
* Translate access (localize strings).
*/
protected function localize_access() {
return FALSE;
}
/**
* Replace path with placeholders.
*
* @param string $path
* Path to replace.
* @param array $replacements
* Replacement variables to override or add to placeholders.
*/
protected function path_replace($path, array $replacements = array()) {
if ($path) {
$path = strtr($path, $replacements + $this->get_placeholders());
// Clean up duplicated and final '/' (empty placeholders).
$path = strtr($path, array('//' => '/'));
return trim($path, '/');
}
else {
return '';
}
}
/**
* Get object info.
*/
public function get_info($property, $default = NULL) {
return i18n_object_info($this->type, $property, $default);
}
/**
* Get object translation set info.
*/
public function get_translation_info($property, $default = NULL) {
return function_exists('i18n_translation_set_info') ? i18n_translation_set_info($this->type, $property, $default) : $default;
}
/**
* Get object string translation info.
*/
public function get_string_info($property, $default = NULL) {
$info = $this->get_info('string translation');
return $info && isset($info[$property]) ? $info[$property] : $default;
}
}