You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

l10n.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.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. * cache
  28. */
  29. protected static $cache = array();
  30. /**
  31. * The best language
  32. */
  33. protected static $language = '';
  34. /**
  35. * Translations
  36. */
  37. private $translations = array();
  38. /**
  39. * Localization
  40. */
  41. private $localizations = array(
  42. 'date' => 'd.m.Y',
  43. 'datetime' => 'd.m.Y H:i:s',
  44. 'time' => 'H:i:s' );
  45. /**
  46. * @brief The constructor
  47. * @param $app the app requesting l10n
  48. * @param $lang default: null Language
  49. * @returns OC_L10N-Object
  50. *
  51. * If language is not set, the constructor tries to find the right
  52. * language.
  53. */
  54. public function __construct( $app, $lang = null ){
  55. global $SERVERROOT;
  56. // Find the right language
  57. if( is_null( $lang )){
  58. $lang = self::findLanguage( $app );
  59. }
  60. // Use cache if possible
  61. if(array_key_exists($app.'::'.$lang, self::$cache )){
  62. $this->translations = self::$cache[$app.'::'.$lang]['t'];
  63. $this->localizations = self::$cache[$app.'::'.$lang]['l'];
  64. }
  65. else{
  66. $i18ndir = self::findI18nDir( $app );
  67. // Localization is in /l10n, Texts are in $i18ndir
  68. // (Just no need to define date/time format etc. twice)
  69. if( file_exists( $i18ndir.$lang.'.php' )){
  70. // Include the file, save the data from $CONFIG
  71. include( $i18ndir.$lang.'.php' );
  72. if( isset( $TRANSLATIONS ) && is_array( $TRANSLATIONS )){
  73. $this->translations = $TRANSLATIONS;
  74. }
  75. }
  76. if( file_exists( $SERVERROOT.'/core/l10n/l10n-'.$lang.'.php' )){
  77. // Include the file, save the data from $CONFIG
  78. include( $SERVERROOT.'/core/l10n/l10n-'.$lang.'.php' );
  79. if( isset( $LOCALIZATIONS ) && is_array( $LOCALIZATIONS )){
  80. $this->localizations = array_merge( $this->localizations, $LOCALIZATIONS );
  81. }
  82. }
  83. self::$cache[$app.'::'.$lang]['t'] = $this->translations;
  84. self::$cache[$app.'::'.$lang]['l'] = $this->localizations;
  85. }
  86. }
  87. /**
  88. * @brief Translating
  89. * @param $text The text we need a translation for
  90. * @returns Translation or the same text
  91. *
  92. * Returns the translation. If no translation is found, $text will be
  93. * returned.
  94. */
  95. public function t($text){
  96. if(array_key_exists($text, $this->translations)){
  97. return $this->translations[$text];
  98. }
  99. return $text;
  100. }
  101. /**
  102. * @brief getTranslations
  103. * @returns Fetch all translations
  104. *
  105. * Returns an associative array with all translations
  106. */
  107. public function getTranslations(){
  108. return $this->translations;
  109. }
  110. /**
  111. * @brief Localization
  112. * @param $type Type of localization
  113. * @param $params parameters for this localization
  114. * @returns String or false
  115. *
  116. * Returns the localized data.
  117. *
  118. * Implemented types:
  119. * - date
  120. * - Creates a date
  121. * - l10n-field: date
  122. * - params: timestamp (int/string)
  123. * - datetime
  124. * - Creates date and time
  125. * - l10n-field: datetime
  126. * - params: timestamp (int/string)
  127. * - time
  128. * - Creates a time
  129. * - l10n-field: time
  130. * - params: timestamp (int/string)
  131. */
  132. public function l($type, $data){
  133. switch($type){
  134. // If you add something don't forget to add it to $localizations
  135. // at the top of the page
  136. case 'date':
  137. if( is_string( $data )) $data = strtotime( $data );
  138. return date( $this->localizations['date'], $data );
  139. break;
  140. case 'datetime':
  141. if( is_string( $data )) $data = strtotime( $data );
  142. return date( $this->localizations['datetime'], $data );
  143. break;
  144. case 'time':
  145. if( is_string( $data )) $data = strtotime( $data );
  146. return date( $this->localizations['time'], $data );
  147. break;
  148. default:
  149. return false;
  150. }
  151. }
  152. /**
  153. * @brief Choose a language
  154. * @param $texts Associative Array with possible strings
  155. * @returns String
  156. *
  157. * $text is an array 'de' => 'hallo welt', 'en' => 'hello world', ...
  158. *
  159. * This function is useful to avoid loading thousands of files if only one
  160. * simple string is needed, for example in appinfo.php
  161. */
  162. public static function selectLanguage( $text ){
  163. $lang = self::findLanguage( array_keys( $text ));
  164. return $text[$lang];
  165. }
  166. /**
  167. * @brief find the best language
  168. * @param $app Array or string, details below
  169. * @returns language
  170. *
  171. * If $app is an array, ownCloud assumes that these are the available
  172. * languages. Otherwise ownCloud tries to find the files in the l10n
  173. * folder.
  174. *
  175. * If nothing works it returns 'en'
  176. */
  177. public static function findLanguage( $app = null ){
  178. if( !is_array( $app) && self::$language != '' ){
  179. return self::$language;
  180. }
  181. $available = array();
  182. if( is_array( $app )){
  183. $available = $app;
  184. }
  185. else{
  186. $available=self::findAvailableLanguages( $app );
  187. }
  188. if( OC_User::getUser() && OC_Preferences::getValue( OC_User::getUser(), 'core', 'lang' )){
  189. $lang = OC_Preferences::getValue( OC_User::getUser(), 'core', 'lang' );
  190. self::$language = $lang;
  191. if( array_search( $lang, $available ) !== false ){
  192. return $lang;
  193. }
  194. }
  195. if( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] )){
  196. $accepted_languages = preg_split( '/,\s*/', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
  197. foreach( $accepted_languages as $i ){
  198. $temp = explode( ';', $i );
  199. if( array_search( $temp[0], $available ) !== false ){
  200. return $temp[0];
  201. }
  202. }
  203. }
  204. // Last try: English
  205. return 'en';
  206. }
  207. /**
  208. * @brief find the l10n directory
  209. * @param $app App that needs to be translated
  210. * @returns directory
  211. */
  212. protected static function findI18nDir( $app ){
  213. global $SERVERROOT;
  214. // find the i18n dir
  215. $i18ndir = $SERVERROOT.'/core/l10n/';
  216. if( $app != '' ){
  217. // Check if the app is in the app folder
  218. if( file_exists( $SERVERROOT.'/apps/'.$app.'/l10n/' )){
  219. $i18ndir = $SERVERROOT.'/apps/'.$app.'/l10n/';
  220. }
  221. else{
  222. $i18ndir = $SERVERROOT.'/'.$app.'/l10n/';
  223. }
  224. }
  225. return $i18ndir;
  226. }
  227. /**
  228. * @brief find all available languages for an app
  229. * @param $app App that needs to be translated
  230. * @returns array an array of available languages
  231. */
  232. public static function findAvailableLanguages( $app=null ){
  233. $available=array('en');//english is always available
  234. $dir = self::findI18nDir( $app );
  235. if( file_exists($dir)){
  236. $dh = opendir($dir);
  237. while(( $file = readdir( $dh )) !== false ){
  238. if( substr( $file, -4, 4 ) == '.php' and strlen($file)==6 ){
  239. $i = substr( $file, 0, -4 );
  240. if( $i != '' ){
  241. $available[] = $i;
  242. }
  243. }
  244. }
  245. closedir($dh);
  246. }
  247. return $available;
  248. }
  249. }