i18n_variable.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @file
  4. * Internationalization (i18n) package. Multilingual variables API.
  5. */
  6. /**
  7. * The type of language used for variables.
  8. */
  9. define('I18N_VARIABLE_LANGUAGE_TYPE', 'i18n_language_variable');
  10. /**
  11. * Implements hook_language_init()
  12. */
  13. function i18n_variable_language_init() {
  14. if (drupal_multilingual()) {
  15. module_invoke('variable_realm', 'initialize', 'language');
  16. }
  17. }
  18. /**
  19. * Implements hook_variable_realm_info().
  20. */
  21. function i18n_variable_variable_realm_info() {
  22. $realm['language'] = array(
  23. 'title' => t('Language'),
  24. 'weight' => 100,
  25. 'controller class' => 'I18nVariableLanguageRealm',
  26. 'store class' => 'VariableStoreRealmStore',
  27. // Variables for this realm can be selected from a list.
  28. 'select' => TRUE,
  29. 'select path' => 'admin/config/regional/i18n/variable',
  30. // Name for variables that belong to this realm: 'multilingual' variable/s
  31. 'variable name' => t('multilingual'),
  32. 'variable class' => 'i18n-variable',
  33. // Automatically handle these variables in system settings forms.
  34. 'form settings' => TRUE,
  35. 'form switcher' => TRUE,
  36. );
  37. return $realm;
  38. }
  39. /**
  40. * Implements hook_menu()
  41. */
  42. function i18n_variable_menu() {
  43. $items['admin/config/regional/i18n/variable'] = array(
  44. 'title' => 'Variables',
  45. 'description' => 'Configure multilingual variables.',
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('variable_realm_select_variables_form', 'language'),
  48. 'access arguments' => array('administer site configuration'),
  49. 'file' => 'variable_realm.form.inc',
  50. 'file path' => drupal_get_path('module', 'variable_realm'),
  51. 'type' => MENU_LOCAL_TASK,
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Get variables language, make sure it is initialized
  57. */
  58. function i18n_variable_language() {
  59. // If we've got a variables language, it will be that one
  60. if (!isset($GLOBALS[I18N_VARIABLE_LANGUAGE_TYPE])) {
  61. $GLOBALS[I18N_VARIABLE_LANGUAGE_TYPE] = i18n_language_interface();
  62. }
  63. return $GLOBALS[I18N_VARIABLE_LANGUAGE_TYPE];
  64. }
  65. /**
  66. * Get original value for global variable/s
  67. */
  68. function i18n_variable_global($name = NULL, $default = NULL) {
  69. return variable_realm_global_get($name, $default);
  70. }
  71. /**
  72. * Get list of multilingual variables or check whether a variable is multilingual
  73. */
  74. function i18n_variable_list($name = NULL) {
  75. $variables = &drupal_static(__FUNCTION__);
  76. if (!isset($variables)) {
  77. $variables = variable_children(variable_get('variable_realm_list_language', array()));
  78. }
  79. return $name ? in_array($name, $variables) : $variables;
  80. }
  81. /**
  82. * Load language variables into array.
  83. *
  84. * Pull variables from the store but filter out the ones that are not multilingual.
  85. */
  86. function i18n_variable_load($langcode) {
  87. $variables = array();
  88. foreach (variable_store('language', $langcode) as $name => $value) {
  89. if (i18n_variable_list($name)) {
  90. $variables[$name] = $value;
  91. }
  92. }
  93. return $variables;
  94. }
  95. /**
  96. * Set a persistent language dependent variable.
  97. *
  98. * @param $name
  99. * The name of the variable to set.
  100. * @param $value
  101. * The value to set. This can be any PHP data type; these functions take care
  102. * of serialization as necessary.
  103. * @param $langcode
  104. * Language code.
  105. */
  106. function i18n_variable_set($name, $value, $langcode) {
  107. variable_realm_set('language', $langcode, $name, $value);
  108. }
  109. /**
  110. * Get single multilingual variable
  111. */
  112. function i18n_variable_get($name, $langcode, $default = NULL) {
  113. return variable_realm_get('language', $langcode, $name, $default);
  114. }
  115. /**
  116. * Unset a persistent multilingual variable.
  117. *
  118. * @param $name
  119. * The name of the variable to undefine.
  120. * @param $langcode
  121. * Language code.
  122. */
  123. function i18n_variable_del($name, $langcode) {
  124. variable_realm_del('language', $langcode, $name);
  125. }