PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/controllers/single_page/dashboard/system/multilingual/translate_interface.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 237 lines | 210 code | 17 blank | 10 comment | 41 complexity | ff8791e98c05c956143e690041c42d87 MD5 | raw file
  1. <?php
  2. namespace Concrete\Controller\SinglePage\Dashboard\System\Multilingual;
  3. use Concrete\Core\Application\EditResponse;
  4. use Concrete\Core\Multilingual\Page\Section\Section;
  5. use Concrete\Core\Page\Controller\DashboardPageController;
  6. use Concrete\Core\Multilingual\Page\Section\Translation;
  7. use Core;
  8. use Database;
  9. use Config;
  10. defined('C5_EXECUTE') or die("Access Denied.");
  11. class TranslateInterface extends DashboardPageController
  12. {
  13. public $helpers = array('form');
  14. public function view()
  15. {
  16. $extractor = Core::make('multilingual/extractor');
  17. $this->set('extractor', $extractor);
  18. $this->set('app', $this->app);
  19. }
  20. public function reloaded()
  21. {
  22. $this->set('message', t('Languages refreshed.'));
  23. $this->view();
  24. }
  25. public function exported()
  26. {
  27. $this->set('message', t('The translations have been exported to file and will be used by the website.'));
  28. $this->view();
  29. }
  30. public function reset_complete()
  31. {
  32. $this->set('message', t('Languages reset.'));
  33. $this->view();
  34. }
  35. public function reset_languages()
  36. {
  37. if (Core::make('token')->validate('reset_languages')) {
  38. $u = new \User();
  39. $extractor = Core::make('multilingual/extractor');
  40. if ($u->isSuperUser()) {
  41. $list = Section::getList();
  42. foreach ($list as $section) {
  43. // now we load the translations that currently exist for each section
  44. $extractor->deleteSectionTranslationFile($section);
  45. }
  46. $extractor->clearTranslationsFromDatabase();
  47. $this->redirect('/dashboard/system/multilingual/translate_interface', 'reset_complete');
  48. }
  49. } else {
  50. $this->error->add(t('Only the admin user may reset all strings.'));
  51. }
  52. }
  53. public function submit()
  54. {
  55. $extractor = Core::make('multilingual/extractor');
  56. if ($this->post('action') == 'reload') {
  57. if (Core::make('token')->validate()) {
  58. // First, we look in all the site sources for PHP code with GetText
  59. $baseTranslations = $extractor->extractTranslatableSiteStrings();
  60. // $translations contains all of our site translations.
  61. $list = Section::getList();
  62. $defaultSourceLocale = Config::get('concrete.multilingual.default_source_locale');
  63. foreach ($list as $section) {
  64. /* @var $section \Concrete\Core\Multilingual\Page\Section\Section */
  65. if ($section->getLocale() != $defaultSourceLocale) {
  66. $localeTranslations = clone $baseTranslations;
  67. /* @var $localeTranslations \Gettext\Translations */
  68. $localeTranslations->setLanguage($section->getLocale());
  69. $localeTranslations->setPluralForms($section->getNumberOfPluralForms(), $section->getPluralsRule());
  70. // now we load the translations that currently exist for each section
  71. $extractor->mergeTranslationsWithSectionFile($section, $localeTranslations);
  72. $extractor->mergeTranslationsWithCore($section, $localeTranslations);
  73. $extractor->mergeTranslationsWithPackages($section, $localeTranslations);
  74. $extractor->saveSectionTranslationsToFile($section, $localeTranslations);
  75. // now that we've updated the translation file, we take all the translations and
  76. // we insert them into the database so we can update our counts, and give the users
  77. // a web interface
  78. $extractor->saveSectionTranslationsToDatabase($section, $localeTranslations);
  79. }
  80. }
  81. $this->redirect('/dashboard/system/multilingual/translate_interface', 'reloaded');
  82. } else {
  83. $this->error->add(Core::make('token')->getErrorMessage());
  84. }
  85. }
  86. if ($this->post('action') == 'export') {
  87. if (Core::make('token')->validate()) {
  88. $defaultSourceLocale = Config::get('concrete.multilingual.default_source_locale');
  89. $list = Section::getList();
  90. foreach ($list as $section) {
  91. if ($section->getLocale() != $defaultSourceLocale) {
  92. $translations = $section->getSectionInterfaceTranslations();
  93. $extractor->mergeTranslationsWithSectionFile($section, $translations);
  94. $extractor->saveSectionTranslationsToFile($section, $translations);
  95. }
  96. }
  97. \Localization::clearCache();
  98. $this->redirect('/dashboard/system/multilingual/translate_interface', 'exported');
  99. } else {
  100. $this->error->add(Core::make('token')->getErrorMessage());
  101. }
  102. }
  103. $this->view();
  104. }
  105. public function save_translation()
  106. {
  107. $result = new EditResponse();
  108. try {
  109. $token = $this->app->make('token');
  110. if (!$token->validate('translate/save')) {
  111. throw new \Exception(t('Invalid token, please refresh and try again.'));
  112. }
  113. $translation = null;
  114. $mtID = @intval($this->post('id'));
  115. if ($mtID > 0) {
  116. $translation = Translation::getByRecordID($mtID);
  117. }
  118. if (!isset($translation)) {
  119. throw new \Exception(t('Invalid parameter received: %s', 'id'));
  120. }
  121. $singular = '';
  122. $plurals = array();
  123. if ($this->post('clear') !== '1') {
  124. $translated = $this->post('translated');
  125. if ((!is_array($translated)) || (count($translated) < 1)) {
  126. throw new \Exception(t('Invalid parameter received: %s', 'translated'));
  127. }
  128. if ($translation->hasPlural()) {
  129. $singular = array_shift($translated);
  130. $plurals = $translated;
  131. } else {
  132. if (count($translated) !== 1) {
  133. throw new \Exception(t('Invalid parameter received: %s', 'translated'));
  134. }
  135. $singular = $translated[0];
  136. }
  137. }
  138. $translation->updateTranslation($singular, $plurals);
  139. } catch (\Exception $x) {
  140. $result->setError($x);
  141. }
  142. $result->outputJSON();
  143. }
  144. public function export_translations($localeCode)
  145. {
  146. $result = new EditResponse();
  147. try {
  148. if (!Core::make('token')->validate('export_translations')) {
  149. throw new \Exception(Core::make('token')->getErrorMessage());
  150. }
  151. $section = Section::getByLocale($localeCode);
  152. if (is_object($section) && (!$section->isError())) {
  153. if ($section->getLocale() == Config::get('concrete.multilingual.default_source_locale')) {
  154. $section = null;
  155. }
  156. } else {
  157. $section = null;
  158. }
  159. if (!isset($section)) {
  160. throw new \Exception(t('Invalid language identifier'));
  161. }
  162. $translations = $section->getSectionInterfaceTranslations();
  163. $extractor = Core::make('multilingual/extractor');
  164. $extractor->mergeTranslationsWithSectionFile($section, $translations);
  165. $extractor->saveSectionTranslationsToFile($section, $translations);
  166. \Localization::clearCache();
  167. $result->setAdditionalDataAttribute('newToken', Core::make('token')->generate('export_translations'));
  168. $result->message = t('The translations have been exported to file and will be used by the website.');
  169. } catch (\Exception $x) {
  170. $result->setError($x);
  171. }
  172. $result->outputJSON();
  173. }
  174. public function translate_po($mtSectionID = false)
  175. {
  176. $this->view();
  177. $mtSectionID = intval($mtSectionID);
  178. $section = Section::getByID(intval($mtSectionID));
  179. if ($section) {
  180. $translations = $section->getSectionInterfaceTranslations();
  181. $this->requireAsset('core/translator');
  182. $this->set('section', $section);
  183. $jsonTranslations = array();
  184. $numPlurals = $section->getNumberOfPluralForms();
  185. foreach ($translations as $translation) {
  186. /* @var $translation \Concrete\Core\Multilingual\Page\Section\Translation */
  187. $jsonTranslation = array();
  188. $jsonTranslation['id'] = $translation->getRecordID();
  189. if ($translation->hasContext()) {
  190. $jsonTranslation['context'] = $translation->getContext();
  191. }
  192. $jsonTranslation['original'] = $translation->getOriginal();
  193. if ($translation->hasPlural()) {
  194. $jsonTranslation['originalPlural'] = $translation->getPlural();
  195. }
  196. if ($translation->hasTranslation()) {
  197. $t = array($translation->getTranslation());
  198. if (($numPlurals > 1) && $translation->hasPlural()) {
  199. $t = array_merge($t, $translation->getPluralTranslation());
  200. }
  201. $jsonTranslation['translations'] = $t;
  202. }
  203. $extractedComments = '';
  204. if ($translation->hasExtractedComments()) {
  205. $extractedComments = trim(implode("\n", $translation->getExtractedComments()));
  206. }
  207. if ($extractedComments !== '') {
  208. $jsonTranslation['comments'] = $extractedComments;
  209. }
  210. if ($translation->hasReferences()) {
  211. $jsonTranslation['references'] = $translation->getReferences();
  212. }
  213. $jsonTranslations[] = $jsonTranslation;
  214. }
  215. $this->set('translations', $jsonTranslations);
  216. } else {
  217. $this->redirect('/dashboard/system/multilingual/translate_interface');
  218. }
  219. }
  220. }