summaryrefslogtreecommitdiffstats
path: root/i18n.inc
blob: 063730fb098cef58112359d23139e601ff47ac5f (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
<?php
// $Id$

/**
 * Internationalization (i18n) module
 *
 * These are the functions to be defined only when module is enabled.
 * Otherwise they could mess up with the admin system
 *
 * @author Jose A. Reyero, 2004
 *
 */

/**
 * Implementation of conf_url_rewrite
 * 
 * This is a conditional definition, just in case it is defined somewhere else
 */

if(!function_exists('custom_url_rewrite')) {
  function custom_url_rewrite($type, $path, $original) {
    return i18n_url_rewrite($type, $path, $original);
  }
}  

function i18n_url_rewrite($type, $path, $original){
  if ($type == 'alias' && !i18n_get_lang_prefix($path) ){
    return $path ? i18n_get_lang() . '/'. $path : i18n_get_lang();
  } else {
    return $path;
  } 
} 

/**
 * Implementation of hook_db_rewrite_sql()
 */
function i18n_db_rewrite_sql($query, $primary_table, $primary_key){
  // Some exceptions for query rewrites
  if(strstr(request_uri(),'cron.php')) return;

  $alias = $primary_table;

  switch ($alias) {
    case 'n':
      $result = i18n_db_node_rewrite($query, $primary_table, $primary_key);
      break;
    case 't':
    case 'v':
      $result = i18n_db_taxonomy_rewrite($query, $primary_table, $primary_key);
      break;
  }

  return $result;
}

function i18n_db_node_rewrite($query, $primary_table, $primary_key){
  $alias = $primary_table;
  // When loading specific nodes, language conditions shouldn't apply
  // TO-DO: Refine this regexp  
  if (preg_match("/WHERE.*$alias.nid\s*=\s*(\d|%d)/", $query)) return;
  //drupal_set_message($query);
  $mode = i18n_selection_mode();
  if($mode == 'off') return;
  
  $result['join'] = "LEFT JOIN {i18n_node} i18n ON $alias.nid = i18n.nid";
  $alias = 'i18n';

  switch($mode){
    case 'simple':
      $result['where'] = "$alias.language ='".i18n_get_lang()."' OR $alias.language IS NULL" ;
      break;
    case 'mixed':
      $result['where'] = "$alias.language ='".i18n_get_lang()."' OR $alias.language ='".i18n_default_language()."' OR $alias.language IS NULL" ;
      break;
    case 'strict':
      $result['where'] = "$alias.language ='".i18n_get_lang()."'" ;
      break;
    case 'default':
      $result['where'] = "$alias.language ='".i18n_default_language()."' OR $alias.language IS NULL" ;
      break;
  }
  
  return $result;
}

function i18n_db_taxonomy_rewrite($query, $primary_table, $primary_key){
  // When loading specific terms, vocabs, language conditions shouldn't apply
  // TO-DO: Refine this regexp  
  if (preg_match("/WHERE.* $primary_table\.tid\s*(=\s*\d|IN)/", $query)) return;
  
  // Decide language depending on what we are doing
  $lang = i18n_get_lang(); // Default
  switch(arg(0)) {
    case 'node':
	    $edit = $_POST['edit'];
	    if( arg(1) == 'add'){
	      // Adding node
	      $lang = isset($edit['language']) ? $edit['language'] : i18n_get_lang();
	    } elseif( is_numeric(arg(1)) && arg(2) == 'edit') {
	      // Editing a node
	      if(is_array($edit)) {
	        $lang = isset($edit['language']) ? $edit['language'] : i18n_get_lang();
	      } else {
	        $lang = i18n_node_get_lang(arg(1), i18n_get_lang());
	      }
	    } else {
	      $lang = i18n_get_lang();
	    }
	    break;
    case 'translation':
      $edit = $_POST['edit'];
      $lang = isset($edit['language']) ? $edit['language'] : arg(3);
      break;
    case 'admin': 
		  if(arg(1) == 'taxonomy') {
		    // Taxonomy administration. Show all
		    $lang = NULL;
		  }
    /*
    case 'taxonomy':
      if(arg(1) == 'term'){
        $lang = NULL;
      }
      */
  }
  
  if($lang) {
    $alias = $primary_table;
    $result['where'] = "$alias.language ='$lang' OR $alias.language = ''";
    return $result;
  }
}

/**
 * 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
 * Alan: - changed to test in case translation_form_alter (or another module/mechanism) has already set language
 *       - translation module may reduce language selection options in case there already exist translations
 */
function i18n_form_alter($form_id, &$form) {
  //drupal_set_message("i18n_form_alter form_id=$form_id ");
  switch($form_id){
    case 'taxonomy_overview_vocabularies':
      $vocabularies = taxonomy_get_vocabularies();
      $languages = i18n_supported_languages();
      foreach ($vocabularies as $vocabulary) {
        if($vocabulary->language) $form[$vocabulary->vid]['type']['#value'] = $form[$vocabulary->vid]['type']['#value'].'&nbsp('.$languages[$vocabulary->language].')';
      }
    	break;
    case 'taxonomy_form_vocabulary': // Taxonomy vocabulary
      if(isset($form['vid'])) {
        $vocabulary = taxonomy_get_vocabulary($form['vid']['#value']);
      } 
      $form['language'] = _i18n_language_select(isset($vocabulary) ? $vocabulary->language : i18n_get_lang(),t('This language will be set for all terms in this vocabulary')); 
      break;
      
    case 'taxonomy_form_term': // Taxonomy term
      if(isset($form['vid']) && is_numeric($form['vid'])) {
        $vocabulary = taxonomy_get_vocabulary($form['vid']);
      } 
	  $form['language'] = _i18n_language_select(isset($vocabulary) ? $vocabulary->language : i18n_get_lang());
      break;
      
    default:
	    // Content type settings
		  if (isset($form['type']) && $form['type']['#value'] .'_node_settings' == $form_id) {
		    $form['workflow']['i18n_node_'. $form['type']['#value']] = array(
		      '#type' => 'radios',
		      '#title' => t('Multilingual support'),
		      '#default_value' => variable_get('i18n_node_'. $form['type']['#value'], 0),
		      '#options' => array(t('Disabled'), t('Enabled')),
		      '#description' => t('Enables language field and multilingual support for this content type.'),
		    );
		  }
	    // Node edit form
		  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id){
	      // Language field
	      if(variable_get('i18n_node_'.$form['type']['#value'], 0) && !isset($form['i18n']['language'])) {
          // Language field
          $form['i18n'] = array('#type' => 'fieldset', '#title' => t('Language'), '#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);
	      }
	      // 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($form['#node']->path) {
	        $alias=drupal_lookup_path('alias', 'node/'.$form['#node']->nid);
	        if($alias && $alias != 'node/'.$form['#node']->nid){
	          $form['#node']->path = $alias;
	        } else {
	          unset($form['#node']->path);
	        }
	      }
	  }
    
  }    
}

