l10n.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. 'date' => 'd.m.Y',
  55. 'datetime' => 'd.m.Y H:i:s',
  56. 'time' => 'H:i:s');
  57. /**
  58. * get an L10N instance
  59. * @return OC_L10N
  60. */
  61. public static function get($app,$lang=null){
  62. if(is_null($lang)){
  63. if(!isset(self::$instances[$app])){
  64. self::$instances[$app]=new OC_L10N($app);
  65. }
  66. return self::$instances[$app];
  67. }else{
  68. return new OC_L10N($app,$lang);
  69. }
  70. }
  71. /**
  72. * @brief The constructor
  73. * @param $app the app requesting l10n
  74. * @param $lang default: null Language
  75. * @returns OC_L10N-Object
  76. *
  77. * If language is not set, the constructor tries to find the right
  78. * language.
  79. */
  80. public function __construct($app, $lang = null){
  81. $this->app = $app;
  82. $this->lang = $lang;
  83. }
  84. protected function init(){
  85. if ($this->app === true) {
  86. return;
  87. }
  88. $app = $this->app;
  89. $lang = $this->lang;
  90. $this->app = true;
  91. // Find the right language
  92. if(is_null($lang) || $lang == '') {
  93. $lang = self::findLanguage($app);
  94. }
  95. // Use cache if possible
  96. if(array_key_exists($app.'::'.$lang, self::$cache)){
  97. $this->translations = self::$cache[$app.'::'.$lang]['t'];
  98. $this->localizations = self::$cache[$app.'::'.$lang]['l'];
  99. }
  100. else{
  101. $i18ndir = self::findI18nDir($app);
  102. // Localization is in /l10n, Texts are in $i18ndir
  103. // (Just no need to define date/time format etc. twice)
  104. if((OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC_App::getAppPath($app).'/l10n/') ||
  105. OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC::$SERVERROOT.'/core/l10n/') ||
  106. OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC::$SERVERROOT.'/lib/l10n/') ||
  107. OC_Helper::issubdirectory($i18ndir.$lang.'.php', OC::$SERVERROOT.'/settings')) && file_exists($i18ndir.$lang.'.php')) {
  108. // Include the file, save the data from $CONFIG
  109. include(strip_tags($i18ndir).strip_tags($lang).'.php');
  110. if(isset($TRANSLATIONS) && is_array($TRANSLATIONS)){
  111. $this->translations = $TRANSLATIONS;
  112. }
  113. }
  114. if(file_exists(OC::$SERVERROOT.'/core/l10n/l10n-'.$lang.'.php')){
  115. // Include the file, save the data from $CONFIG
  116. include(OC::$SERVERROOT.'/core/l10n/l10n-'.$lang.'.php');
  117. if(isset($LOCALIZATIONS) && is_array($LOCALIZATIONS)){
  118. $this->localizations = array_merge($this->localizations, $LOCALIZATIONS);
  119. }
  120. }
  121. self::$cache[$app.'::'.$lang]['t'] = $this->translations;
  122. self::$cache[$app.'::'.$lang]['l'] = $this->localizations;
  123. }
  124. }
  125. /**
  126. * @brief Translating
  127. * @param $text The text we need a translation for
  128. * @param $parameters default:array() Parameters for sprintf
  129. * @returns Translation or the same text
  130. *
  131. * Returns the translation. If no translation is found, $text will be
  132. * returned.
  133. */
  134. public function t($text, $parameters = array()){
  135. return new OC_L10N_String($this, $text, $parameters);
  136. }
  137. /**
  138. * @brief Translating
  139. * @param $textArray The text array we need a translation for
  140. * @returns Translation or the same text
  141. *
  142. * Returns the translation. If no translation is found, $textArray will be
  143. * returned.
  144. *
  145. *
  146. * @deprecated deprecated since ownCloud version 5.0
  147. * This method will probably be removed with ownCloud 6.0
  148. *
  149. *
  150. */
  151. public function tA($textArray){
  152. OC_Log::write('core', 'DEPRECATED: the method tA is deprecated and will be removed soon.',OC_Log::WARN);
  153. $result = array();
  154. foreach($textArray as $key => $text){
  155. $result[$key] = (string)$this->t($text);
  156. }
  157. return $result;
  158. }
  159. /**
  160. * @brief getTranslations
  161. * @returns Fetch all translations
  162. *
  163. * Returns an associative array with all translations
  164. */
  165. public function getTranslations(){
  166. $this->init();
  167. return $this->translations;
  168. }
  169. /**
  170. * @brief Localization
  171. * @param $type Type of localization
  172. * @param $params parameters for this localization
  173. * @returns String or false
  174. *
  175. * Returns the localized data.
  176. *
  177. * Implemented types:
  178. * - date
  179. * - Creates a date
  180. * - l10n-field: date
  181. * - params: timestamp (int/string)
  182. * - datetime
  183. * - Creates date and time
  184. * - l10n-field: datetime
  185. * - params: timestamp (int/string)
  186. * - time
  187. * - Creates a time
  188. * - l10n-field: time
  189. * - params: timestamp (int/string)
  190. */
  191. public function l($type, $data){
  192. $this->init();
  193. switch($type){
  194. // If you add something don't forget to add it to $localizations
  195. // at the top of the page
  196. case 'date':
  197. case 'datetime':
  198. case 'time':
  199. if($data instanceof DateTime) return $data->format($this->localizations[$type]);
  200. elseif(is_string($data)) $data = strtotime($data);
  201. return date($this->localizations[$type], $data);
  202. break;
  203. default:
  204. return false;
  205. }
  206. }
  207. /**
  208. * @brief Choose a language
  209. * @param $texts Associative Array with possible strings
  210. * @returns String
  211. *
  212. * $text is an array 'de' => 'hallo welt', 'en' => 'hello world', ...
  213. *
  214. * This function is useful to avoid loading thousands of files if only one
  215. * simple string is needed, for example in appinfo.php
  216. */
  217. public static function selectLanguage($text){
  218. $lang = self::findLanguage(array_keys($text));
  219. return $text[$lang];
  220. }
  221. /**
  222. * @brief find the best language
  223. * @param $app Array or string, details below
  224. * @returns language
  225. *
  226. * If $app is an array, ownCloud assumes that these are the available
  227. * languages. Otherwise ownCloud tries to find the files in the l10n
  228. * folder.
  229. *
  230. * If nothing works it returns 'en'
  231. */
  232. public static function findLanguage($app = null){
  233. if(!is_array($app) && self::$language != ''){
  234. return self::$language;
  235. }
  236. if(OC_User::getUser() && OC_Preferences::getValue(OC_User::getUser(), 'core', 'lang')){
  237. $lang = OC_Preferences::getValue(OC_User::getUser(), 'core', 'lang');
  238. self::$language = $lang;
  239. if(is_array($app)){
  240. $available = $app;
  241. $lang_exists = array_search($lang, $available) !== false;
  242. }
  243. else {
  244. $lang_exists = self::languageExists($app, $lang);
  245. }
  246. if($lang_exists){
  247. return $lang;
  248. }
  249. }
  250. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  251. $accepted_languages = preg_split('/,\s*/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  252. if(is_array($app)){
  253. $available = $app;
  254. }
  255. else{
  256. $available = self::findAvailableLanguages($app);
  257. }
  258. foreach($accepted_languages as $i){
  259. $temp = explode(';', $i);
  260. if(array_search($temp[0], $available) !== false){
  261. return $temp[0];
  262. }
  263. }
  264. }
  265. // Last try: English
  266. return 'en';
  267. }
  268. /**
  269. * @brief find the l10n directory
  270. * @param $app App that needs to be translated
  271. * @returns directory
  272. */
  273. protected static function findI18nDir($app){
  274. // find the i18n dir
  275. $i18ndir = OC::$SERVERROOT.'/core/l10n/';
  276. if($app != ''){
  277. // Check if the app is in the app folder
  278. if(file_exists(OC_App::getAppPath($app).'/l10n/')){
  279. $i18ndir = OC_App::getAppPath($app).'/l10n/';
  280. }
  281. else{
  282. $i18ndir = OC::$SERVERROOT.'/'.$app.'/l10n/';
  283. }
  284. }
  285. return $i18ndir;
  286. }
  287. /**
  288. * @brief find all available languages for an app
  289. * @param $app App that needs to be translated
  290. * @returns array an array of available languages
  291. */
  292. public static function findAvailableLanguages($app=null){
  293. $available=array('en');//english is always available
  294. $dir = self::findI18nDir($app);
  295. if(is_dir($dir)){
  296. $files=scandir($dir);
  297. foreach($files as $file){
  298. if(substr($file, -4, 4) == '.php'){
  299. $i = substr($file, 0, -4);
  300. $available[] = $i;
  301. }
  302. }
  303. }
  304. return $available;
  305. }
  306. public static function languageExists($app, $lang){
  307. if ($lang == 'en'){//english is always available
  308. return true;
  309. }
  310. $dir = self::findI18nDir($app);
  311. if(is_dir($dir)){
  312. return file_exists($dir.'/'.$lang.'.php');
  313. }
  314. return false;
  315. }
  316. }