PageRenderTime 33ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/l10n.php

https://github.com/jlgg/simple_trash
PHP | 365 lines | 188 code | 26 blank | 151 comment | 44 complexity | 064739b12238f2e8dc6cd00dcd89f221 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 = $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. include strip_tags($i18ndir).strip_tags($lang).'.php';
  114. if(isset($TRANSLATIONS) && is_array($TRANSLATIONS)) {
  115. $this->translations = $TRANSLATIONS;
  116. }
  117. }
  118. if(file_exists(OC::$SERVERROOT.'/core/l10n/l10n-'.$lang.'.php')) {
  119. // Include the file, save the data from $CONFIG
  120. include OC::$SERVERROOT.'/core/l10n/l10n-'.$lang.'.php';
  121. if(isset($LOCALIZATIONS) && is_array($LOCALIZATIONS)) {
  122. $this->localizations = array_merge($this->localizations, $LOCALIZATIONS);
  123. }
  124. }
  125. self::$cache[$app.'::'.$lang]['t'] = $this->translations;
  126. self::$cache[$app.'::'.$lang]['l'] = $this->localizations;
  127. }
  128. }
  129. /**
  130. * @brief Translating
  131. * @param $text String The text we need a translation for
  132. * @param array $parameters default:array() Parameters for sprintf
  133. * @return \OC_L10N_String Translation or the same text
  134. *
  135. * Returns the translation. If no translation is found, $text will be
  136. * returned.
  137. */
  138. public function t($text, $parameters = array()) {
  139. return new OC_L10N_String($this, $text, $parameters);
  140. }
  141. /**
  142. * @brief Translating
  143. * @param $textArray The text array we need a translation for
  144. * @returns Translation or the same text
  145. *
  146. * Returns the translation. If no translation is found, $textArray will be
  147. * returned.
  148. *
  149. *
  150. * @deprecated deprecated since ownCloud version 5.0
  151. * This method will probably be removed with ownCloud 6.0
  152. *
  153. *
  154. */
  155. public function tA($textArray) {
  156. OC_Log::write('core', 'DEPRECATED: the method tA is deprecated and will be removed soon.', OC_Log::WARN);
  157. $result = array();
  158. foreach($textArray as $key => $text) {
  159. $result[$key] = (string)$this->t($text);
  160. }
  161. return $result;
  162. }
  163. /**
  164. * @brief getTranslations
  165. * @returns Fetch all translations
  166. *
  167. * Returns an associative array with all translations
  168. */
  169. public function getTranslations() {
  170. $this->init();
  171. return $this->translations;
  172. }
  173. /**
  174. * @brief Localization
  175. * @param $type Type of localization
  176. * @param $params parameters for this localization
  177. * @returns String or false
  178. *
  179. * Returns the localized data.
  180. *
  181. * Implemented types:
  182. * - date
  183. * - Creates a date
  184. * - l10n-field: date
  185. * - params: timestamp (int/string)
  186. * - datetime
  187. * - Creates date and time
  188. * - l10n-field: datetime
  189. * - params: timestamp (int/string)
  190. * - time
  191. * - Creates a time
  192. * - l10n-field: time
  193. * - params: timestamp (int/string)
  194. */
  195. public function l($type, $data) {
  196. $this->init();
  197. switch($type) {
  198. // If you add something don't forget to add it to $localizations
  199. // at the top of the page
  200. case 'date':
  201. case 'datetime':
  202. case 'time':
  203. if($data instanceof DateTime) return $data->format($this->localizations[$type]);
  204. elseif(is_string($data)) $data = strtotime($data);
  205. $locales = array(self::findLanguage());
  206. if (strlen($locales[0]) == 2) {
  207. $locales[] = $locales[0].'_'.strtoupper($locales[0]);
  208. }
  209. setlocale(LC_TIME, $locales);
  210. $format = $this->localizations[$type];
  211. // Check for Windows to find and replace the %e modifier correctly
  212. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  213. $format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
  214. }
  215. return strftime($format, $data);
  216. break;
  217. case 'firstday':
  218. case 'jsdate':
  219. return $this->localizations[$type];
  220. default:
  221. return false;
  222. }
  223. }
  224. /**
  225. * @brief Choose a language
  226. * @param $texts Associative Array with possible strings
  227. * @returns String
  228. *
  229. * $text is an array 'de' => 'hallo welt', 'en' => 'hello world', ...
  230. *
  231. * This function is useful to avoid loading thousands of files if only one
  232. * simple string is needed, for example in appinfo.php
  233. */
  234. public static function selectLanguage($text) {
  235. $lang = self::findLanguage(array_keys($text));
  236. return $text[$lang];
  237. }
  238. /**
  239. * @brief find the best language
  240. * @param $app Array or string, details below
  241. * @returns language
  242. *
  243. * If $app is an array, ownCloud assumes that these are the available
  244. * languages. Otherwise ownCloud tries to find the files in the l10n
  245. * folder.
  246. *
  247. * If nothing works it returns 'en'
  248. */
  249. public static function findLanguage($app = null) {
  250. if(!is_array($app) && self::$language != '') {
  251. return self::$language;
  252. }
  253. if(OC_User::getUser() && OC_Preferences::getValue(OC_User::getUser(), 'core', 'lang')) {
  254. $lang = OC_Preferences::getValue(OC_User::getUser(), 'core', 'lang');
  255. self::$language = $lang;
  256. if(is_array($app)) {
  257. $available = $app;
  258. $lang_exists = array_search($lang, $available) !== false;
  259. }
  260. else {
  261. $lang_exists = self::languageExists($app, $lang);
  262. }
  263. if($lang_exists) {
  264. return $lang;
  265. }
  266. }
  267. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  268. $accepted_languages = preg_split('/,\s*/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  269. if(is_array($app)) {
  270. $available = $app;
  271. }
  272. else{
  273. $available = self::findAvailableLanguages($app);
  274. }
  275. foreach($accepted_languages as $i) {
  276. $temp = explode(';', $i);
  277. $temp[0] = str_replace('-', '_', $temp[0]);
  278. if( ($key = array_search($temp[0], $available)) !== false) {
  279. return $available[$key];
  280. }
  281. foreach($available as $l) {
  282. if ( $temp[0] == substr($l, 0, 2) ) {
  283. return $l;
  284. }
  285. }
  286. }
  287. }
  288. // Last try: English
  289. return 'en';
  290. }
  291. /**
  292. * @brief find the l10n directory
  293. * @param $app App that needs to be translated
  294. * @returns directory
  295. */
  296. protected static function findI18nDir($app) {
  297. // find the i18n dir
  298. $i18ndir = OC::$SERVERROOT.'/core/l10n/';
  299. if($app != '') {
  300. // Check if the app is in the app folder
  301. if(file_exists(OC_App::getAppPath($app).'/l10n/')) {
  302. $i18ndir = OC_App::getAppPath($app).'/l10n/';
  303. }
  304. else{
  305. $i18ndir = OC::$SERVERROOT.'/'.$app.'/l10n/';
  306. }
  307. }
  308. return $i18ndir;
  309. }
  310. /**
  311. * @brief find all available languages for an app
  312. * @param $app App that needs to be translated
  313. * @returns array an array of available languages
  314. */
  315. public static function findAvailableLanguages($app=null) {
  316. $available=array('en');//english is always available
  317. $dir = self::findI18nDir($app);
  318. if(is_dir($dir)) {
  319. $files=scandir($dir);
  320. foreach($files as $file) {
  321. if(substr($file, -4, 4) == '.php') {
  322. $i = substr($file, 0, -4);
  323. $available[] = $i;
  324. }
  325. }
  326. }
  327. return $available;
  328. }
  329. public static function languageExists($app, $lang) {
  330. if ($lang == 'en') {//english is always available
  331. return true;
  332. }
  333. $dir = self::findI18nDir($app);
  334. if(is_dir($dir)) {
  335. return file_exists($dir.'/'.$lang.'.php');
  336. }
  337. return false;
  338. }
  339. }