/**
 * Implementation of hook_nodeapi
 * Updated for new table i18n_node
 */
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 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){
          db_query("INSERT INTO {i18n_node} (nid, trid, language) VALUES(%d, '%d', '%s')", $node->nid, $node->trid, $node->language);
        }
        break;
      case 'delete': 
        db_query('DELETE FROM {i18n_node} WHERE nid=%d', $node->nid);
        break;
	  }
  }   
}

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

/**
 * Implementation of hook_taxonomy
 * 
 * $edit parameter may be an array or an object !!
 */
function i18n_taxonomy($op, $type, $edit = NULL) {
  $edit = (array)$edit;
  switch ("$type/$op") {
    case 'term/insert':
    case 'term/update':
	    $language = isset($edit['language']) ? $edit['language'] : '';
      db_query("UPDATE {term_data} SET language='%s' WHERE tid=%d", $language, $edit['tid']);    
      break;
    case 'vocabulary/insert':    
    case 'vocabulary/update':
	    $language = isset($edit['language']) ? $edit['language'] : '';
	    db_query("UPDATE {vocabulary} SET language='%s' WHERE vid=%d", $language, $edit['vid']);    
	    if ($language && $op == 'update') {
	      db_query("UPDATE {term_data} t SET t.language='%s' WHERE t.vid=%d", $edit['language'], $edit['vid']);
	      drupal_set_message(t('Reset language for all terms.'));
	    }
	    break;
  }
}

