PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/translator/translator.php

http://github.com/imagecms/ImageCMS
PHP | 291 lines | 270 code | 14 blank | 7 comment | 9 complexity | 52ef9fe0bf4f403a9e73c6540300ccdd MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. use translator\classes\PoFileManager;
  3. use translator\classes\Replacer;
  4. (defined('BASEPATH')) OR exit('No direct script access allowed');
  5. /**
  6. * Image CMS
  7. * Translator Module
  8. */
  9. class Translator extends MY_Controller
  10. {
  11. public function __construct() {
  12. parent::__construct();
  13. $this->load->helper('translator');
  14. $lang = new MY_Lang();
  15. $lang->load('translator');
  16. }
  17. public function index() {
  18. $this->core->error_404();
  19. }
  20. /**
  21. * @param string $templateName
  22. */
  23. public function replaceKeys($templateName) {
  24. $templatesPath = ' ./templates / ' . $templateName;
  25. $fromLocale = 'en_US';
  26. $pofilePath = $templatesPath . ' / language / ' . $templateName . ' / ' . $fromLocale . ' / LC_MESSAGES / ' . $templateName . ' . po';
  27. $poFile = file($pofilePath);
  28. $result = [];
  29. foreach ($poFile as $line) {
  30. $first2symbols = substr($line, 0, 2);
  31. if ($first2symbols == '#:') {
  32. $links[] = trim(substr($line, 2, -1));
  33. continue;
  34. }
  35. if (substr($line, 0, 5) == 'msgid') {
  36. if (preg_match('/"(.*?)"/', $line, $matches)) {
  37. $origin = $matches[1];
  38. if (!strlen($origin)) {
  39. $origin = 0;
  40. }
  41. }
  42. continue;
  43. }
  44. if (substr($line, 0, 6) == 'msgstr') {
  45. if ($origin) {
  46. preg_match('/"(.*?)"/', $line, $translation);
  47. $translation = $translation[1];
  48. $result[] = [
  49. 'paths' => $links,
  50. 'origin' => $origin,
  51. 'translation' => $translation,
  52. ];
  53. unset($links);
  54. }
  55. }
  56. }
  57. foreach ($result as $key => $value) {
  58. foreach ($value['paths'] as $path) {
  59. $path = preg_replace('/:[\d]+/', '', $path);
  60. $file = file_get_contents($path);
  61. $translation = str_replace("'", '', $value['translation']);
  62. $translation = str_replace('"', '', $translation);
  63. $file = preg_replace('/(?<!\w)lang\([\"]{1}' . preg_quote($value['origin']) . '[\"]{1}/', "lang('" . $translation . "'", $file);
  64. $file = preg_replace("/(?<!\w)lang\([']{1}" . preg_quote($value['origin']) . "[']{1}/", "lang('" . $translation . "'", $file);
  65. file_put_contents($path, $file);
  66. }
  67. }
  68. foreach ($poFile as $key => $line) {
  69. if (strstr($line, 'msgid') && $key > 5) {
  70. $tmp = $poFile[$key];
  71. $poFile[$key] = $poFile[$key + 1];
  72. $poFile[$key + 1] = $tmp;
  73. $poFile[$key] = str_replace('msgstr', 'msgid', $poFile[$key]);
  74. $poFile[$key + 1] = str_replace('msgid', 'msgstr', $poFile[$key + 1]);
  75. }
  76. }
  77. $pofilePathRu = str_replace('en_US', 'ru_RU', $pofilePath);
  78. file_put_contents($pofilePathRu, implode('', $poFile));
  79. foreach ($poFile as $key => $line) {
  80. if (strstr($line, 'msgstr') && $key > 5) {
  81. $poFile[$key] = "msgstr \"\"\n";
  82. }
  83. }
  84. file_put_contents($pofilePath, implode('', $poFile));
  85. $poFileManager = new PoFileManager();
  86. $poFileManager->convertToMO($pofilePath);
  87. $poFileManager->convertToMO($pofilePathRu);
  88. }
  89. public static function adminAutoload() {
  90. self::fetchApiTpl();
  91. }
  92. public function autoload() {
  93. self::fetchApiTpl();
  94. }
  95. private static function fetchApiTpl() {
  96. $obj = CI::$APP;
  97. if (!$obj->input->is_ajax_request() || $obj->input->get('_pjax')) {
  98. $translator = $obj->db->where('name', 'translator')->get('components');
  99. if ($translator) {
  100. $translator = $translator->row_array();
  101. if ($translator['settings']) {
  102. $translatorSettings = unserialize($translator['settings']);
  103. if (isset($translatorSettings['showApiForm']) && $obj->dx_auth->is_admin()) {
  104. if (!defined('ENABLE_TRANSLATION_API')) {
  105. define('ENABLE_TRANSLATION_API', TRUE);
  106. }
  107. $lang = new MY_Lang();
  108. $lang->load('translator');
  109. $obj->template->registerJsFile('/templates/administrator/js/jquery-ui-1.8.23.custom.min.js');
  110. if (MAINSITE) {
  111. $obj->template->registerJsFile(MAINSITE . getModulePath('translator') . '/assets/js/translateSingleLang.js');
  112. $obj->template->display('file:' . getModulePath('translator') . '/assets/translationApiForm');
  113. } else {
  114. $obj->template->registerJsFile(getModulePath('translator') . '/assets/js/translateSingleLang.js');
  115. $obj->template->display('file:' . getModulePath('translator') . '/assets/translationApiForm');
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function translate() {
  126. $domain = $this->input->post('domain');
  127. $translation = $this->input->post('translation');
  128. $origin = $this->input->post('origin');
  129. $comment = $this->input->post('comment');
  130. $poFileManager = new PoFileManager();
  131. $po_Attributes = getPoFileAttributes($domain);
  132. if ($po_Attributes) {
  133. $data[$origin] = [
  134. 'translation' => $translation,
  135. 'comment' => $comment,
  136. ];
  137. if ($poFileManager->update($po_Attributes['name'], $po_Attributes['type'], $po_Attributes['lang'], $data)) {
  138. return json_encode(['success' => TRUE, 'message' => lang('Successfully translated.', 'translator')]);
  139. } else {
  140. $errors = $poFileManager->getErrors();
  141. $errors = $errors ? array_pop($errors) : '';
  142. return json_encode(['errors' => TRUE, 'message' => $errors]);
  143. }
  144. } else {
  145. return json_encode(['errors' => TRUE, 'message' => lang('Not valid translation file attributes.', 'translator')]);
  146. }
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getSettings() {
  152. $settings = getSettings();
  153. if (strstr($this->input->server('HTTP_REFERER'), 'admin')) {
  154. $locale = $this->config->item('language');
  155. $language = $this->db->select('identif')->where('locale', $locale)->get('languages');
  156. if ($language) {
  157. $language = $language->row_array();
  158. }
  159. $locale = $language['identif'];
  160. } else {
  161. $locale = MY_Controller::getCurrentLocale();
  162. }
  163. $settings['curLocale'] = $locale;
  164. $settings['successMessage'] = lang('Successfully translated.', 'translator');
  165. return json_encode($settings);
  166. }
  167. public function _install() {
  168. ($this->dx_auth->is_admin()) OR exit;
  169. $this->db->where('name', 'translator')
  170. ->update(
  171. 'components',
  172. [
  173. 'autoload' => '1',
  174. 'enabled' => '1',
  175. 'settings' => serialize(['originsLang' => 'en', 'editorTheme' => 'chrome']),
  176. ]
  177. );
  178. }
  179. public function _deinstall() {
  180. ($this->dx_auth->is_admin()) OR exit;
  181. $this->db->where('name', 'translator')->delete('components');
  182. }
  183. /**
  184. * Replace temlates languages(ru to en)
  185. * @param string $template_name - template name
  186. */
  187. public function replaceLangs($template_name) {
  188. if ($template_name) {
  189. Replacer::getInstatce()->run($template_name);
  190. }
  191. }
  192. /**
  193. * Restore template replaced template
  194. */
  195. public function restoreTemplate() {
  196. $source = $this->input->get('source');
  197. $backup = $this->input->get('backup');
  198. if ($source) {
  199. Replacer::getInstatce()->restoreTemplate($source, $backup);
  200. }
  201. }
  202. public function copyLangs() {
  203. $it = new RecursiveDirectoryIterator('/var/www/_image.loc/');
  204. foreach (new RecursiveIteratorIterator($it) as $file) {
  205. $file = (string) $file;
  206. $ext = end(explode('.', $file));
  207. if (strstr($file, '/uk_UA/')) {
  208. if ($ext == 'po') {
  209. $copyTo = str_replace('_image.loc', 'image.loc', $file);
  210. $path = array_shift(explode('/uk_UA/', $copyTo));
  211. $path = $path . '/uk_UA';
  212. mkdir($path);
  213. chmod($path, 0777);
  214. $path = $path . '/LC_MESSAGES';
  215. mkdir($path);
  216. chmod($path, 0777);
  217. unlink($copyTo);
  218. copy($file, $copyTo);
  219. chmod($copyTo, 0777);
  220. }
  221. if ($ext == 'mo') {
  222. $copyTo = str_replace('_image.loc', 'image.loc', $file);
  223. $path = array_shift(explode('/uk_UA/', $copyTo));
  224. $path = $path . '/uk_UA';
  225. mkdir($path);
  226. chmod($path, 0777);
  227. $path = $path . '/LC_MESSAGES';
  228. mkdir($path);
  229. chmod($path, 0777);
  230. unlink($copyTo);
  231. copy($file, $copyTo);
  232. chmod($copyTo, 0777);
  233. }
  234. }
  235. }
  236. }
  237. }