summaryrefslogtreecommitdiffstats
path: root/i18n.module
blob: 3c013502e7de000a951b798abfb6678545ebaf1c (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<?php
// $Id$

/**
 * @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);

/**
 * Implementation of hook_init()
 * 
 * Will initialize language dependent variables
 * Modify rewriting conditions when viewing specific nodes
 */
function i18n_init(){
  // If not in bootstrap, variable init
  if(!_i18n_is_bootstrap()){
    //include drupal_get_path('module', 'i18n').'/i18n.inc';
    i18n_variable_init();    
  }
  if (arg(0) == 'node') {
    if(isset($_POST['language']) && $_POST['language']) {
      $pagelang = $_POST['language'];
    } elseif( is_numeric(arg(1)) && $node = node_load(arg(1)) ) {
      // Node language when loading specific nodes
      $pagelang = $node->language;
    }
    if(isset($pagelang)) i18n_selection_mode('node', db_escape_string($pagelang));
  } elseif(arg(0) == 'admin' && arg(0) == 'content' && user_access('administer all languages')) {
    // No restrictions for administration pages
    i18n_selection_mode('off');
  }
}

/**
 * Implementation of hook_help().
 */
function i18n_help($section = 'admin/help#i18n' ) {
  switch ($section) {
    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: <i>i18n_flags</i> and <i>i18n_links</i>').'</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 please read the <a href="@i18n">on-line help pages</a>.', array('@i18n' =>'http://drupal.org/node/31631')) .'</p>';
      return $output;
    case 'admin/settings/i18n':
      $output = '<ul>';
      $output .= '<li>'.t('To manage languages go to the !configure_languages', array('!configure_languages' => l(t('Languages configuration page'), 'admin/settings/language'))).'</li>';
      $output .= '<li>'.t('To enable multilingual support for specific content types go to !configure_content_types.', array('!configure_content_types' => l(t('configure content types'), 'admin/content/types'), )).'</li>';
      $output . '</ul>';
      return $output;
  }
}

/**
 * Implementation of hook_menu().
 */
function i18n_menu() {
  $items['admin/settings/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',
  );
  $items['admin/settings/i18n/main'] = array(
    'title' => 'Internationalization',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  return $items;
}

/**
 * Implementation of hook_menu_alter().
 * 
 * Take over the node translation page
 */
function i18n_menu_alter(&$items) {
  // dsm($router_items);
  /*
  $items['node/%node/translate']['page callback'] = 'i18n_translation_node_overview';
  $items['node/%node/translate']['file'] = 'i18n.pages.inc';
  $items['node/%node/translate']['module'] = 'i18n';
  */
  /**
  $items = array();
  $items['node/%node/translate'] = array(
    'title' => 'Translate',
    'page callback' => 'translation_node_overview',
    'page arguments' => array(1),
    'access callback' => '_translation_tab_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'i18n.pages.inc',
  );
  return $items;
  */
}

/**
 * Implementation of hook_nodeapi().
 */
/*
function i18n_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if (variable_get("i18n_node_$node->type", 0)) {
      switch ($op) {
      case 'load':
        return db_fetch_array(db_query("SELECT trid, language, status AS i18n_status FROM {i18n_node} WHERE nid=%d", $node->nid));
      case 'insert':
      case 'update':
        db_query("DELETE FROM {i18n_node} WHERE nid=%d",$node->nid);
        if ($node->language){
          // Assign a trid from the beginning
          db_query("INSERT INTO {i18n_node} (nid, trid, language, status) VALUES(%d, '%d', '%s', '%d')", $node->nid, $node->trid, $node->language, $node->i18n_status);
        }
        // Handle menu items. Fixes duplication issue and language for menu items which happens when editing nodes in languages other than current.
        if (isset($node->menu) && !$node->menu['delete'] && $node->menu['title']) {
          $item = $node->menu;
          $item['path'] = ($item['path']) ? $item['path'] : "node/$node->nid";
          $item['type'] = $item['type'] | MENU_MODIFIED_BY_ADMIN;
          if ($item['mid']) {
            // Update menu item 
            db_query("UPDATE {menu} SET pid = %d, path = '%s', title = '%s', description = '%s', weight = %d, type = %d, language = '%s' WHERE mid = %d", $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type'], $node->language, $item['mid']);
            drupal_set_message(t('The menu item %title has been updated with node language.', array('%title' => $item['title'])));
          } elseif(SAVED_NEW == menu_save_item($item)) {
            // Creating new menu item with node language
            db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", $node->language, $item['mid']);
            drupal_set_message(t('The menu item %title has been added with node language.',  array('%title' => $item['title'])));         
          }
          menu_rebuild();
          unset($node->menu); // Avoid further processing by menu module
        }
        // Pathauto integration. Dynamic replacement of variables to allow different patterns per language
        if (module_exists('path') && module_exists('pathauto')) {
          // Language for pathauto variables is either node language or default language
          $language = $node->language ? $node->language : i18n_default_language();
          if ($language != i18n_get_lang()) {
            i18n_variable_init($language, 'pathauto_node');
          }
        }
        break;
      case 'delete': 
        db_query('DELETE FROM {i18n_node} WHERE nid=%d', $node->nid);
        break;
      case 'prepare':
        // Book pages, set the right language nodes and outlines
        if (arg(3) == 'parent' && is_numeric(arg(4)) && ($parent = node_load(arg(4))) && $parent->language) {
          $node->language = $parent->language;
          i18n_selection_mode('node', $parent->language);
        } 
        break;
      }
  }   
}
*/

/**
 * Implementation of hook_alter_translation_link().
 *
 * Handles links for extended language. The links will have current language
 */
function i18n_translation_link_alter(&$links, $path) {
  global $language;
  
  // Check for a node related path, and for its translations.
  if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
    $languages = language_list();
    $extended = array();
    foreach (translation_node_get_translations($node->tnid) as $langcode => $translation_node) {
      if (!isset($links[$langcode]) && isset($languages[$langcode])) {
        $extended[$langcode] = array(
           'href' => 'node/'.$translation_node->nid . $matches[2],
           'language' => $language,
           'language_icon' => $languages[$langcode],
           'title' => $languages[$langcode]->native,
           'attributes' => array('class' => 'language-link'),
        );
      }
    }
    // This will run after languageicon module, so we add icon in case that one is enabled
    if ($extended && function_exists('languageicons_translation_link_alter')) {
      languageicons_translation_link_alter($extended, $path);
    }
    $links = array_merge($links, $extended);
  }
}

/**
 * Implementation of hook_link_alter().
 *
 * Handles links for extended languages. Sets current interface language;
 */
function i18n_link_alter(&$links, $node) {
  global $language;
  
  if ($node->tnid) {
    foreach (array_keys(i18n_language_list('extended')) as $langcode) {
      $index = 'node_translation_'.$langcode;
      if (!empty($links[$index])) {
        $links[$index]['language'] = $language;
      }
    }
  }
}

/**
 * Implementation of hook_user()
 * 
 * Switch to user's language after login
 */
function i18n_user($op, &$edit, &$account, $category = NULL) {
  if($op == 'login' && $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;
}

/**
 * Get node language
 */
function i18n_node_get_lang($nid, $default = '') {
  $lang = db_result(db_query('SELECT language FROM {node} WHERE nid = %d',$nid));
  return $lang ? $lang : $default ;
}

/**
 * Get allowed languages for node
 * 
 * This allows node types to define its own language list implementing hook 'language_list'
 * 
 * @param $node
 *   Node to retrieve language list for
 * @param $translate
 *   Only languages available for translation. Filter out existing translations
 */
function i18n_node_language_list($node, $translate = FALSE) {
  // Check if the node module manages its own language list
  $languages = node_invoke($node, 'language_list', $translate);
  if (!$languages) {
    if (variable_get('i18n_node_'.$node->type, 0) == LANGUAGE_SUPPORT_EXTENDED) {
      $languages = locale_language_list('name', TRUE); // All defined languages
    } else {
      $languages = locale_language_list(); // All enabled languages
    }
    if ($translate && isset($node->tnid) && $node->tnid && ($translations = translation_node_get_translations($node->tnid))) {
      unset($translations[$node->language]);
      foreach (array_keys($translations) as $langcode) {
        unset($languages[$langcode]);
      }
    }
    $languages = array('' => t('Language neutral')) + $languages;
  }

  return $languages;
}

/**
 * Function i18n_get_links
 * 
 * Returns an array of links for all languages, with or without names/flags
 * 
 * @param $path
 *   Drupal internal path
 * @param $query
 *   Query string
 * @param $names
 *   Names to use for the links. Defaults to native language names
 */
function i18n_get_links($path = '', $query = NULL, $names = NULL) {
  if($path == i18n_frontpage()) {
    $path = '';
  }
  $names = $names ? $names : locale_language_list('native');
  foreach (array_keys(i18n_supported_languages()) as $lang){
    $links[$lang]= theme('i18n_link', $names[$lang], i18n_path($path, $lang), $lang, $query);
  }
  return $links;  
}

/**
 * i18n_selection_mode
 * 
 * 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'"
 */
function i18n_selection_mode($mode= NULL, $params= NULL){
  static $current_mode = 'simple';
  static $current_value = '';
  static $store = array();
  
  if(!$mode) {
    return $current_mode;
  } elseif($mode == 'params'){
    return $current_value;
  } elseif($mode == 'reset'){
    list($current_mode, $current_value) = array_pop($store);
    //drupal_set_message("i18n mode reset mode=$current_mode value=$current_value");
  } else {
    array_push($store, array($current_mode, $current_value));
    $current_mode = $mode;
    $current_value = $params;
  } 
}

/**
 * Implementation of hook_db_rewrite_sql()
 */
function i18n_db_rewrite_sql($query, $primary_table, $primary_key){

  switch ($primary_table) {
    case 'n':
    case 'node':
      // Node queries.
      // 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;

      //$result['join'] = "LEFT JOIN {i18n_node} i18n ON $primary_table.nid = i18n.nid";
      $result['where'] = i18n_db_rewrite_where($primary_table, 'node');
      return $result;
  }
}

/**
 * Rewrites queries depending on rewriting mode
 */
function i18n_db_rewrite_where($alias, $type, $mode = NULL){
  if (!$mode) {
    // Some exceptions for query rewrites
    $mode = i18n_selection_mode();
    // drupal_set_message("i18n_db_rewrite mode=$mode query=$query");
  } 
    // Special case. Selection mode is 'strict' but this should be only for node queries
    if ($mode == 'strict' && $type != 'node') {
      $mode = 'simple';
    }   
  switch($mode){
    case 'off':
      return '1 = 1';
    case 'simple':
      return "$alias.language ='".i18n_get_lang()."' OR $alias.language ='' OR $alias.language IS NULL" ;
    case 'mixed':
      return "$alias.language ='".i18n_get_lang()."' OR $alias.language ='".i18n_default_language()."' OR $alias.language ='' OR $alias.language IS NULL" ;
    case 'strict':
      return "$alias.language ='".i18n_get_lang()."'" ;
    case 'node':
    case 'translation':
      return "$alias.language ='".i18n_selection_mode('params')."' OR $alias.language ='' OR $alias.language IS NULL" ;
    case 'default':
      return "$alias.language ='".i18n_default_language()."' OR $alias.language ='' OR $alias.language IS NULL" ;
    case 'custom':
      return str_replace('%alias', $alias, i18n_selection_mode('params'));
  }  
}

/**
 * Implementation of hook_exit
 */
function i18n_exit(){
  _i18n_variable_exit();
}

/**
 * Implementation of hook_form_alter();
 * 
 * This is the place to add language fields to all forms
 */
function i18n_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id){
     case 'node_type_form':
       // Add extended language support option to content type form.
       $form['workflow']['i18n_node'] = array(
          '#type' => 'radios',
          '#title' => t('Extended language support'),
          '#default_value' => variable_get('i18n_node_'. $form['#node_type']->type, LANGUAGE_SUPPORT_NORMAL),
          '#options' => _i18n_content_language_options(),
          '#description' => t('If enabled, all defined languages will be allowed for this content type in addition to only enabled ones. This is useful to have more languages for content than for the interface.')
       );

       break;

    default:
      // Extended language for node edit form
      if (isset($form['#id']) && $form['#id'] == 'node-form') {
        if (isset($form['#node']->type) && variable_get('language_content_type_'. $form['#node']->type, 0)) {
          $form['language']['#options'] = i18n_node_language_list($form['#node'], TRUE);
        }
      }
      /** @ TO DO Upgrade
      if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && $node = $form['#node']) {
        // Language field
        if(variable_get('i18n_node_'.$form['type']['#value'], 0) && !isset($form['i18n']['language'])) {
          // Language field
          $form['i18n'] = array('#type' => 'fieldset', '#title' => t('Multilingual settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => -4);
          // Language will default to current only when creating a node
          $language = isset($form['#node']->language) ? $form['#node']->language : (arg(1)=='add' ? i18n_get_lang() : '');
          $form['i18n']['language'] = _i18n_language_select($language, t('If you change the Language, you must click on <i>Preview</i> to get the right Categories &amp; Terms for that language.'), -4, i18n_node_language_list($node));
          $form['i18n']['trid'] = array('#type' => 'value', '#value' => $form['#node']->trid);
        }
        // Correction for lang/node/nid aliases generated by path module
        // if($form['#node']->path && $form['#node']->path == i18n_get_lang().'/node/'.$form['#node']->nid){
        if($node->path) {
          $alias = drupal_lookup_path('alias', 'node/'.$node->nid);
          if($alias && $alias != 'node/'.$node->nid){
            $form['#node']->path = $alias;
          } else {
            unset($form['#node']->path);
          }
        }
      }
      */
      // Multilingual variables in settings form
      if (isset($form['#theme']) && $form['#theme'] == 'system_settings_form' && $variables = variable_get('i18n_variables', 0)) {
        if (i18n_form_alter_settings($form, $variables)) {
          $form['#submit'][] = 'i18n_variable_form_submit';
        }
      }
  }    
}