/**
 * More i18n API
 */

/**
 * This one expects to be called first from common.inc
 */
function i18n_get_lang() {
  static $i18n_language;
  //see if the language is already set.
  if ($i18n_language) {
    return $i18n_language;
  } else {
    return $i18n_language = _i18n_get_lang();
  }
}

// Note: do not call url() anymore, it will be called from l()
function i18n_url($url, $lang) {
  // Strip prefix if default language
  if ($url) {
    return $lang.'/'.$url;
  } else {
    return $lang;
  }
}

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

/**
 * Returns main language, two letter code
 */
function i18n_get_main_lang($lang = NULL){
  $lang = $lang ? $lang : i18n_get_lang();
  return substr($lang, 2);
}


/**
 * Language block
 * 
 * This is a simple language switcher which knows nothing about translations
 */
function i18n_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Language switcher');
  }
  elseif($op == 'view') {
    $blocks['subject'] = t('Languages');
    $blocks['content'] = theme('item_list', i18n_get_links($_GET['q']));
  }

  return $blocks;
}

/**
 * Function i18n_get_links
 * 
 * Returns an array of urls for all languages, with or without names/flags
 */
function i18n_get_links($path = '') {
  foreach(i18n_supported_languages() as $lang => $name){
    $links[$lang]= theme('i18n_link', $name, i18n_url($path, $lang), $lang);
  }
  return $links;  
}


/**
 * Multilingual variables 
 */
function i18n_variable_init(){
  global $conf;
  global $i18n_conf;
  $lang = _i18n_get_lang();
  if($i18n_variables = variable_get('i18n_variables', '')){
    $i18n_conf = array();
    $variables = _i18n_variable_init();
    foreach($i18n_variables as $name){
      $i18n_conf[$name] = isset($variables[$name]) ? $variables[$name] : (isset($conf[$name]) ? $conf[$name] : '');
    }
    $conf = array_merge($conf, $i18n_conf);
  }
}

function _i18n_variable_init(){  
  $lang = _i18n_get_lang();
  $variables = array();
  if ($cached = cache_get('variables:'.$lang)) {
    $variables = unserialize($cached->data);
  }
  else {
    $result = db_query("SELECT * FROM {i18n_variable} WHERE language='%s'", $lang);
    while ($variable = db_fetch_object($result)) {
      $variables[$variable->name] = unserialize($variable->value);
    }
    cache_set('variables:'.$lang, serialize($variables));
  }

  return $variables;    

}

function _i18n_variable_exit(){
  global $i18n_conf;
  global $conf;
  $lang = _i18n_get_lang();
  if($i18n_conf){
    $variables = array_diff_assoc($i18n_conf, $conf);
    if($variables){
      foreach($variables as $name => $value){
        $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]));
      }
      cache_set('variables:'.$lang, serialize($i18n_conf));
    }
  }
}
 
/**
 * @name Themeable functions
 * @{
 */
  
/**
 * Produces a language link with the right flag
 */
function theme_i18n_link($text, $target, $lang, $separator='&nbsp;'){
  $output = '<span class="i18n-link">';
  $attributes = ($lang == i18n_get_lang()) ? array('class' => 'active') : NULL;
  $output .= l(theme('i18n_language_icon', $lang), $target, $attributes, NULL, NULL, FALSE, TRUE);
  $output .= $separator;
  $output .= l($text, $target, $attributes, NULL, NULL, FALSE, TRUE);
  $output .= '</span>';
  return $output;
}
 
function theme_i18n_language_icon($lang){
  if ($path = variable_get('i18n_icon_path', drupal_get_path('module', 'i18n').'/flags/*.png')) {
    $languages = i18n_supported_languages();
    $src = base_path().str_replace('*', $lang, $path);
    list($width, $height) = explode('x', variable_get('i18n_flags_size', '16x12'));
    $attribs = array('class' => 'i18n-icon', 'width' => $width, 'height' => $height, 'alt' => $languages[$lang]);
    return "<img src=\"$src\" ".drupal_attributes($attribs)." />";
  }  
}

/* @} */
?>