l10n.php 9.3 KB

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