features.locale.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @file
  4. * Features hooks for language.
  5. */
  6. /**
  7. * Implements of hook_features_api().
  8. */
  9. function locale_features_api() {
  10. return array(
  11. 'language' => array(
  12. 'name' => t('Languages'),
  13. 'default_hook' => 'locale_default_languages',
  14. 'feature_source' => TRUE,
  15. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_features_export_options().
  21. */
  22. function language_features_export_options() {
  23. return locale_language_list('native', TRUE);
  24. }
  25. /**
  26. * Implements hook_features_export().
  27. */
  28. function language_features_export($data, &$export, $module_name = '') {
  29. $export['dependencies']['features'] = 'features';
  30. $export['dependencies']['locale'] = 'locale';
  31. $language_list = locale_language_list('native', TRUE);
  32. foreach ($data as $name) {
  33. // Only export existing languages.
  34. if (!empty($language_list[$name])) {
  35. // Add language to exports.
  36. $export['features']['language'][$name] = $name;
  37. }
  38. }
  39. // No pipe to return.
  40. return array();
  41. }
  42. /**
  43. * Implements hook_features_export_render().
  44. */
  45. function language_features_export_render($module, $data, $export = NULL) {
  46. $code = array();
  47. $code[] = ' $languages = array();';
  48. $code[] = '';
  49. $language_list = language_list();
  50. foreach ($data as $name) {
  51. // Only render existing languages.
  52. if (!empty($language_list[$name])) {
  53. $var = (array) $language_list[$name];
  54. // Unset javascript hash
  55. unset($var['javascript']);
  56. $lang_export = features_var_export($var, ' ');
  57. $lang_identifier = features_var_export($name);
  58. $code[] = " // Exported language: $name.";
  59. $code[] = " \$languages[{$lang_identifier}] = {$lang_export};";
  60. }
  61. }
  62. $code[] = ' return $languages;';
  63. $code = implode("\n", $code);
  64. return array('locale_default_languages' => $code);
  65. }
  66. /**
  67. * Implements hook_features_revert().
  68. */
  69. function language_features_revert($module) {
  70. return language_features_rebuild($module);
  71. }
  72. /**
  73. * Implements hook_features_rebuild().
  74. */
  75. function language_features_rebuild($module) {
  76. if ($defaults = features_get_default('language', $module)) {
  77. foreach ($defaults as $key => $language) {
  78. _features_language_save((object) $language);
  79. }
  80. // Set correct language count.
  81. $enabled_languages = db_select('languages')
  82. ->condition('enabled', 1)
  83. ->fields('languages')
  84. ->execute()
  85. ->rowCount();
  86. variable_set('language_count', $enabled_languages);
  87. }
  88. }
  89. /**
  90. * Helper function to save the language to database.
  91. *
  92. * @see locale_languages_edit_form_submit()
  93. */
  94. function _features_language_save($language) {
  95. $current_language = db_select('languages')
  96. ->condition('language', $language->language)
  97. ->fields('languages')
  98. ->execute()
  99. ->fetchAssoc();
  100. // Set the default language when needed.
  101. $default = language_default();
  102. // Insert new language via api function.
  103. if (empty($current_language)) {
  104. locale_add_language($language->language,
  105. $language->name,
  106. $language->native,
  107. $language->direction,
  108. $language->domain,
  109. $language->prefix,
  110. $language->enabled,
  111. ($language->language == $default->language));
  112. // Additional params, locale_add_language does not implement.
  113. db_update('languages')
  114. ->fields(array(
  115. 'plurals' => empty($language->plurals) ? 0 : $language->plurals,
  116. 'formula' => empty($language->formula) ? '' : $language->formula,
  117. ))
  118. ->condition('language', $language->language)
  119. ->execute();
  120. }
  121. // Update Existing language.
  122. else {
  123. // @TODO: get properties from schema.
  124. $properties = array('language', 'name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight', 'javascript');
  125. // The javascript hash is not in the imported data but should be empty
  126. if (!isset($language->javascript)) {
  127. $language->javascript = '';
  128. }
  129. $fields = array_intersect_key((array) $language, array_flip($properties));
  130. db_update('languages')
  131. ->fields($fields)
  132. ->condition('language', $language->language)
  133. ->execute();
  134. // Set the default language when needed.
  135. $default = language_default();
  136. if ($default->language == $language->language) {
  137. variable_set('language_default', (object) $fields);
  138. }
  139. }
  140. }