PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Ionize/application/controllers/admin/translation.php

http://you.googlecode.com/
PHP | 332 lines | 150 code | 65 blank | 117 comment | 19 complexity | 2fa1b83050ed70070794da9e30f683ed MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Ionize
  4. *
  5. * @package Ionize
  6. * @author Ionize Dev Team
  7. * @license http://ionizecms.com/doc-license
  8. * @link http://ionizecms.com
  9. * @since Version 0.90
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * Ionize Category Controller
  14. *
  15. * @package Ionize
  16. * @subpackage Controllers
  17. * @category Translation files management
  18. * @author Ionize Dev Team
  19. *
  20. */
  21. class Translation extends MY_admin
  22. {
  23. /**
  24. * Constructor
  25. *
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. // $this->connect->restrict('editors');
  31. }
  32. // ------------------------------------------------------------------------
  33. /**
  34. * Shows standard settings
  35. *
  36. */
  37. function index()
  38. {
  39. $this->template['views_terms'] = $this->_get_terms_from_views();
  40. sort($this->template['views_terms']['term'], SORT_STRING );
  41. // Get the already translated items from languages files
  42. $translated_items = $this->_get_translated_items();
  43. foreach(Settings::get_languages() as $language)
  44. {
  45. $lang = $language['lang'];
  46. foreach($this->template['views_terms']['term'] as $term)
  47. {
  48. // translation not defined : set to ''
  49. if(!isset($translated_items[$lang][$term]))
  50. {
  51. $translated_items[$lang][$term] = '';
  52. }
  53. }
  54. }
  55. $this->template['translated_items'] = $translated_items;
  56. unset($translated_items);
  57. $this->output('translation');
  58. }
  59. // ------------------------------------------------------------------------
  60. /**
  61. * Saves the translation language files
  62. *
  63. */
  64. function save()
  65. {
  66. // Get the item to translate from views
  67. $items = $this->_get_items();
  68. // Tries to creates each language file
  69. foreach(Settings::get_languages() as $language)
  70. {
  71. $lang = $language['lang'];
  72. // Creates the lang folder if it doesn't exists
  73. $path = $this->config->item('base_path').'/themes/'.Settings::get('theme').'/language/'.$lang;
  74. if ( ! is_dir($path) )
  75. {
  76. // Try creating the dir
  77. try
  78. {
  79. @mkdir($path);
  80. }
  81. catch (Exception $e)
  82. {
  83. // Send error message to user
  84. $this->error(lang('ionize_message_language_dir_creation_fail'));
  85. }
  86. }
  87. // Saves the languages files
  88. $data = "<?php\n\n";
  89. foreach ($items as $item)
  90. {
  91. $data .= "\$lang['".$item."'] = \"".addslashes(htmlentities($this->input->post($item.'_'.$lang, TRUE), ENT_QUOTES, 'UTF-8'))."\";\n";
  92. }
  93. $data .= "\n".'?'.'>';
  94. // Try writing the language file
  95. try
  96. {
  97. write_file($path.'/'.Settings::get('theme').'_lang.php', $data);
  98. }
  99. catch (Exception $e)
  100. {
  101. $this->error(lang('ionize_message_language_file_creation_fail'));
  102. }
  103. }
  104. // If method arrive her, everything was OK
  105. $this->success(lang('ionize_message_language_files_saved'));
  106. }
  107. // ------------------------------------------------------------------------
  108. /**
  109. * Get the array of items to translate
  110. *
  111. * @return array A simple array of unique items to translate, used for saving
  112. *
  113. */
  114. function _get_items()
  115. {
  116. // File helper
  117. $this->load->helper('file');
  118. // Theme views folder
  119. $path = $this->config->item('base_path').'/themes/'.Settings::get('theme').'/views';
  120. // Returned items array
  121. $items = array();
  122. if (is_dir($path))
  123. {
  124. $dir_iterator = new RecursiveDirectoryIterator($path);
  125. $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
  126. foreach ($iterator as $file)
  127. {
  128. if ($file->isFile() && (substr($file->getFilename(), 0, 1) != ".") )
  129. {
  130. $content = read_file($file->getPath() . '/' . $file->getFilename());
  131. /*
  132. if (preg_match_all('%<'.$this->context_tag.':translation term=\"([ \w:]+?)\" *\/>%', $content, $matches))
  133. {
  134. foreach($matches[1] as $term)
  135. {
  136. if (!in_array($term, $items))
  137. {
  138. $items[] = $term;
  139. }
  140. }
  141. }
  142. */
  143. if (preg_match_all('% term=\"([ \w:]+?)\" *\/>%', $content, $matches))
  144. {
  145. foreach($matches[1] as $term)
  146. {
  147. if (!in_array($term, $items))
  148. {
  149. $items[] = $term;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. return $items;
  157. }
  158. // ------------------------------------------------------------------------
  159. /**
  160. * Get the array of items to translate, per file
  161. * Parses the current theme views
  162. *
  163. * @return array Items to translate, by view
  164. * Array(
  165. * 'terms' => terms to translate,
  166. * 'views' => array(
  167. * term => views list
  168. * )
  169. * )
  170. *
  171. */
  172. function _get_terms_from_views()
  173. {
  174. // File helper
  175. $this->load->helper('file');
  176. // Theme views folder
  177. $path = $this->config->item('base_path').'/themes/'.Settings::get('theme').'/views';
  178. // Returned items array
  179. $items = array (
  180. 'term' => array(), // array of terms and their translations
  181. 'views' => array() // array of view in which each term appears, key : term
  182. );
  183. // Only do something if dir exists !
  184. if (is_dir($path))
  185. {
  186. // Recursive walk in the views folder
  187. $dir_iterator = new RecursiveDirectoryIterator($path);
  188. $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
  189. foreach ($iterator as $file)
  190. {
  191. if ($file->isFile() && (substr($file->getFilename(), 0, 1) != ".") )
  192. {
  193. $content = read_file($file->getPath() . '/' . $file->getFilename());
  194. // Check for each <ion:translation term="something" /> tag
  195. /*
  196. if (preg_match_all('%<'.$this->context_tag.':translation term=\"([ \w:]+?)\" *\/>%', $content, $matches))
  197. {
  198. foreach($matches[1] as $term)
  199. {
  200. // Add the view to the term view list
  201. if ( ! isset($items['views'][$term]) || ! in_array($file->getFilename(), $items['views'][$term]))
  202. $items['views'][$term][] = $file->getFilename();
  203. // Add the term to the term array
  204. if (!in_array($term, $items['term']))
  205. $items['term'][] = $term;
  206. }
  207. }
  208. */
  209. // Check for each term="something" in tags
  210. if (preg_match_all('% term=\"([ \w:]+?)\" *\/>%', $content, $matches))
  211. {
  212. foreach($matches[1] as $term)
  213. {
  214. // Add the view to the term view list
  215. if ( ! isset($items['views'][$term]) || ! in_array($file->getFilename(), $items['views'][$term]))
  216. $items['views'][$term][] = $file->getFilename();
  217. // Add the term to the term array
  218. if (!in_array($term, $items['term']))
  219. $items['term'][] = $term;
  220. }
  221. }
  222. }
  223. }
  224. // Make a string list from 'views' array
  225. foreach ($items['views'] as $term => $views)
  226. {
  227. $items['views'][$term] = implode(', ', $views);
  228. }
  229. }
  230. return $items;
  231. }
  232. // ------------------------------------------------------------------------
  233. /**
  234. * Gets already translated items from language files
  235. *
  236. * @return array Array of already translated terms
  237. */
  238. function _get_translated_items()
  239. {
  240. $items = array();
  241. $this->load->helper('file');
  242. // Theme folder
  243. $path = $this->config->item('base_path').'/themes/'.Settings::get('theme');
  244. $result = array();
  245. // Read the template language directory
  246. foreach(Settings::get_languages() as $language)
  247. {
  248. $lang_code = $language['lang'];
  249. // Translation file name. look like [theme_name]_lang.php
  250. $file = $path.'/language/'.$lang_code.'/'.Settings::get('theme').'_lang.php';
  251. // Include the file if it exists
  252. if (file_exists($file))
  253. {
  254. include($file);
  255. if (isset($lang))
  256. {
  257. $items[$lang_code] = $lang;
  258. unset($lang);
  259. }
  260. }
  261. }
  262. return $items;
  263. }
  264. }
  265. /* End of file translation.php */
  266. /* Location: ./application/controllers/admin/translation.php */