/**
 * Implementation of hook_perm().
 * 
 * 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
 */
function i18n_perm() {
  return array('administer all languages');
}
/**
 * Process menu and menu item add/edit form submissions.
 */
function i18n_menu_edit_item_form_submit($form_id, $form_values) {
  $mid = menu_edit_item_save($form_values);
  db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", $form_values['language'], $mid);
  return 'admin/build/menu';
}

/**
 * Check for multilingual variables in form
 */
function i18n_form_alter_settings(&$form, &$variables) {
  $result = 0;
  foreach (element_children($form) as $field) {
    if(isset($form[$field]['#type']) && $form[$field]['#type'] == 'fieldset') {
      $result += i18n_form_alter_settings($form[$field], $variables);
    }
    elseif (in_array($field, $variables)) {
      $form[$field]['#description'] .= ' <strong>'.t('This is a multilingual variable.').'</strong>';
      $result++;
    }
  }
  return $result;
}

/**
 * Save multilingual variables and remove them from form
 */
function i18n_variable_form_submit($form, &$form_state) {
  $form_values = $form_state['values'];
  $op = isset($form_values['op']) ? $form_values['op'] : '';
  $variables = variable_get('i18n_variables', array());
  $language = i18n_get_lang();
  foreach ($form_values as $key => $value) {
    if (in_array($key, $variables)) {
      if ($op == t('Reset to defaults')) {
        i18n_variable_del($key, $language);
      }
      else {
        if (is_array($value) && isset($form_values['array_filter'])) {
          $value = array_keys(array_filter($value));
        }
        i18n_variable_set($key, $value, $language);
      }
      unset($form_values[$key]);
    } 
  }
  // Re-submit form
  // system_settings_form_submit($form_id, $form_values);
}

