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

/instances/common/controllers/manager/findi18n.ctrl.php

http://github.com/alombarte/SIFO
PHP | 220 lines | 211 code | 6 blank | 3 comment | 2 complexity | 02f7fa926c1104792cb69c9f72ec0edc MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. namespace Common;
  3. class ManagerFindi18nController extends \Sifo\Controller
  4. {
  5. public function extractStringsForTranslation( $path, $instance, $in_templates = false )
  6. {
  7. // Parse .php files:
  8. $literals = array();
  9. if ( !$in_templates )
  10. {
  11. exec( "find * $path |grep .php$", $file_list );
  12. }
  13. else
  14. {
  15. exec( "find * $path |grep .tpl$", $file_list );
  16. }
  17. foreach ( $file_list as $file_path )
  18. {
  19. $tpl_text = shell_exec( "cat {$file_path}" );
  20. if ( !$in_templates )
  21. {
  22. // $this->translate functions
  23. preg_match_all( "/translate\s*\(\s*\'([^\']+)\'[^\)]*\)/", $tpl_text, $translate_single_quotes );
  24. preg_match_all( "/translate\s*\(\s*\"([^\"]+)\"[^\)]*\)/", $tpl_text, $translate_double_quotes );
  25. // \Sifo\\Sifo\\Sifo\I18N::getTranslation functions
  26. preg_match_all( "/getTranslation\s*\(\s*\'([^\']+)\'[^\)]*\)/", $tpl_text, $i18n_translate_single_quotes );
  27. preg_match_all( "/getTranslation\s*\(\s*\"([^\"]+)\"[^\)]*\)/", $tpl_text, $i18n_translate_double_quotes );
  28. // \Sifo\FlashMessages
  29. preg_match_all( "/FlasMessages::set\s*\(\s*\'([^\']+)\'[^\)]*\)/", $tpl_text, $flash_translate_single_quotes );
  30. preg_match_all( "/FlasMessages::set\s*\(\s*\"([^\"]+)\"[^\)]*\)/", $tpl_text, $flash_translate_double_quotes );
  31. $file_literals = array_unique( array_merge(
  32. $translate_single_quotes[1],
  33. $translate_double_quotes[1],
  34. $i18n_translate_single_quotes[1],
  35. $i18n_translate_double_quotes[1],
  36. $flash_translate_single_quotes[1],
  37. $flash_translate_double_quotes[1]
  38. ) );
  39. }
  40. else
  41. {
  42. // {t}Search 'T' blocks{/t}
  43. preg_match_all( "/\{t([^\{\}]*)\}([^\{\}]+)\{\/t[^\}]*\}/", $tpl_text, $matches );
  44. $file_literals = array_unique( $matches[2] );
  45. }
  46. if ( preg_match("/{$instance}\/(.+)$/", $file_path, $matchs) )
  47. {
  48. $file_relative_path = $matchs[1];
  49. }
  50. foreach ( $file_literals as $literal )
  51. {
  52. if ( array_key_exists( $literal, $literals ) )
  53. {
  54. $literals[$literal] = ( $literals[$literal] . ", " . $file_relative_path );
  55. }
  56. else
  57. {
  58. $literals[$literal] = $file_relative_path;
  59. }
  60. }
  61. }
  62. return $literals;
  63. }
  64. public function getLiterals( $instance )
  65. {
  66. $path = \Sifo\Bootstrap::$application . "/$instance";
  67. // Parse all templates
  68. $literals_groups['tpl'] = $this->extractStringsForTranslation( "$path/templates", $instance, true );
  69. // Parse all models:
  70. $literals_groups['models'] = $this->extractStringsForTranslation( "$path/models", $instance, false );
  71. // Parse all controllers:
  72. $literals_groups['controllers'] = $this->extractStringsForTranslation( "$path/controllers", $instance, false );
  73. // Parse all form configs:
  74. $literals_groups['forms'] = $this->extractStringsForTranslation( "$path/config", $instance, false );
  75. // Smarty plugins:
  76. $libs_path = ROOT_PATH . \Sifo\Config::getInstance()->getLibrary( 'smarty' ) . '/plugins';
  77. $literals_groups['smarty'] = $this->extractStringsForTranslation( $libs_path, 'libs', false );
  78. // Your instance plugins:
  79. $instance_plugins = $path . '/templates/_smarty/plugins';
  80. if ( is_dir( $instance_plugins ) )
  81. {
  82. $literals_groups['smarty'] = array_merge( $literals_groups['smarty'], $this->extractStringsForTranslation( $instance_plugins, $instance, false ) );
  83. }
  84. $final_literals = array();
  85. foreach ( $literals_groups as $group )
  86. {
  87. foreach ( $group as $literal=>$relative_path )
  88. {
  89. if ( array_key_exists( $literal, $final_literals ) )
  90. {
  91. $final_literals[$literal] = ( $final_literals[$literal] . ", " . $relative_path );
  92. }
  93. else
  94. {
  95. $final_literals[$literal] = $relative_path;
  96. }
  97. }
  98. }
  99. return $final_literals;
  100. }
  101. public function build()
  102. {
  103. $this->setLayout( 'manager/findi18n.tpl' );
  104. if ( !\Sifo\Domains::getInstance()->getDevMode() )
  105. {
  106. throw new \Sifo\Exception_404( 'Translation only available while in devel mode' );
  107. }
  108. $post = \Sifo\Filter::getInstance();
  109. $available_instances = $this->getFileSystemFiles( 'instances', true );
  110. $locales_available = array();
  111. foreach ( $available_instances as $inst )
  112. {
  113. $locales_available[$inst] = $this->getFilesystemFiles( "instances/$inst/locale" );
  114. }
  115. $this->assign( 'instances', $available_instances );
  116. $this->assign( 'locales', $locales_available );
  117. $charset = $post->getString( 'charset' );
  118. $this->assign( 'charset', ( $charset ? $charset : 'utf-8' ) );
  119. $this->assign( 'instance', 'common' );
  120. if ( $post->isSent( 'instance' ) )
  121. {
  122. $instance = $post->getString( 'instance' );
  123. $locale = $post->getString( 'locale' );
  124. $temp_lang = explode ( '_', $locale );
  125. $this->assign( 'language', $temp_lang[1] );
  126. $literals = $this->getLiterals( $instance );
  127. $this->assign( 'literals', $literals );
  128. $path = \Sifo\Bootstrap::$application . "/$instance";
  129. $translations_file = "$path/locale/$locale";
  130. if ( file_exists( $translations_file ) )
  131. {
  132. include "$translations_file";
  133. $missing = array();
  134. foreach( $literals as $key=>$relative_path )
  135. {
  136. if ( !isset( $translations[$key] ) )
  137. {
  138. $missing[$key]= $relative_path;
  139. }
  140. }
  141. $this->assign( 'missing', $missing );
  142. $this->assign( 'instance', $instance );
  143. $this->assign( 'locale', $locale );
  144. }
  145. else
  146. {
  147. $this->assign( 'error', "File $locale not available for <strong>$instance</strong>" );
  148. }
  149. }
  150. }
  151. /**
  152. * Extracts from the filesystem all the files under a path.
  153. * If the flag only_dirs is set to true returns only the directories names.
  154. *
  155. * @return array
  156. */
  157. public function getFileSystemFiles( $relative_path, $only_dirs = false )
  158. {
  159. $files = array();
  160. // Extract directories:
  161. $iterator = new \DirectoryIterator( ROOT_PATH . "/$relative_path" );
  162. foreach ( $iterator as $fileinfo )
  163. {
  164. $file = $fileinfo->getFilename();
  165. // Exclude .svn, .cache and any other file starting with .
  166. if ( 0 !== strpos( $file, '.' ) )
  167. {
  168. if ( $only_dirs )
  169. {
  170. if ( $fileinfo->isDir() ) $files[] = $file;
  171. }
  172. else
  173. {
  174. $files[] = $file;
  175. }
  176. }
  177. }
  178. return $files;
  179. }
  180. }