Projet

Général

Profil

Paste
Télécharger (4,89 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_localization.inc @ 5d12d676

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_localization.
6
 */
7

    
8
/**
9
 * @defgroup views_localization_plugins Views localization plugins
10
 * @{
11
 * @todo.
12
 *
13
 * @see hook_views_plugins()
14
 */
15

    
16
/**
17
 * The base plugin to handle localization of Views strings.
18
 */
19
class views_plugin_localization extends views_plugin {
20

    
21
  /**
22
   * Store for exported strings.
23
   */
24
  public $export_strings = array();
25

    
26
  /**
27
   *
28
   */
29
  public $translate = TRUE;
30

    
31
  /**
32
   * Initialize the plugin.
33
   *
34
   * @param view $view
35
   *   The view object.
36
   */
37
  public function init(&$view) {
38
    $this->view = &$view;
39
  }
40

    
41
  /**
42
   * Translate a string / text with format.
43
   *
44
   * The $source parameter is an array with the following elements:
45
   * - value, source string
46
   * - format, input format in case the text has some format to be applied
47
   * - keys. An array of keys to identify the string. Generally constructed from
48
   *   view name, display_id, and a property, e.g., 'header'.
49
   *
50
   * @param string $source
51
   *   Full data for the string to be translated.
52
   *
53
   * @return string
54
   *   Translated string / text.
55
   */
56
  public function translate($source) {
57
    // Allow other modules to make changes to the string before and after
58
    // translation.
59
    $source['pre_process'] = $this->invoke_translation_process($source, 'pre');
60
    $source['translation'] = $this->translate_string($source['value'], $source['keys'], $source['format']);
61
    $source['post_process'] = $this->invoke_translation_process($source, 'post');
62
    return $source['translation'];
63
  }
64

    
65
  /**
66
   * Translate a string.
67
   *
68
   * @param string $string
69
   *   The string to be translated.
70
   * @param array $keys
71
   *   An array of keys to identify the string. Generally constructed from
72
   *   view name, display_id, and a property, e.g. 'header'.
73
   * @param string $format
74
   *   The input format of the string. This is optional.
75
   */
76
  public function translate_string($string, $keys = array(), $format = '') {}
77

    
78
  /**
79
   * Save string source for translation.
80
   *
81
   * @param string $source
82
   *   Full data for the string to be translated.
83
   */
84
  public function save($source) {
85
    // Allow other modules to make changes to the string before saving.
86
    $source['pre_process'] = $this->invoke_translation_process($source, 'pre');
87
    $this->save_string($source['value'], $source['keys'], isset($source['format']) ? $source['format'] : '');
88
  }
89

    
90
  /**
91
   * Save a string for translation.
92
   *
93
   * @param string $string
94
   *   The string to be translated.
95
   * @param array $keys
96
   *   An array of keys to identify the string. Generally constructed from
97
   *   view name, display_id, and a property, e.g., 'header'.
98
   * @param string $format
99
   *   The input format of the string. This is optional.
100
   */
101
  public function save_string($string, $keys = array(), $format = '') {}
102

    
103
  /**
104
   * Delete a string.
105
   *
106
   * @param string $source
107
   *   Full data for the string to be translated.
108
   */
109
  public function delete($source) {
110
  }
111

    
112
  /**
113
   * Collect strings to be exported to code.
114
   *
115
   * @param string $source
116
   *   Full data for the string to be translated.
117
   */
118
  public function export($source) {
119
  }
120

    
121
  /**
122
   * Render any collected exported strings to code.
123
   *
124
   * @param string $indent
125
   *   An optional indentation for prettifying nested code.
126
   */
127
  public function export_render($indent = '  ') {
128
  }
129

    
130
  /**
131
   * Invoke hook_translation_pre_process() or hook_translation_post_process().
132
   *
133
   * Like node_invoke_nodeapi(), this function is needed to enable both passing
134
   * by reference and fetching return values.
135
   */
136
  public function invoke_translation_process(&$value, $op) {
137
    $return = array();
138
    $hook = 'translation_' . $op . '_process';
139
    foreach (module_implements($hook) as $module) {
140
      $function = $module . '_' . $hook;
141
      $result = $function($value);
142
      if (isset($result)) {
143
        $return[$module] = $result;
144
      }
145
    }
146
    return $return;
147
  }
148

    
149
  /**
150
   *
151
   */
152
  public function process_locale_strings($op) {
153
    $this->view->init_display();
154

    
155
    foreach ($this->view->display as $display_id => $display) {
156
      $translatable = array();
157
      // Special handling for display title.
158
      if (isset($display->display_title)) {
159
        $translatable[] = array('value' => $display->display_title, 'keys' => array('display_title'));
160
      }
161
      // Unpack handlers.
162
      if (is_object($this->view->display[$display_id]->handler)) {
163
        $this->view->display[$display_id]->handler->unpack_translatables($translatable);
164
      }
165
      foreach ($translatable as $data) {
166
        $data['keys'] = array_merge(array($this->view->name, $display_id), $data['keys']);
167
        switch ($op) {
168
          case 'save':
169
            $this->save($data);
170
            break;
171

    
172
          case 'delete':
173
            $this->delete($data);
174
            break;
175

    
176
          case 'export':
177
            $this->export($data);
178
            break;
179
        }
180
      }
181
    }
182
  }
183

    
184
}
185

    
186
/**
187
 * @}
188
 */