/**
 * Initialization of multilingual variables
 * 
 * @param $language
 *   Language to retrieve variables. Defaults to current language
 * @param $prefix
 *   Variable name prefix to load just a selected group of variables
 */
function i18n_variable_init($language = NULL, $prefix = ''){
  global $conf;
  global $i18n_conf;
  $language = $language ? $language : i18n_get_lang();
  if ($i18n_variables = variable_get('i18n_variables', '')){
    if (!$i18n_conf) {
      $i18n_conf = array();
    }
    $variables = _i18n_variable_init($language, $prefix);
    foreach($i18n_variables as $name){
      $i18n_conf[$name] = isset($variables[$name]) ? $variables[$name] : (isset($conf[$name]) ? $conf[$name] : '');
    }
    $conf = array_merge($conf, $i18n_conf);
  }
}



/**
 * Helper function to create language selector
 */
function _i18n_language_select($value ='', $description ='', $weight = -20, $languages = NULL){
  $languages = $languages ? $languages : locale_language_list();
  return array(
      '#type' => 'select',
      '#title' => t('Language'),
      '#default_value' => $value,
      '#options' => array_merge(array('' => ''), $languages),
      '#description' => $description,
      '#weight' => $weight,
  );
}

/**
 * Load language variables into array
 */
function _i18n_variable_init($language, $prefix = ''){  
  $variables = array();
  $cacheid = 'variables:'.$language.($prefix ? ':'.$prefix : '');
  if ($cached = cache_get($cacheid)) {
    $variables = $cached->data;
  }
  else {
    $result = db_query("SELECT * FROM {i18n_variable} WHERE language='%s' AND name LIKE '%s%'", $language, $prefix);
    while ($variable = db_fetch_object($result)) {
      $variables[$variable->name] = unserialize($variable->value);
    }
    cache_set($cacheid, $variables);
  }
  return $variables;
}

