PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Translate.php

https://bitbucket.org/enurkov/prestashop
PHP | 237 lines | 128 code | 34 blank | 75 comment | 32 complexity | 21d48c92b8283f200ad1a46d41c3c9d3 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class TranslateCore
  30. {
  31. /**
  32. * Get a translation for an admin controller
  33. *
  34. * @param $string
  35. * @param string $class
  36. * @param bool $addslashes
  37. * @param bool $htmlentities
  38. * @return string
  39. */
  40. public static function getAdminTranslation($string, $class = 'AdminTab', $addslashes = false, $htmlentities = true, $sprintf = null)
  41. {
  42. static $modules_tabs = null;
  43. // @todo remove global keyword in translations files and use static
  44. global $_LANGADM;
  45. if ($modules_tabs === null)
  46. $modules_tabs = Tab::getModuleTabList();
  47. if ($_LANGADM == null)
  48. {
  49. $iso = Context::getContext()->language->iso_code;
  50. include_once(_PS_TRANSLATIONS_DIR_.$iso.'/admin.php');
  51. }
  52. if (isset($modules_tabs[strtolower($class)]))
  53. {
  54. $class_name_controller = $class.'controller';
  55. // if the class is extended by a module, use modules/[module_name]/xx.php lang file
  56. if (class_exists($class_name_controller) && Module::getModuleNameFromClass($class_name_controller))
  57. {
  58. $string = str_replace('\'', '\\\'', $string);
  59. return Translate::getModuleTranslation(Module::$classInModule[$class_name_controller], $string, $class_name_controller);
  60. }
  61. }
  62. $key = md5(str_replace('\'', '\\\'', $string));
  63. if (isset($_LANGADM[$class.$key]))
  64. $str = $_LANGADM[$class.$key];
  65. else
  66. $str = Translate::getGenericAdminTranslation($string, $key, $_LANGADM);
  67. if ($htmlentities)
  68. $str = htmlentities($str, ENT_QUOTES, 'utf-8');
  69. $str = str_replace('"', '&quot;', $str);
  70. if ($sprintf !== null)
  71. $str = Translate::checkAndReplaceArgs($str, $sprintf);
  72. return ($addslashes ? addslashes($str) : stripslashes($str));
  73. }
  74. /**
  75. * Return the translation for a string if it exists for the base AdminController or for helpers
  76. *
  77. * @static
  78. * @param $string string to translate
  79. * @param null $key md5 key if already calculated (optional)
  80. * @param $lang_array global array of admin translations
  81. * @return string translation
  82. */
  83. public static function getGenericAdminTranslation($string, $key = null, &$lang_array)
  84. {
  85. if (is_null($key))
  86. $key = md5(str_replace('\'', '\\\'', $string));
  87. if (isset($lang_array['AdminController'.$key]))
  88. $str = $lang_array['AdminController'.$key];
  89. else if (isset($lang_array['Helper'.$key]))
  90. $str = $lang_array['Helper'.$key];
  91. else if (isset($lang_array['AdminTab'.$key]))
  92. $str = $lang_array['AdminTab'.$key];
  93. else
  94. // note in 1.5, some translations has moved from AdminXX to helper/*.tpl
  95. $str = $string;
  96. return $str;
  97. }
  98. /**
  99. * Get a translation for a module
  100. *
  101. * @param string|Module $module
  102. * @param string $string
  103. * @param string $source
  104. * @return string
  105. */
  106. public static function getModuleTranslation($module, $string, $source, $sprintf = null, $js = false)
  107. {
  108. global $_MODULES, $_MODULE, $_LANGADM;
  109. static $lang_cache = array();
  110. // $_MODULES is a cache of translations for all module.
  111. // $translations_merged is a cache of wether a specific module's translations have already been added to $_MODULES
  112. static $translations_merged = array();
  113. $name = $module instanceof Module ? $module->name : $module;
  114. if (!isset($translations_merged[$name]))
  115. {
  116. $filesByPriority = array(
  117. // Translations in theme
  118. _PS_THEME_DIR_.'modules/'.$name.'/translations/'.Context::getContext()->language->iso_code.'.php',
  119. _PS_THEME_DIR_.'modules/'.$name.'/'.Context::getContext()->language->iso_code.'.php',
  120. // PrestaShop 1.5 translations
  121. _PS_MODULE_DIR_.$name.'/translations/'.Context::getContext()->language->iso_code.'.php',
  122. // PrestaShop 1.4 translations
  123. _PS_MODULE_DIR_.$name.'/'.Context::getContext()->language->iso_code.'.php'
  124. );
  125. foreach ($filesByPriority as $file)
  126. if (Tools::file_exists_cache($file))
  127. {
  128. include_once($file);
  129. $_MODULES = !empty($_MODULES) ? $_MODULES + $_MODULE : $_MODULE; //we use "+" instead of array_merge() because array merge erase existing values.
  130. $translations_merged[$name] = true;
  131. }
  132. }
  133. $key = md5(str_replace('\'', '\\\'', $string));
  134. $cache_key = $name.'|'.$string.'|'.$source;
  135. if (!isset($lang_cache[$cache_key]))
  136. {
  137. if ($_MODULES == null)
  138. {
  139. if ($sprintf !== null)
  140. $string = Translate::checkAndReplaceArgs($string, $sprintf);
  141. return str_replace('"', '&quot;', $string);
  142. }
  143. $current_key = strtolower('<{'.$name.'}'._THEME_NAME_.'>'.$source).'_'.$key;
  144. $default_key = strtolower('<{'.$name.'}prestashop>'.$source).'_'.$key;
  145. if (isset($_MODULES[$current_key]))
  146. $ret = stripslashes($_MODULES[$current_key]);
  147. elseif (isset($_MODULES[$default_key]))
  148. $ret = stripslashes($_MODULES[$default_key]);
  149. // if translation was not found in module, look for it in AdminController or Helpers
  150. elseif (!empty($_LANGADM))
  151. $ret = Translate::getGenericAdminTranslation($string, $key, $_LANGADM);
  152. else
  153. $ret = stripslashes($string);
  154. if ($sprintf !== null)
  155. $ret = Translate::checkAndReplaceArgs($ret, $sprintf);
  156. if ($js)
  157. $ret = addslashes($ret);
  158. $lang_cache[$cache_key] = str_replace('"', '&quot;', $ret);
  159. }
  160. return $lang_cache[$cache_key];
  161. }
  162. /**
  163. * Get a translation for a PDF
  164. *
  165. * @param string $string
  166. * @return string
  167. */
  168. public static function getPdfTranslation($string)
  169. {
  170. global $_LANGPDF;
  171. $iso = Context::getContext()->language->iso_code;
  172. if (!Validate::isLangIsoCode($iso))
  173. Tools::displayError(sprintf('Invalid iso lang (%s)', Tools::safeOutput($iso)));
  174. $override_i18n_file = _PS_THEME_DIR_.'pdf/lang/'.$iso.'.php';
  175. $i18n_file = _PS_TRANSLATIONS_DIR_.$iso.'/pdf.php';
  176. if (file_exists($override_i18n_file))
  177. $i18n_file = $override_i18n_file;
  178. if (!include($i18n_file))
  179. Tools::displayError(sprintf('Cannot include PDF translation language file : %s', $i18n_file));
  180. if (!isset($_LANGPDF) || !is_array($_LANGPDF))
  181. return str_replace('"', '&quot;', $string);
  182. $key = md5(str_replace('\'', '\\\'', $string));
  183. $str = (array_key_exists('PDF'.$key, $_LANGPDF) ? $_LANGPDF['PDF'.$key] : $string);
  184. return $str;
  185. }
  186. /**
  187. * Check if string use a specif syntax for sprintf and replace arguments if use it
  188. *
  189. * @param $string
  190. * @param $args
  191. * @return string
  192. */
  193. public static function checkAndReplaceArgs($string, $args)
  194. {
  195. if (preg_match_all('#(?:%%|%(?:[0-9]+\$)?[+-]?(?:[ 0]|\'.)?-?[0-9]*(?:\.[0-9]+)?[bcdeufFosxX])#', $string, $matches) && !is_null($args))
  196. {
  197. if (!is_array($args))
  198. $args = array($args);
  199. return vsprintf($string, $args);
  200. }
  201. return $string;
  202. }
  203. }