root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.api.php @ 01f36513
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* API documentation file for String translation module.
|
6 |
*
|
7 |
* Basically we are collecting translatable strings for each text group. There are two ways a
|
8 |
* module can produce this list of strings. It should be one or the other, not both.
|
9 |
*
|
10 |
* 1. Provide a list of objects that are translatable for that text group either defining a
|
11 |
* 'list callback' for that object type or implementing hook_i18n_string_objects($type) for
|
12 |
* that object type.
|
13 |
*
|
14 |
* 2. Provide a full list of strings for that text group by implementing
|
15 |
* hook_i18n_string_list()
|
16 |
*
|
17 |
* Then we have hook_i18n_string_list_TEXTGROUP_alter() for other modules to alter either the
|
18 |
* list of strings for a single object or the full list of strings at the end.
|
19 |
*/
|
20 |
|
21 |
/**
|
22 |
* List text groups for string translation.
|
23 |
*
|
24 |
* This information will be automatically produced later for hook_locale()
|
25 |
*/
|
26 |
function hook_i18n_string_info() { |
27 |
$groups['menu'] = array( |
28 |
'title' => t('Menu'), |
29 |
'description' => t('Translatable menu items: title and description.'), |
30 |
'format' => FALSE, // This group doesn't have strings with format |
31 |
'list' => TRUE, // This group can list all strings |
32 |
); |
33 |
return $groups; |
34 |
} |
35 |
|
36 |
/**
|
37 |
* Provide list of translatable strings for text group.
|
38 |
|
39 |
* A module can provide either a list of translatable strings using hook_i18n_string_list() or
|
40 |
* it can provide a list of objects using hook_i18n_string_objects() from which the string
|
41 |
* list will be produced automatically. But not both.
|
42 |
*
|
43 |
* @param $group
|
44 |
* Text group name.
|
45 |
*/
|
46 |
function hook_i18n_string_list($group) { |
47 |
if ($group == 'mygroup') { |
48 |
$strings['mygroup']['type1']['key1']['name'] = 'Name of object type1/key1'; |
49 |
$strings['mygroup']['type1']['key1']['description'] = 'Description of object type1/key1'; |
50 |
return $strings; |
51 |
} |
52 |
} |
53 |
|
54 |
/**
|
55 |
* Alter string list for objects of text group.
|
56 |
*
|
57 |
* To build a list of translatable strings for a text group, we'll follow these steps:
|
58 |
* 1. Invoke hook_i18n_string_list($textgroup), that will get us an array of strings
|
59 |
* 2. Get the object types for that textgroup, collectin it from i18n object information.
|
60 |
* @see i18n_string_group_object_types()
|
61 |
* 3. For each object type, collect the full list of objects invoking hook_i18n_string_objects($type)
|
62 |
* @see i18n_string_object_type_string_list()
|
63 |
* If an object defines a 'list callback' function that one will be called to get the list of strings.
|
64 |
* 4. For each object, collect the properties for that specific object.
|
65 |
* $properties = i18n_object($type, $object)->get_properties();
|
66 |
* 5. Run this hook to alter the strings for that specific object. In this case we'll pass the
|
67 |
* $type and $object parameters.
|
68 |
* 6. Merge all strings from all objects in an array indexed by textgroup, type, id, name
|
69 |
* 7. Run this hook once again to alter *all* strings for this textgroup. In this case we
|
70 |
* don't have a $type and $object parameters.
|
71 |
*
|
72 |
* Thus this hook is really invoked once per object and once per textgroup on top of that.
|
73 |
*
|
74 |
* @see i18n_string_group_string_list()
|
75 |
* @see i18n_string_object_type_string_list()
|
76 |
* @see i18n_menu_i18n_string_list_menu_alter()
|
77 |
*
|
78 |
* @param $strings
|
79 |
* Associative array with current string list indexed by textgroup, type, id, name
|
80 |
* @param $type
|
81 |
* Object type ad defined on i18n_object_info()
|
82 |
* @param $object
|
83 |
* Object defined on i18n_object_info()
|
84 |
*
|
85 |
* These last parameters are optional. If type and object are not present
|
86 |
* we are altering the full list of strings for the text group that happens once at the end.
|
87 |
*/
|
88 |
function hook_i18n_string_list_TEXTGROUP_alter(&$strings, $type = NULL, $object = NULL) { |
89 |
if ($type == 'menu_link' && $object) { |
90 |
if (isset($object['options']['attributes']['title'])) { |
91 |
$strings['menu']['item'][$object['mlid']]['title']['string'] = $object['link_title']; |
92 |
$strings['menu']['item'][$object['mlid']]['description']['string'] = $object['options']['attributes']['title']; |
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
/**
|
98 |
* List objects to collect translatable strings.
|
99 |
*
|
100 |
* A module can provide either a list of translatable strings using hook_i18n_string_list() or
|
101 |
* it can provide a list of objects using hook_i18n_string_objects() from which the string
|
102 |
* list will be produced automatically. But not both.
|
103 |
*
|
104 |
* @see i18n_object_info()
|
105 |
* @see i18n_menu_i18n_string_objects()
|
106 |
* @see i18n_string_i18n_string_list()
|
107 |
*
|
108 |
* @param $type string
|
109 |
* Object type
|
110 |
* @return $objects array
|
111 |
* Associative array of objects indexed by object id
|
112 |
*/
|
113 |
function hook_i18n_string_objects($type) { |
114 |
if ($type == 'menu_link') { |
115 |
// All menu items that have no language and are customized.
|
116 |
return db_select('menu_links', 'm') |
117 |
->fields('m')
|
118 |
->condition('language', LANGUAGE_NONE) |
119 |
->condition('customized', 1) |
120 |
->execute() |
121 |
->fetchAllAssoc('mlid', PDO::FETCH_ASSOC); |
122 |
} |
123 |
} |