tmgmt_i18n_string.plugin.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Provides the i18n string source controller.
  5. */
  6. /**
  7. * Translation plugin controller for i18n strings.
  8. */
  9. class TMGMTI18nStringSourcePluginController extends TMGMTDefaultSourcePluginController {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function getData(TMGMTJobItem $job_item) {
  14. $i18n_object = $this->getI18nObjectWrapper($job_item);
  15. $structure = array();
  16. $languages = language_list();
  17. if ($i18n_object instanceof i18n_string_object_wrapper) {
  18. $i18n_strings = $i18n_object->get_strings();
  19. $source_language = $job_item->getJob()->source_language;
  20. foreach ($i18n_strings as $string_id => $string) {
  21. // If the job source language is different from the i18n source language
  22. // try to load an existing translation for the language and use it as
  23. // the source.
  24. if ($source_language != i18n_string_source_language()) {
  25. $translation = $string->get_translation($source_language);
  26. if (empty($translation)) {
  27. throw new TMGMTException(t('Unable to load %language translation for the string %title',
  28. array('%language' => $languages[$source_language]->name, '%title' => $string->title)));
  29. }
  30. // If '#label' is empty theme_tmgmt_ui_translator_review_form() fails.
  31. $structure[$string_id] = array(
  32. '#label' => !empty($string->title) ? $string->title : $string->property,
  33. '#text' => $translation,
  34. '#translate' => TRUE
  35. );
  36. }
  37. else {
  38. // If '#label' is empty theme_tmgmt_ui_translator_review_form() fails.
  39. $structure[$string_id] = array(
  40. '#label' => !empty($string->title) ? $string->title : $string->property,
  41. '#text' => $string->string,
  42. '#translate' => TRUE
  43. );
  44. }
  45. }
  46. }
  47. return $structure;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function saveTranslation(TMGMTJobItem $job_item) {
  53. $job = tmgmt_job_load($job_item->tjid);
  54. $data = array_filter(tmgmt_flatten_data($job_item->getData()), '_tmgmt_filter_data');
  55. foreach ($data as $i18n_string => $item) {
  56. if (isset($item['#translation']['#text'])) {
  57. i18n_string_translation_update($i18n_string, $item['#translation']['#text'], $job->target_language);
  58. }
  59. }
  60. // We just saved the translation, set the state of the job item to
  61. // 'finished'.
  62. $job_item->accepted();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getLabel(TMGMTJobItem $job_item) {
  68. if ($i18n_object = $this->getI18nObjectWrapper($job_item)) {
  69. // Get the label, default to the get_title() method, fall back to the
  70. // first string if that is empty.
  71. $title = t('Unknown');
  72. if ($i18n_object->get_title()) {
  73. $title = $i18n_object->get_title();
  74. }
  75. elseif ($strings = $i18n_object->get_strings(array('empty' => TRUE))) {
  76. $title = reset($strings)->get_string();
  77. }
  78. return t('@title (@id)', array('@title' => strip_tags(drupal_substr($title, 0, 64)), '@id' => $job_item->item_id));
  79. }
  80. return parent::getLabel($job_item);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getUri(TMGMTJobItem $job_item) {
  86. if ($wrapper = $this->getI18nObjectWrapper($job_item)) {
  87. return array(
  88. 'path' => $wrapper->get_path(),
  89. 'options' => array(),
  90. );
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getType(TMGMTJobItem $job_item) {
  97. if ($label = $this->getItemTypeLabel($job_item->item_type)) {
  98. return $label;
  99. }
  100. return parent::getType($job_item);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getSourceLangCode(TMGMTJobItem $job_item) {
  106. return i18n_string_source_language();
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getExistingLangCodes(TMGMTJobItem $job_item) {
  112. $existing_lang_codes = array();
  113. $languages = language_list();
  114. if ($object = $this->getI18nObjectWrapper($job_item)) {
  115. $existing_lang_codes = array_keys($languages);
  116. foreach ($object->load_strings() as $string) {
  117. foreach ($languages as $language) {
  118. if ($language->language == $this->getSourceLangCode($job_item)) {
  119. continue;
  120. }
  121. // Remove languages for which we fail to find translation.
  122. if (in_array($language->language, $existing_lang_codes) && !$string->get_translation($language->language)) {
  123. $existing_lang_codes = array_diff($existing_lang_codes, array($language->language));
  124. }
  125. }
  126. }
  127. }
  128. return $existing_lang_codes;
  129. }
  130. /**
  131. * Helper function to get i18n_object_wrapper for given job item.
  132. *
  133. * @param TMGMTJobItem $job_item
  134. *
  135. * @return i18n_string_object_wrapper
  136. */
  137. protected function getI18nObjectWrapper(TMGMTJobItem $job_item) {
  138. list(, $type, $object_id) = explode(':', $job_item->item_id, 3);
  139. return tmgmt_i18n_string_get_wrapper($job_item->item_type, (object) array('type' => $type, 'objectid' => $object_id));
  140. }
  141. }