Projet

Général

Profil

Paste
Télécharger (2,85 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.i18n.inc @ 9faa5de0

1
<?php
2
/**
3
 * @file
4
 * Implementation of i18n hooks
5
 */
6

    
7
/**
8
 * Implements hook_i18n_string_objects().
9
 * 
10
 * Automate object list for object types that have a 'table' property
11
 */
12
function i18n_string_i18n_string_objects($type) {
13
  if ($function = i18n_object_info($type, 'list callback')) {
14
    return call_user_func($function);
15
  }
16
  elseif ($table = i18n_string_object_info($type, 'table')) {
17
    $query = db_select($table, 's')->fields('s');
18
    return $query->execute()->fetchAll();
19
  }
20
}
21

    
22
/**
23
 * Implements hook_i18n_string_list().
24
 * 
25
 * Collect all strings from objects of this group.
26
 */
27
function i18n_string_i18n_string_list($group) {
28
  $strings = array();
29
  // It may be for one group or all groups
30
  $groups = $group == 'all' ? array_keys(i18n_string_group_info()) : array($group);
31
  foreach ($groups as $group) {  
32
    // Compile strings for object types for this group
33
    foreach (i18n_string_group_object_types($group) as $type) {
34
      $type_strings = i18n_string_object_type_string_list($type);
35
      if ($type_strings && !empty($type_strings[$group])) {
36
        $strings[$group] = isset($strings[$group]) ? i18n_string_array_merge($strings[$group], $type_strings[$group]) : $type_strings[$group];
37
      }
38
    }
39
  }
40
  return $strings;
41
}
42

    
43
/**
44
 * Get object types for text group
45
 */
46
function i18n_string_group_object_types($group) {
47
  $types = array();
48
  foreach (i18n_object_info() as $type => $type_info) {
49
    if (!empty($type_info['string translation']) && $type_info['string translation']['textgroup'] == $group) {
50
      $types[] = $type;
51
    }
52
  }
53
  return $types;
54
}
55

    
56
/**
57
 * Get object string list that are in this text group.
58
 * 
59
 * @param $type
60
 *   Object type
61
 */
62
function i18n_string_object_type_string_list($type) {
63
  $strings = array();
64
  if ($objects = module_invoke_all('i18n_string_objects', $type)) {
65
    foreach ($objects as $object) {
66
      if ($object_strings = i18n_object($type, $object)->get_properties()) {
67
        $strings = i18n_string_array_merge($strings, $object_strings);
68
      }
69
    }
70
  }
71
  return $strings;
72
}
73

    
74
/**
75
 * Merges multiple arrays, recursively, and returns the merged array.
76
 *
77
 * This function is not equivalent to PHP's array_merge_recursive(),
78
 * as this version leaves integer keys intact.
79
 *
80
 * @see drupal_array_merge_deep(), @see array_merge_recursive()
81
 *
82
 * @param ...
83
 *   Arrays to merge.
84
 * @return
85
 *   The merged array.
86
 */
87
function i18n_string_array_merge() {
88
  $arrays = func_get_args();
89
  $result = array();
90

    
91
  foreach ($arrays as $array) {
92
    foreach ($array as $key => $value) {
93
      // Recurse when both values are arrays.
94
      if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
95
        $result[$key] = i18n_string_array_merge($result[$key], $value);
96
      }
97
      // Otherwise, use the latter value, overriding any previous value.
98
      else {
99
        $result[$key] = $value;
100
      }
101
    }
102
  }
103

    
104
  return $result;
105
}