Projet

Général

Profil

Paste
Télécharger (10,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_translation / i18n_translation.module @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Internationalization (i18n) module - Entity translations
6
 */
7

    
8
// Language list with only enabled languages
9
define('I18N_ENTITY_LANGUAGES_ENABLED', 0);
10
// Language list with all languages
11
define('I18N_ENTITY_LANGUAGES_EXTENDED', 1);
12

    
13
/**
14
 * Default entity controller for notifications objects
15
 */
16
class I18nTranslationSetController extends DrupalDefaultEntityController {
17
  /**
18
   * Builds objects of specific classes upon loading.
19
   *
20
   * @param $queried_entities
21
   *   Associative array of query results, keyed on the entity ID.
22
   * @param $revision_id
23
   *   ID of the revision that was loaded, or FALSE if teh most current revision
24
   *   was loaded.
25
   */
26
  protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
27
    foreach ($queried_entities as $id => $entity) {
28
      $queried_entities[$id] = i18n_translation_set_build($entity->type, $entity);
29
    }
30
    return parent::attachLoad($queried_entities, $revision_id);
31
  }
32
}
33

    
34
/**
35
 * Implements hook_entity_info().
36
 */
37
function i18n_translation_entity_info() {
38
  $bundles = array();
39
  foreach (i18n_translation_set_info() as $type => $info) {
40
    $bundles[$type] = array(
41
      'label' => $info['title'],
42
    );
43
  }
44
  $return = array(
45
    'i18n_translation' => array(
46
      'label' => t('Translation set'),
47
      'controller class' => 'I18nTranslationSetController',
48
      'base table' => 'i18n_translation_set',
49
      //'uri callback' => 'taxonomy_term_uri',
50
      'fieldable' => FALSE,
51
      'entity keys' => array(
52
        'id' => 'tsid',
53
        'bundle' => 'type',
54
        'label' => 'title',
55
      ),
56
      'bundle keys' => array(
57
        'bundle' => 'type',
58
      ),
59
      'bundles' => $bundles,
60
    ),
61
  );
62

    
63
  return $return;
64
}
65

    
66
/**
67
 * Implements hook_menu()
68
 */
69
function i18n_translation_menu() {
70
  $items['admin/config/regional/i18n_translation'] = array(
71
    'title' => 'Translation sets',
72
    'description' => 'Translation sets overview.',
73
    'page callback' => 'i18n_translation_admin_overview',
74
    //'page arguments' => array('i18n_translation_set_overview'),
75
    'access arguments' => array('administer site configuration'),
76
    'file' => 'i18n_translation.admin.inc',
77
    'weight' => 10,
78
  );
79
  $items['admin/config/regional/i18n_translation/configure'] = array(
80
    'title' => 'Translation sets',
81
    'description' => 'Overview of existing translation sets.',
82
    'type' => MENU_DEFAULT_LOCAL_TASK,
83
  );
84
  return $items;
85
}
86

    
87
/**
88
 * Implements hook_hook_info().
89
 */
90
function i18n_translation_hook_info() {
91
  $hooks['i18n_translation_set_info'] = array(
92
    'group' => 'i18n',
93
  );
94
  return $hooks;
95
}
96

    
97
/**
98
 * Check whether this object can be part of a translation set
99
 */
100
function i18n_translation_check_object($type, $object) {
101
  if ($info = i18n_translation_set_info($type)) {
102

    
103
  }
104
}
105

    
106
/**
107
 * Get form element for translation mode and language
108
 *
109
 * @param $object_type
110
 *   Object type for the container element
111
 * @param $i18n_mode
112
 *   Current or default translation mode
113
 * @param $langcode
114
 *   Current or default language code
115
 * @param $options
116
 *   Restricted list of translation modes if we don't want all of them
117
 */
