i18n_string.api.php 4.8 KB

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