forked from josereyero/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.module
405 lines (364 loc) · 12.9 KB
/
i18n.module
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
// $Id: i18n.module,v 1.41.2.49.2.1 2010/09/14 11:30:30 jareyero Exp $
/**
* @file
* Internationalization (i18n) module.
*
* This module extends multilingual support being the base module for the i18n package.
* - Multilingual variables
* - Extended languages for nodes
* - Extended language API
*
* @author Jose A. Reyero, 2004
*/
// Some constants. Language support modes for content
define('LANGUAGE_SUPPORT_NONE', 0);
define('LANGUAGE_SUPPORT_NORMAL', 1);
define('LANGUAGE_SUPPORT_EXTENDED', 2);
define('LANGUAGE_SUPPORT_EXTENDED_NOT_DISPLAYED', 3);
/**
* Implements hook_init().
*
* Special fix for site_frontpage, that may have been used before the language variables are loaded.
*/
function i18n_init() {
// If not in bootstrap, variable init. Otherwise we are serving a cached page so we don't need anything else.
if (!_i18n_is_bootstrap()) {
_i18n_init_mode();
}
}
/**
* Initialize selection mode
*/
function _i18n_init_mode() {
if (i18n_selection_mode() != 'off') {
// Node language when loading specific nodes or creating translations.
if (arg(0) == 'node' ) {
if (($node = menu_get_object('node')) && $node->language) {
i18n_selection_mode('node', $node->language);
}
elseif (arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language'])) {
i18n_selection_mode('translation', db_escape_string($_GET['language']));
}
}
elseif (arg(0) == 'admin') {
// There are some exceptions for admin pages.
if (arg(1) == 'content' && user_access('administer all languages')) {
// No restrictions for administration pages.
i18n_selection_mode('off');
}
elseif (arg(1) == 'build' && (arg(2) == 'menu-customize' || arg(2) == 'menu')) {
// All nodes available when editing custom menu items.
i18n_selection_mode('off');
}
}
}
}
/**
* Implements hook_help().
*/
function i18n_help($path = 'admin/help#i18n', $arg) {
switch ($path) {
case 'admin/help#i18n' :
$output = '<p>'. t('This module improves support for multilingual content in Drupal sites:') .'</p>';
$output .= '<ul>';
$output .= '<li>'. t('Shows content depending on page language.') .'</li>';
$output .= '<li>'. t('Handles multilingual variables.') .'</li>';
$output .= '<li>'. t('Extended language option for chosen content types. For these content types transations will be allowed for all defined languages, not only for enabled ones.') .'</li>';
$output .= '<li>'. t('Provides a block for language selection and two theme functions: <em>i18n_flags</em> and <em>i18n_links</em>.') .'</li>';
$output .= '</ul>';
$output .= '<p>'. t('This is the base module for several others adding different features:') .'</p>';
$output .= '<ul>';
$output .= '<li>'. t('Multilingual menu items.') .'</li>';
$output .= '<li>'. t('Multilingual taxonomy adds a language field for taxonomy vocabularies and terms.') .'</li>';
$output .= '</ul>';
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@i18n">Internationalization module</a>.', array('@i18n' => 'http://drupal.org/node/133977')) .'</p>';
return $output;
case 'admin/config/language/i18n':
$output = '<ul>';
$output .= '<li>'. t('To enable multilingual support for specific content types go to <a href="@configure_content_types">configure content types</a>.', array('@configure_content_types' => url('admin/structure/types'))) .'</li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_menu().
*/
function i18n_menu() {
$items['admin/config/regional/i18n'] = array(
'title' => 'Multilingual system',
'description' => 'Configure extended options for multilingual content and translations.',
'page callback' => 'drupal_get_form',
'page arguments' => array('i18n_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'i18n.admin.inc',
'weight' => 10,
);
$items['admin/config/regional/i18n/configure'] = array(
'title' => 'Multilingual system',
'description' => 'Configure extended options for multilingual content and translations.',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('i18n_admin_settings'),
//'access arguments' => array('administer site configuration'),
'file' => 'i18n.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_user_login().
*
* Switch to user's language after login.
*/
function i18n_user_login(&$edit, &$account) {
if ($account->language) {
$_SESSION['language'] = $account->language;
i18n_get_lang($account->language);
}
}
/**
* Simple i18n API
*/
/**
* Get language properties.
*
* @param $code
* Language code.
* @param $property
* It may be 'name', 'native', 'ltr'...
*/
function i18n_language_property($code, $property) {
$languages = language_list();
return isset($languages[$code]->$property) ? $languages[$code]->$property : NULL;
}
/**
* Selection mode for content.
*
* Warning: when used with params they need to be escaped, as some values are thrown directly in queries.
*
* Allows several modes for query rewriting and to change them programatically.
* off = No language conditions inserted.
* simple = Only current language and no language.
* mixed = Only current and default languages.
* strict = Only current language.
* default = Only default language.
* user = User defined, in the module's settings page.
* params = Gets the stored params.
* reset = Returns to previous.
* custom = add custom where clause, like "%alias.language = 'en'".
*
* @param $reset
* Whether to reset the internal cache for the selection mode.
*/
function i18n_selection_mode($mode = NULL, $params = NULL, $reset = FALSE) {
$current_mode = &drupal_static(__FUNCTION__ . '_mode', NULL, $reset);
$current_mode = &drupal_static(__FUNCTION__ . '_value', '', $reset);
$store = &drupal_static(__FUNCTION__ . '_store', '', $reset);
// Initialization, first time this runs
if (!isset($current_mode)) {
$current_mode = variable_get('i18n_selection_mode', 'simple');
}
if (!$mode) {
return $current_mode;
}
elseif ($mode == 'params') {
return $current_value;
}
elseif ($mode == 'reset') {
list($current_mode, $current_value) = array_pop($store);
}
else {
array_push($store, array($current_mode, $current_value));
$current_mode = $mode;
$current_value = $params;
}
}
/**
* Implements hook_db_rewrite_sql().
*
* Rewrite node queries so language selection options are enforced.
*/
function i18n_db_rewrite_sql($query, $primary_table, $primary_key, $args = array()) {
// If mode is 'off' = no rewrite, we cannot return any empty 'where' string so check here.
$mode = i18n_selection_mode();
if ($mode == 'off') return;
// Disable language conditions for views.
if (array_key_exists('view', $args)) return;
switch ($primary_table) {
case 'n':
case 'node':
// No rewrite for queries with subselect ? (views count queries).
// @ TO DO Actually these queries look un-rewrittable, check with other developers.
if (preg_match("/FROM \(SELECT/", $query)) return;
// No rewrite for translation module queries.
if (preg_match("/.*FROM {node} $primary_table WHERE.*$primary_table\.tnid/", $query)) return;
// When loading specific nodes, language conditions shouldn't apply.
if (preg_match("/WHERE.*\s$primary_table.nid\s*=\s*(\d|%d)/", $query)) return;
// If language conditions already there, get out.
if (preg_match("/i18n/", $query)) return;
// Mixed mode is a bit more complex, we need to join in one more table
// and add some more conditions, but only if language is not default.
if ($mode == 'mixed') {
$result['where'] = i18n_db_rewrite_where($primary_table, 'node', 'simple');
if (i18n_get_lang() != i18n_default_language()) {
$result['join'] = "LEFT JOIN {node} i18n ON $primary_table.tnid > 0 AND $primary_table.tnid = i18n.tnid AND i18n.language = '". i18n_get_lang() ."'";
// So we show also nodes that have default language.
$result['where'] .= " OR $primary_table.language = '". i18n_default_language() ."' AND i18n.nid IS NULL";
}
}
else {
$result['where'] = i18n_db_rewrite_where($primary_table, 'node', $mode);
}
return $result;
}
}
/**
* Rewrites queries depending on rewriting mode.
*
* @return DatabaseCondition
*/
function i18n_db_rewrite_where($alias, $type, $mode = NULL) {
if (!$mode) {
// Some exceptions for query rewrites.
$mode = i18n_selection_mode();
}
// Get languages to simplify query building.
$current = i18n_get_lang();
$default = i18n_default_language();
if ($mode == 'strict' && $type != 'node') {
// Special case. Selection mode is 'strict' but this should be only for node queries.
$mode = 'simple';
}
elseif ($mode == 'mixed' && $current == $default) {
// If mode is mixed but current = default, is the same as 'simple'.
$mode = 'simple';
}
switch ($mode) {
case 'off':
return '';
case 'simple':
return db_condition('OR')
->condition($alias . '.language', $current)
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'mixed':
return db_condition('OR')
->condition($alias . '.language', $current)
->condition($alias . '.language', $default)
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'strict':
return db_condition('AND')->condition($alias . '.language', $current);
case 'node':
case 'translation':
return db_condition('OR')
->condition($alias . '.language', i18n_selection_mode('params'))
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'default':
return db_condition('OR')
->condition($alias . '.language', $default)
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'custom':
return str_replace('%alias', $alias, i18n_selection_mode('params'));
}
}
/**
* Implements hook_preprocess_page().
*
* Add the language code to the classes for the <body> tag. Unfortunately, some
* themes will not respect the variable we're modifying to achieve this - in
* particular, Garland and Minelli do not.
*/
function i18n_preprocess_page(&$variables) {
if (isset($variables['body_classes'])) {
global $language;
$variables['body_classes'] .= ' i18n-' . $language->language;
}
}
/**
* Implements hook_permission().
*
* Permissions defined
* - administer all languages
* Disables language conditions for administration pages, so the user can view objects for all languages at the same time.
* This applies for: menu items, taxonomy
* - administer translations
* Will allow to add/remove existing nodes to/from translation sets.
*/
function i18n_permission() {
return array(
'administer all languages' => array(
'title' => t('Administer all languages'),
'description' => t('Administer all languages.'),
),
'administer translations' => array(
'title' => t('Administer translations'),
'description' => t('Administer translations.'),
),
);
}
/**
* Process menu and menu item add/edit form submissions.
*/
function i18n_menu_edit_item_form_submit($form, &$form_state) {
$mid = menu_edit_item_save($form_state['values']);
db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", array($form_state['values']['language'], $mid));
return 'admin/build/menu';
}
/**
* Get language from context.
*/
function _i18n_get_context_lang() {
// Node language when loading specific nodes or creating translations.
if (arg(0) == 'node' ) {
if (($node = menu_get_object('node')) && $node->language) {
return $node->language;
}
elseif (arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language'])) {
return $_GET['language'];
}
}
}
/**
* Check whether we are in bootstrap mode.
*/
function _i18n_is_bootstrap() {
return !function_exists('drupal_get_headers');
}
/**
* Drupal 6, backwards compatibility layer.
*
* @ TO DO Fully upgrade all the modules and remove
*/
/**
* This one expects to be called first from common.inc
*/
function i18n_get_lang() {
global $language;
return $language->language;
}
/**
* @defgroup i18n_api Extended language API
* @{
* This is an extended language API to be used by modules in i18n package.
*/
/**
* Returns default language code.
*/
function i18n_default_language() {
return language_default('language');
}
/**
* Get list of supported languages, native name.
*
* @param $all
* TRUE to get all defined languages.
*/
function i18n_supported_languages($all = FALSE) {
return locale_language_list('native', $all);
}
/**
* @} End of "defgroup i18n_api".
*/