118
function i18n_translation_mode_element($object_type, $i18n_mode = I18N_MODE_NONE, $langcode = LANGUAGE_NONE, $options = NULL) {
119
  $form['i18n_translation'] = array(
120
    '#type' => 'fieldset',
121
    '#title' => t('Multilingual options'),
122
    '#collapsible' => TRUE,
123
  );
124
  $form['i18n_translation']['i18n_mode'] = array(
125
    '#type' => 'radios',
126
    '#title' => t('Translation mode'),
127
    '#options' => i18n_translation_options($object_type, $options),
128
    '#default_value' => $i18n_mode,
129
    '#description' => t('For localizable elements, to have all items available for translation visit the <a href="@locale-refresh">translation refresh</a> page.', array('@locale-refresh' => url('admin/config/regional/translate/i18n_string'))),
130
  );
131
  $form['i18n_translation']['language'] = array(
132
    '#default_value' => $langcode ? $langcode : LANGUAGE_NONE,
133
    '#description' => t('Predefined language. If set, it will apply to all items.'),
134
    '#required' => TRUE,
135
    '#states' => array(
136
      'visible' => array('input[name="i18n_mode"]' => array('value' => (string)I18N_MODE_LANGUAGE)),
137
    ),
138
  ) + i18n_element_language_select();
139

    
140
  // The option value 'Language neutral' makes no sense here.
141
  $form['i18n_translation']['language']['#options'][LANGUAGE_NONE] = t('- Select a language -');
142
  return $form;
143
}
144

    
145
/**
146
 * Get list of translation modes
147
 *
148
 * @param $container_type
149
 *   Object type for the container
150
 * @param $options
151
 *   Options to include. If none, defaults for container type will be returned.
152
 */
153
function i18n_translation_options($container_type, $options = NULL) {
154
  // Get names and translation options for container object and items
155
  $container_info = i18n_object_info($container_type, 'translation container');
156
  $replacements = array(
157
    '@container_name' => $container_info['name'],
158
    '@item_name_multiple' => $container_info['item name'],
159
    '@item_name_multiple_capitalized' => ucfirst($container_info['item name']),
160
  );
161
  $options = $options ? $options : $container_info['options'];
162
  return i18n_translation_options_list($replacements, $options);
163
}
164

    
165
/**
166
 * Get list of translation modes
167
 */
168
function i18n_translation_options_list($replacements = array(), $options = array()) {
169
  $list = array(
170
    I18N_MODE_NONE => t('No multilingual options for @item_name_multiple. Only the @container_name will be translatable.', $replacements),
171
    I18N_MODE_LOCALIZE => t('Localize. @item_name_multiple_capitalized are common for all languages, but their name and description may be localized.', $replacements),
172
    I18N_MODE_TRANSLATE => t('Translate. Different @item_name_multiple will be allowed for each language and they can be translated.', $replacements),
173
    I18N_MODE_MULTIPLE => t('Translate and Localize. @item_name_multiple_capitalized with language will allow translations. @item_name_multiple_capitalized without language will be localized.', $replacements),
174
    I18N_MODE_LANGUAGE => t('Fixed Language. @item_name_multiple_capitalized will have a global language and they will only show up for pages in that language.', $replacements),
175
  );
176
  if ($options) {
177
    foreach (array_keys($list) as $key) {
178
      if (!in_array($key, $options, TRUE)) {
179
        unset($list[$key]);
180
      }
181
    }
182
  }
183
  return $list;
184
}
185

    
186
/**
187
 * Build translation fieldset for object
188
 */
189
function i18n_translation_set_element($type, $object) {
190
  $element = array(
191
    '#type' => 'fieldset',
192
    '#title' => t('Translations'),
193
  );
194
  if ($set = i18n_translation_object($type, $object)) {
195
    $element['values']['#markup'] = i18n_translation_format_items($set->item_list());
196
  }
197
  else {
198
    $element['message']['#markup'] = t('No translations');
199
  }
200
  return $element;
201
}
202

    
203
/**
204
 * Format translation set info as table
205
 */
206
function i18n_translation_format_items($translations) {
207
  foreach ($translations as $langcode => $item) {
208
    $rows[] = array(i18n_language_name($langcode), $item);
209
  }
210
  return !empty($rows) ? theme('table', array('rows' => $rows)) : '';
211
}
212

    
213
/**
214
 * Get translation set for object
215
 */
216
function i18n_translation_object($type, $object, $create = FALSE) {
217
  if (($field = i18n_translation_set_info($type, 'field', 'i18n_tsid')) && ($tsid = i18n_object_field($object, $field))) {
218
    return i18n_translation_set_load($tsid, $type);
219
  }
220
  elseif ($create) {
221
    $set = i18n_translation_set_build($type);
222
    if ($langcode = i18n_object_langcode($object)) {
223
      $set->add_item($object, $langcode);
224
    }
225
    return $set;
226
  }
227
}
228

    
229
/**
230
 * Get information about translation sets
231
 */