/**
 * Save multilingual variables that may have been changed by other methods than settings pages
 */
function _i18n_variable_exit(){
  global $i18n_conf;
  global $conf;
  if($i18n_conf){
    $lang = i18n_get_lang();
    $refresh = FALSE;
    // Rewritten because array_diff_assoc may fail with array variables
    foreach($i18n_conf as $name => $value){
      if($value != $conf[$name]) {
        $refresh = TRUE;
        $i18n_conf[$name] = $conf[$name];
        db_query("DELETE FROM {i18n_variable} WHERE name='%s' AND language='%s'", $name, $lang );
        db_query("INSERT INTO {i18n_variable} (language, name, value) VALUES('%s', '%s', '%s')", $lang, $name, serialize($conf[$name]));
      }
    }
    if($refresh) {
      cache_set('variables:'.$lang, 'cache', serialize($i18n_conf));
    }
  }
}

/**
 * Check whether we are in bootstrap mode
 */  
function _i18n_is_bootstrap(){
  return !function_exists('drupal_get_headers');
}    

/**
 * Sets db_prefix to given language
 */
function _i18n_set_db_prefix($lang) {
  global $db_prefix, $db_prefix_i18n;
  if (is_array($db_prefix_i18n)) {
    $db_prefix = array_merge($db_prefix, str_replace('**', $lang, $db_prefix_i18n));
  }
}

