locale.install 11 KB
Newer Older
1
<?php
2
// $Id$
3

4
5
6
7
8
/**
 * @file
 * Install, update and uninstall functions for the locale module.
 */

9
/**
10
 * Implements hook_install().
11
 */
12
function locale_install() {
13
14
15
16
  // locales_source.source and locales_target.target are not used as binary
  // fields; non-MySQL database servers need to ensure the field type is text
  // and that LIKE produces a case-sensitive comparison.

17
18
19
20
21
22
23
24
25
26
27
  db_insert('languages')
    ->fields(array(
      'language' => 'en',
      'name' => 'English',
      'native' => 'English',
      'direction' => 0,
      'enabled' => 1,
      'weight' => 0,
      'javascript' => '',
    ))
    ->execute();
28
}
29

30
31
32
33
34
35
/**
 * @defgroup updates-6.x-to-7.x Locale updates from 6.x to 7.x
 * @{
 */

/**
36
 * Add context field index and allow longer location.
37
38
 */
function locale_update_7000() {
39
40
  db_drop_index('locales_source', 'source');
  db_add_index('locales_source', 'source_context', array(array('source', 30), 'context'));
41
42
43
44
45
46
47
48
49
50
51
52
53

  // Also drop the 'textgroup_location' index added by the i18nstrings module
  // of the i18n project, which prevents the below schema update from running.
  if (db_index_exists('locales_source', 'textgroup_location')) {
    db_drop_index('locales_source', 'textgroup_location');
  }

  db_change_field('locales_source', 'location', 'location', array(
    'type' => 'text',
    'not null' => FALSE,
    'size' => 'big',
    'description' => 'Drupal path in case of online discovered translations or file path in case of imported strings.',
  ));
54
55
}

56
57
58
59
60
/**
 * Upgrade language negotiation settings.
 */
function locale_update_7001() {
  require_once DRUPAL_ROOT . '/includes/language.inc';
61
  require_once DRUPAL_ROOT . '/includes/locale.inc';
62
  require_once DRUPAL_ROOT . '/modules/locale/locale.module';
63
64
65
66
67
68
69
70
71
72

  switch (variable_get('language_negotiation', 0)) {
    // LANGUAGE_NEGOTIATION_NONE.
    case 0:
      $negotiation = array();
      break;

    // LANGUAGE_NEGOTIATION_PATH_DEFAULT.
    case 1:
      $negotiation = array(LOCALE_LANGUAGE_NEGOTIATION_URL);
73
74
75
76
77
      // In Drupal 6 path prefixes are shown for the default language only when
      // language negotiation is set to LANGUAGE_NEGOTIATION_PATH, while in
      // Drupal 7 path prefixes are always shown if not empty. Hence we need to
      // ensure that the default language has an empty prefix to avoid breaking
      // the site URLs with a prefix that previously was missing.
78
79
80
      $default = language_default();
      $default->prefix = '';
      variable_set('language_default', $default);
81
      db_update('languages')
82
83
        ->fields(array('prefix' => $default->prefix))
        ->condition('language', $default->language)
84
        ->execute();
85
86
87
88
89
90
91
92
93
94
95
96
97
98
      break;

    // LANGUAGE_NEGOTIATION_PATH.
    case 2:
      $negotiation = array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_USER, LOCALE_LANGUAGE_NEGOTIATION_BROWSER);
      break;

    // LANGUAGE_NEGOTIATION_DOMAIN.
    case 3:
      variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN);
      $negotiation = array(LOCALE_LANGUAGE_NEGOTIATION_URL);
      break;
  }

99
  // Save the new language negotiation options.
100
101
  language_negotiation_set(LANGUAGE_TYPE_INTERFACE, array_flip($negotiation));
  language_negotiation_set(LANGUAGE_TYPE_CONTENT, array(LOCALE_LANGUAGE_NEGOTIATION_INTERFACE => 0));
102
103
  language_negotiation_set(LANGUAGE_TYPE_URL, array(LOCALE_LANGUAGE_NEGOTIATION_URL => 0));

104
105
106
107
108
  // Save admininstration UI settings.
  $type = LANGUAGE_TYPE_INTERFACE;
  $provider_weights = array_flip(array_keys(locale_language_negotiation_info()));
  variable_set("locale_language_providers_weight_$type", $provider_weights);

109
110
111
112
113
114
  // Unset the old language negotiation system variable.
  variable_del('language_negotiation');

  return array();
}

115
116
117
118
/**
 * @} End of "defgroup updates-6.x-to-7.x"
 */

119
/**
120
 * Implements hook_uninstall().
121
122
 */