232
function i18n_translation_set_info($type = NULL, $property = NULL, $default = NULL) {
233
  $info = &drupal_static(__FUNCTION__);
234
  if (!$info) {
235
    $info = module_invoke_all('i18n_translation_set_info');
236
    drupal_alter('i18n_translation_set_info', $info);
237
  }
238
  if ($property && $type) {
239
    return isset($info[$type][$property]) ? $info[$type][$property] : $default;
240
  }
241
  elseif ($type) {
242
    return isset($info[$type]) ? $info[$type] : $default;
243
  }
244
  else {
245
    return $info;
246
  }
247
}
248

    
249
/**
250
 * Build a translation set from type, data
251
 */
252
function i18n_translation_set_build($type, $data = array()) {
253
  $class = i18n_translation_set_info($type, 'class', 'i18n_translation_set');
254
  $set = new $class((array)$data);
255
  $set->type = $type;
256
  return $set;
257
}
258

    
259
/**
260
 * Create a new translation set
261
 */
262
function i18n_translation_set_create($type, $bundle = '', $translations = NULL, $master_id = 0) {
263
  $set = i18n_translation_set_build($type, array('type' => $type, 'bundle' => $bundle, 'master_id' => $master_id, 'translations' => $translations));
264
  $set->insert();
265
  return $set;
266
}
267

    
268
/**
269
 * Load single translation set.
270
 *
271
 * @param int $tsid
272
 *   Translation set id.
273
 * @param string $type
274
 *   (Optional) translation set type (bundle).
275
 */
276
function i18n_translation_set_load($tsid, $type = NULL) {
277
  $conditions['tsid'] = $tsid;
278
  $list = entity_load('i18n_translation', array($tsid));
279
  $entity = reset($list);
280
  if ($entity && $type && $entity->type != $type) {
281
    return NULL;
282
  }
283
  return $entity;
284
}
285

    
286
/**
287
 * Index objects in translation set by language
288
 */
289
function i18n_translation_set_index($translations) {
290
  $list = array();
291
  foreach ($translations as $object) {
292
    if ($lang = i18n_object_langcode($object)) {
293
      $list[$lang] = $object;
294
    }
295
  }
296
  return $list;
297
}
298

    
299
/**
300
 * Translation set generic form
301
 */
302
function i18n_translation_set_overview($form, &$form_state, $type = NULL, $tsids = NULL) {
303
  module_load_include('admin.inc', 'i18n_translation');
304
  return i18n_translation_admin_form($form, $form_state, $type, $tsids);
305
}
306

    
307
/**
308
 * Generate a tabular listing of translations for this type.
309
 */
310
function i18n_translation_set_list_manage($type) {
311
  module_load_include('admin.inc', 'i18n_translation');
312
  return i18n_translation_admin_overview($type);
313
}
314

    
315
/**
316
 * Ask for confirmation of translation set deletion
317
 */
318
function i18n_translation_set_delete_confirm($form, &$form_state, $translation_set) {
319
  $form['#translation_set'] = $translation_set;
320
  $form['tsid'] = array(
321
    '#type' => 'value',
322
    '#value' => $translation_set->tsid,
323
  );
324
  if ($items = $translation_set->item_list()) {
325
    $form['items'] = array(
326
      '#type' => 'item',
327
      '#title' => t('Items in this translation set'),
328
      '#markup' => theme('item_list', array('items' => $items)),
329
    );
330
  }
331
  return confirm_form($form,
332
    t('Are you sure you want to delete %title translation set?', array('%title' => $translation_set->get_title())),
333
    i18n_translation_set_info($translation_set->type, 'list path'),
334
    t('This action cannot be undone.'),
335
    t('Delete'),
336
    t('Cancel')
337
  );
338
}
339

    
340
/**
341
 * Execute translation set deletion
342
 */
343
function i18n_translation_set_delete_confirm_submit($form, &$form_state) {
344
  if ($form_state['values']['confirm']) {
345
    $set = i18n_translation_set_load($form_state['values']['tsid']);
346
    $set->delete(TRUE);
347
    drupal_set_message(t('The translation set has been deleted.'));
348
  }
349

    
350
  $form_state['redirect'] = i18n_translation_set_info($set->type, 'list path');
351
}