l10n.php 7.2 KB

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