views_plugin_localization_core.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Contains the Drupal core localization plugin.
  5. */
  6. /**
  7. * Localization plugin to pass translatable strings through t().
  8. *
  9. * @ingroup views_localization_plugins
  10. */
  11. class views_plugin_localization_core extends views_plugin_localization {
  12. /**
  13. * Translate a string.
  14. *
  15. * @param $string
  16. * The string to be translated.
  17. * @param $keys
  18. * An array of keys to identify the string. Generally constructed from
  19. * view name, display_id, and a property, e.g., 'header'.
  20. * @param $format
  21. * The input format of the string. This is optional.
  22. */
  23. function translate_string($string, $keys = array(), $format = '') {
  24. return t($string);
  25. }
  26. /**
  27. * Save a string for translation.
  28. *
  29. * @param $string
  30. * The string to be translated.
  31. * @param $keys
  32. * An array of keys to identify the string. Generally constructed from
  33. * view name, display_id, and a property, e.g., 'header'.
  34. * @param $format
  35. * The input format of the string. This is optional.
  36. */
  37. function save_string($string, $keys = array(), $format = '') {
  38. global $language;
  39. // If the current language is 'en', we need to reset the language
  40. // in order to trigger an update.
  41. // TODO: add test for number of languages.
  42. if ($language->language == 'en') {
  43. $changed = TRUE;
  44. $languages = language_list();
  45. $cached_language = $language;
  46. unset($languages['en']);
  47. if (!empty($languages)) {
  48. $language = current($languages);
  49. }
  50. }
  51. t($string);
  52. if (isset($cached_language)) {
  53. $language = $cached_language;
  54. }
  55. return TRUE;
  56. }
  57. /**
  58. * Delete a string.
  59. *
  60. * Deletion is not supported.
  61. *
  62. * @param $source
  63. * Full data for the string to be translated.
  64. */
  65. function delete($source) {
  66. return FALSE;
  67. }
  68. /**
  69. * Collect strings to be exported to code.
  70. *
  71. * String identifiers are not supported so strings are anonymously in an array.
  72. *
  73. * @param $source
  74. * Full data for the string to be translated.
  75. */
  76. function export($source) {
  77. if (!empty($source['value'])) {
  78. $this->export_strings[] = $source['value'];
  79. }
  80. }
  81. /**
  82. * Render any collected exported strings to code.
  83. *
  84. * @param $indent
  85. * An optional indentation for prettifying nested code.
  86. */
  87. function export_render($indent = ' ') {
  88. $output = '';
  89. if (!empty($this->export_strings)) {
  90. $this->export_strings = array_unique($this->export_strings);
  91. $output = $indent . '$translatables[\'' . $this->view->name . '\'] = array(' . "\n";
  92. foreach ($this->export_strings as $string) {
  93. $output .= $indent . " t('" . str_replace("'", "\'", $string) . "'),\n";
  94. }
  95. $output .= $indent . ");\n";
  96. }
  97. return $output;
  98. }
  99. }