/**
 * To get the original path. 
 * Cannot use $_GET["q"] cause it may have been already changed
 */
function _i18n_get_original_path() {
  return isset($_REQUEST["q"]) ? trim($_REQUEST["q"],"/") : '';
}

/**
 * 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;
}



/**
 * Language dependent front page
 * This function will search for aliases like 'en/home', 'es/home'...
 */
function i18n_frontpage($lang = NULL) {
  $lang = $lang ? $lang : i18n_get_lang();
  return i18n_get_normal_path($lang.'/'.variable_get('site_frontpage','node'));
}


/**
 * @defgroup i18n_api Extended language API
 * @{
 * This is an extended language API to be used by modules in i18n package.
 */

/**
 * Returns language lists
 */
function i18n_language_list($type = 'enabled', $field = 'name') {
  switch ($type) {
    case 'enabled':
      return locale_language_list($field);
    case 'extended':
      $enabled = locale_language_list($field);
      $defined = locale_language_list($field, TRUE);
      return array_diff_assoc($defined, $enabled);
  }
}
/**
 * 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);
}

/**
 * Set a persistent language dependent variable.
 *
 * @param $name
 *   The name of the variable to set.
 * @param $value
 *   The value to set. This can be any PHP data type; these functions take care
 *   of serialization as necessary.
 * @param $langcode
 *   Language code
 */
function i18n_variable_set($name, $value, $langcode) {
  global $conf, $i18n_conf;

  db_lock_table('i18n_variable');
  db_query("DELETE FROM {i18n_variable} WHERE name = '%s' AND language='%s'", $name, $langcode);
  db_query("INSERT INTO {i18n_variable} (name, language, value) VALUES ('%s', '%s', '%s')", $name, $langcode, serialize($value));
  db_unlock_tables();

  cache_clear_all('variables:'.$langcode, 'cache');

  $conf[$name] = $value;
  $i18n_conf[$name] = $value;
}

/**
 * Unset a persistent multilingual variable.
 *
 * @param $name
 *   The name of the variable to undefine.
 * @param $langcode
 *   Language code
 */
function i18n_variable_del($name, $langcode) {
  global $conf, $i18n_conf;

  db_query("DELETE FROM {i18n_variable} WHERE name = '%s' AND language='%s'", $name, $langcode);
  cache_clear_all('variables:'.$langcode, 'cache');

  unset($conf[$name]);
  unset($i18n_conf[$name]);
}

/**
 * Utility. Get part of array variable
 */
function i18n_array_variable_get($name, $element, $default = NULL) {
  if (($values = variable_get($name, array())) && isset($values[$element])) {
    return $values[$element];
  } else {
    return $default;
  }  
}

/**
 * Utility. Set part of array variable
 */
function i18n_array_variable_set($name, $element, $value) {
  $values = variable_get($name, array());
  $values[$element] = $value;
  variable_set($name, $values);
}

/**
 * @} End of "defgroup i18n_api".
 */

 // List of language support modes for content
function _i18n_content_language_options() {
  return array(
    LANGUAGE_SUPPORT_NORMAL => t('Normal - All enabled languages will be allowed.'),
    LANGUAGE_SUPPORT_EXTENDED => t('Extended - All defined languages will be allowed.')
  );
}