PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/l10n.php

https://github.com/sezuan/core
PHP | 383 lines | 205 code | 26 blank | 152 comment | 50 complexity | e2dc391072ca191108ae249555ee8771 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This class is for i18n and l10n
  24. */
  25. class OC_L10N{
  26. /**
  27. * cached instances
  28. */
  29. protected static $instances=array();
  30. /**
  31. * cache
  32. */
  33. protected static $cache = array();
  34. /**
  35. * The best language
  36. */
  37. protected static $language = '';
  38. /**
  39. * App of this object
  40. */
  41. protected $app;
  42. /**
  43. * Language of this object
  44. */
  45. protected $lang;
  46. /**
  47. * Translations
  48. */
  49. private $translations = array();
  50. /**
  51. * Localization
  52. */
  53. private $localizations = array(
  54. 'jsdate' => 'dd.mm.yy',
  55. 'date' => '%d.%m.%Y',
  56. 'datetime' => '%d.%m.%Y %H:%M:%S',
  57. 'time' => '%H:%M:%S',
  58. 'firstday' => 0);
  59. /**
  60. * get an L10N instance
  61. * @return OC_L10N
  62. */
  63. public static function get($app, $lang=null) {
  64. if(is_null($lang)) {
  65. if(!isset(self::$instances[$app])) {
  66. self::$instances[$app]=new OC_L10N($app);
  67. }
  68. return self::$instances[$app];
  69. }else{
  70. return new OC_L10N($app, $lang);
  71. }
  72. }
  73. /**
  74. * @brief The constructor
  75. * @param $app the app requesting l10n
  76. * @param $lang default: null Language
  77. * @returns OC_L10N-Object
  78. *
  79. * If language is not set, the constructor tries to find the right
  80. * language.
  81. */
  82. public function __construct($app, $lang = null) {
  83. $this->app = $app;
  84. $this->lang = $lang;
  85. }
  86. protected function init() {
  87. if ($this->app === true) {
  88. return;
  89. }
  90. $app = OC_App::cleanAppId($this->app);
  91. $lang = $this->lang;
  92. $this->app = true;
  93. // Find the right language
  94. if(is_null($lang) || $lang == '') {
  95. $lang = self::findLanguage($app);
  96. }
  97. // Use cache if possible
  98. if(array_key_exists($app.'::'.$lang, self::$cache)) {
  99. $this->translations = self::$cache[$app.'::'.$lang]['t'];
  100. $this->localizations = self::$cache[$app.'::'.$lang]['l'];
  101. }
  102. else{
  103. $i18ndir = self::findI18nDir($app);
  104. // Localization is in /l10n, Texts are in $i18ndir
  105. // (Just no need to define date/time format etc. twice)
  106. if((OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC_App::getAppPath($app).'/l10n/')
  107. || OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC::$SERVERROOT.'/core/l10n/')
  108. || OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC::$SERVERROOT.'/lib/l10n/')
  109. || OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC::$SERVERROOT.'/settings')
  110. )
  111. && file_exists($i18ndir.$lang.'.php')) {
  112. // Include the file, save the data from $CONFIG
  113. $transFile = strip_tags($i18ndir).strip_tags($lang).'.php';
  114. include $transFile;
  115. if(isset($TRANSLATIONS) && is_array($TRANSLATIONS)) {
  116. $this->translations = $TRANSLATIONS;
  117. //merge with translations from theme
  118. $theme = OC_Config::getValue( "theme" );
  119. if (!is_null($theme)) {
  120. $transFile = OC::$SERVERROOT.'/themes/'.$theme.substr($transFile, strlen(OC::$SERVERROOT));
  121. if (file_exists($transFile)) {
  122. include $transFile;
  123. if (isset($TRANSLATIONS) && is_array($TRANSLATIONS)) {
  124. $this->translations = array_merge($this->translations, $TRANSLATIONS);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. if(file_exists(OC::$SERVERROOT.'/core/l10n/l10n-'.$lang.'.php')) {
  131. // Include the file, save the data from $CONFIG
  132. include OC::$SERVERROOT.'/core/l10n/l10n-'.$lang.'.php';
  133. if(isset($LOCALIZATIONS) && is_array($LOCALIZATIONS)) {
  134. $this->localizations = array_merge($this->localizations, $LOCALIZATIONS);
  135. }
  136. }
  137. self::$cache[$app.'::'.$lang]['t'] = $this->translations;
  138. self::$cache[$app.'::'.$lang]['l'] = $this->localizations;
  139. }
  140. }
  141. /**
  142. * @brief Translating
  143. * @param $text String The text we need a translation for
  144. * @param array $parameters default:array() Parameters for sprintf
  145. * @return \OC_L10N_String Translation or the same text
  146. *
  147. * Returns the translation. If no translation is found, $text will be
  148. * returned.
  149. */
  150. public function t($text, $parameters = array()) {
  151. return new OC_L10N_String($this, $text, $parameters);
  152. }
  153. /**
  154. * @brief Translating
  155. * @param $textArray The text array we need a translation for
  156. * @returns Translation or the same text
  157. *
  158. * Returns the translation. If no translation is found, $textArray will be
  159. * returned.
  160. *
  161. *
  162. * @deprecated deprecated since ownCloud version 5.0
  163. * This method will probably be removed with ownCloud 6.0
  164. *
  165. *
  166. */
  167. public function tA($textArray) {
  168. OC_Log::write('core', 'DEPRECATED: the method tA is deprecated and will be removed soon.', OC_Log::WARN);
  169. $result = array();
  170. foreach($textArray as $key => $text) {
  171. $result[$key] = (string)$this->t($text);
  172. }
  173. return $result;
  174. }
  175. /**
  176. * @brief getTranslations
  177. * @returns Fetch all translations
  178. *
  179. * Returns an associative array with all translations
  180. */
  181. public function getTranslations() {
  182. $this->init();
  183. return $this->translations;
  184. }
  185. /**
  186. * @brief Localization
  187. * @param $type Type of localization
  188. * @param $params parameters for this localization
  189. * @returns String or false
  190. *
  191. * Returns the localized data.
  192. *
  193. * Implemented types:
  194. * - date
  195. * - Creates a date
  196. * - l10n-field: date
  197. * - params: timestamp (int/string)
  198. * - datetime
  199. * - Creates date and time
  200. * - l10n-field: datetime
  201. * - params: timestamp (int/string)
  202. * - time
  203. * - Creates a time
  204. * - l10n-field: time
  205. * - params: timestamp (int/string)
  206. */
  207. public function l($type, $data) {
  208. $this->init();
  209. switch($type) {
  210. // If you add something don't forget to add it to $localizations
  211. // at the top of the page
  212. case 'date':
  213. case 'datetime':
  214. case 'time':
  215. if($data instanceof DateTime) return $data->format($this->localizations[$type]);
  216. elseif(is_string($data)) $data = strtotime($data);
  217. $locales = array(self::findLanguage());
  218. if (strlen($locales[0]) == 2) {
  219. $locales[] = $locales[0].'_'.strtoupper($locales[0]);
  220. }
  221. setlocale(LC_TIME, $locales);
  222. $format = $this->localizations[$type];
  223. // Check for Windows to find and replace the %e modifier correctly
  224. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  225. $format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
  226. }
  227. return strftime($format, $data);
  228. break;
  229. case 'firstday':
  230. case 'jsdate':
  231. return $this->localizations[$type];
  232. default:
  233. return false;
  234. }
  235. }
  236. /**
  237. * @brief Choose a language
  238. * @param $texts Associative Array with possible strings
  239. * @returns String
  240. *
  241. * $text is an array 'de' => 'hallo welt', 'en' => 'hello world', ...
  242. *
  243. * This function is useful to avoid loading thousands of files if only one
  244. * simple string is needed, for example in appinfo.php
  245. */
  246. public static function selectLanguage($text) {
  247. $lang = self::findLanguage(array_keys($text));
  248. return $text[$lang];
  249. }
  250. /**
  251. * @brief find the best language
  252. * @param $app Array or string, details below
  253. * @returns language
  254. *
  255. * If $app is an array, ownCloud assumes that these are the available
  256. * languages. Otherwise ownCloud tries to find the files in the l10n
  257. * folder.
  258. *
  259. * If nothing works it returns 'en'
  260. */
  261. public static function findLanguage($app = null) {
  262. if(!is_array($app) && self::$language != '') {
  263. return self::$language;
  264. }
  265. if(OC_User::getUser() && OC_Preferences::getValue(OC_User::getUser(), 'core', 'lang')) {
  266. $lang = OC_Preferences::getValue(OC_User::getUser(), 'core', 'lang');
  267. self::$language = $lang;
  268. if(is_array($app)) {
  269. $available = $app;
  270. $lang_exists = array_search($lang, $available) !== false;
  271. }
  272. else {
  273. $lang_exists = self::languageExists($app, $lang);
  274. }
  275. if($lang_exists) {
  276. return $lang;
  277. }
  278. }
  279. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  280. $accepted_languages = preg_split('/,\s*/', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
  281. if(is_array($app)) {
  282. $available = $app;
  283. }
  284. else{
  285. $available = self::findAvailableLanguages($app);
  286. }
  287. foreach($accepted_languages as $i) {
  288. $temp = explode(';', $i);
  289. $temp[0] = str_replace('-', '_', $temp[0]);
  290. if( ($key = array_search($temp[0], $available)) !== false) {
  291. if (is_null($app)) {
  292. self::$language = $available[$key];
  293. }
  294. return $available[$key];
  295. }
  296. foreach($available as $l) {
  297. if ( $temp[0] == substr($l, 0, 2) ) {
  298. if (is_null($app)) {
  299. self::$language = $l;
  300. }
  301. return $l;
  302. }
  303. }
  304. }
  305. }
  306. // Last try: English
  307. return 'en';
  308. }
  309. /**
  310. * @brief find the l10n directory
  311. * @param $app App that needs to be translated
  312. * @returns directory
  313. */
  314. protected static function findI18nDir($app) {
  315. // find the i18n dir
  316. $i18ndir = OC::$SERVERROOT.'/core/l10n/';
  317. if($app != '') {
  318. // Check if the app is in the app folder
  319. if(file_exists(OC_App::getAppPath($app).'/l10n/')) {
  320. $i18ndir = OC_App::getAppPath($app).'/l10n/';
  321. }
  322. else{
  323. $i18ndir = OC::$SERVERROOT.'/'.$app.'/l10n/';
  324. }
  325. }
  326. return $i18ndir;
  327. }
  328. /**
  329. * @brief find all available languages for an app
  330. * @param $app App that needs to be translated
  331. * @returns array an array of available languages
  332. */
  333. public static function findAvailableLanguages($app=null) {
  334. $available=array('en');//english is always available
  335. $dir = self::findI18nDir($app);
  336. if(is_dir($dir)) {
  337. $files=scandir($dir);
  338. foreach($files as $file) {
  339. if(substr($file, -4, 4) === '.php' && substr($file, 0, 4) !== 'l10n') {
  340. $i = substr($file, 0, -4);
  341. $available[] = $i;
  342. }
  343. }
  344. }
  345. return $available;
  346. }
  347. public static function languageExists($app, $lang) {
  348. if ($lang == 'en') {//english is always available
  349. return true;
  350. }
  351. $dir = self::findI18nDir($app);
  352. if(is_dir($dir)) {
  353. return file_exists($dir.'/'.$lang.'.php');
  354. }
  355. return false;
  356. }
  357. }