function locale_uninstall() {
123
  // Delete all JavaScript translation files.
124
  $locale_js_directory = 'public://' . variable_get('locale_js_directory', 'languages');
125
126
127
128
129
130
131
132
133
134

  if (is_dir($locale_js_directory)) {
    $files = db_query('SELECT language, javascript FROM {languages}');
    foreach ($files as $file) {
      if (!empty($file->javascript)) {
        file_unmanaged_delete($locale_js_directory . '/' . $file->language . '_' . $file->javascript . '.js');
      }
    }
    // Delete the JavaScript translations directory if empty.
    if (!file_scan_directory($locale_js_directory, '/.*/')) {
135
      drupal_rmdir($locale_js_directory);
136
    }
137
  }
138

139
140
141
  // Clear variables.
  variable_del('language_default');
  variable_del('language_count');
142
143
144
  variable_del('language_types');
  variable_del('locale_language_negotiation_url_part');
  variable_del('locale_language_negotiation_session_param');
145
146
147
148
  variable_del('language_content_type_default');
  variable_del('language_content_type_negotiation');
  variable_del('locale_cache_strings');
  variable_del('locale_js_directory');
149
  variable_del('javascript_parsed');
150
  variable_del('locale_field_language_fallback');
151
  variable_del('locale_cache_length');
152
153
154
155
156

  foreach (language_types() as $type) {
    variable_del("language_negotiation_$type");
    variable_del("locale_language_providers_weight_$type");
  }
157

158
  foreach (node_type_get_types() as $type => $content_type) {
159
    $setting = variable_del("language_content_type_$type");
160
  }
161

162
  // Switch back to English: with a $language->language value different from 'en'
163
  // successive calls of t() might result in calling locale(), which in turn might
164
  // try to query the unexisting {locales_source} and {locales_target} tables.
165
  drupal_language_initialize();
166

167
}
168
169

/**
170
 * Implements hook_schema().
171
172
173
 */
function locale_schema() {
  $schema['languages'] = array(
174
    'description' => 'List of all available languages in the system.',
175
    'fields' => array(
176
177
178
179
180
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
181
        'description' => "Language code, e.g. 'de' or 'en-US'.",
182
183
184
185
186
187
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
188
        'description' => 'Language name in English.',
189
190
191
192
193
194
      ),
      'native' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
195
        'description' => 'Native language name.',
196
197
198
199
200
      ),
      'direction' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
201
        'description' => 'Direction of language (Left-to-Right = 0, Right-to-Left = 1).',
202
203
204
205
206
      ),
      'enabled' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
207
        'description' => 'Enabled flag (1 = Enabled, 0 = Disabled).',
208
209
210
211
212
      ),
      'plurals' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
213
        'description' => 'Number of plural indexes in this language.',
214
215
216
217
218
219
      ),
      'formula' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
220
        'description' => 'Plural formula in PHP code to evaluate to get plural indexes.',
221
222
223
224
225
226
      ),
      'domain' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
227
        'description' => 'Domain to use for this language.',
228
229
230
231
232
233
      ),
      'prefix' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
234
        'description' => 'Path prefix to use for this language.',
235
236
237
238
239
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
240
        'description' => 'Weight, used in lists of languages.',
241
242
243
      ),
      'javascript' => array(
        'type' => 'varchar',
244
        'length' => 64,
245
246
        'not null' => TRUE,
        'default' => '',
247
        'description' => 'Location of JavaScript translation file.',
248
      ),
249
250
    ),
    'primary key' => array('language'),
251
252
253
    'indexes' => array(
      'list' => array('weight', 'name'),
    ),
254
255
256
  );

  $schema['locales_source'] = array(
257
    'description' => 'List of English source strings.',
258
    'fields' => array(
259
260
261
      'lid' => array(
        'type' => 'serial',
        'not null' => TRUE,
262
        'description' => 'Unique identifier of this string.',
263
264
      ),
      'location' => array(
265
266
267
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
268
        'description' => 'Drupal path in case of online discovered translations or file path in case of imported strings.',
269
270
271
272
273
274
      ),
      'textgroup' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'default',
275
        'description' => 'A module defined group of translations, see hook_locale().',
276
277
278
      ),
      'source' => array(
        'type' => 'text',
279
        'mysql_type' => 'blob',
280
        'not null' => TRUE,
281
        'description' => 'The original string in English.',
282
      ),
283
284
285
286
287
288
289
      'context' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The context this string applies to.',
      ),
290
291
292
293
294
      'version' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'default' => 'none',
295
        'description' => 'Version of Drupal, where the string was last used (for locales optimization).',
296
      ),
297
298
    ),
    'primary key' => array('lid'),
299
    'indexes' => array(
300
      'source_context' => array(array('source', 30), 'context'),
301
    ),
302
303
304
  );

  $schema['locales_target'] = array(
305
    'description' => 'Stores translated versions of strings.',
306
    'fields' => array(
307
308
309
310
      'lid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
311
        'description' => 'Source string ID. References {locales_source}.lid.',
312
313
314
      ),
      'translation' => array(
        'type' => 'text',
315
        'mysql_type' => 'blob',
316
        'not null' => TRUE,
317
        'description' => 'Translation string value in this language.',
318
319
320
321
322
323
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
324
        'description' => 'Language code. References {languages}.language.',
325
326
327
      ),
      'plid' => array(
        'type' => 'int',
328
        'not null' => TRUE, // This should be NULL for no referenced string, not zero.
329
        'default' => 0,
330
        'description' => 'Parent lid (lid of the previous string in the plural chain) in case of plural strings. References {locales_source}.lid.',
331
332
333
334
335
      ),
      'plural' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
336
        'description' => 'Plural index number in case of plural strings.',
337
      ),
338
    ),
339
    'primary key' => array('language', 'lid', 'plural'),
340
    'foreign keys' => array(
341
342
343
344
      'locales_source' => array(
        'table' => 'locales_source',
        'columns' => array('lid' => 'lid'),
      ),
345
    ),
346
    'indexes' => array(
347
348
349
      'lid'      => array('lid'),
      'plid'     => array('plid'),
      'plural'   => array('plural'),
350
351
352
353
354
355
    ),
  );

  return $schema;
}