L10N.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program 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 License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\L10N;
  23. use OCP\IL10N;
  24. use OCP\L10N\IFactory;
  25. use Punic\Calendar;
  26. class L10N implements IL10N {
  27. /** @var IFactory */
  28. protected $factory;
  29. /** @var string App of this object */
  30. protected $app;
  31. /** @var string Language of this object */
  32. protected $lang;
  33. /** @var string Plural forms (string) */
  34. private $pluralFormString = 'nplurals=2; plural=(n != 1);';
  35. /** @var string Plural forms (function) */
  36. private $pluralFormFunction = null;
  37. /** @var string[] */
  38. private $translations = [];
  39. /**
  40. * @param IFactory $factory
  41. * @param string $app
  42. * @param string $lang
  43. * @param array $files
  44. */
  45. public function __construct(IFactory $factory, $app, $lang, array $files) {
  46. $this->factory = $factory;
  47. $this->app = $app;
  48. $this->lang = $lang;
  49. $this->translations = [];
  50. foreach ($files as $languageFile) {
  51. $this->load($languageFile);
  52. }
  53. }
  54. /**
  55. * The code (en, de, ...) of the language that is used for this instance
  56. *
  57. * @return string language
  58. */
  59. public function getLanguageCode() {
  60. return $this->lang;
  61. }
  62. /**
  63. * Translating
  64. * @param string $text The text we need a translation for
  65. * @param array $parameters default:array() Parameters for sprintf
  66. * @return string Translation or the same text
  67. *
  68. * Returns the translation. If no translation is found, $text will be
  69. * returned.
  70. */
  71. public function t($text, $parameters = array()) {
  72. return (string) new \OC_L10N_String($this, $text, $parameters);
  73. }
  74. /**
  75. * Translating
  76. * @param string $text_singular the string to translate for exactly one object
  77. * @param string $text_plural the string to translate for n objects
  78. * @param integer $count Number of objects
  79. * @param array $parameters default:array() Parameters for sprintf
  80. * @return string Translation or the same text
  81. *
  82. * Returns the translation. If no translation is found, $text will be
  83. * returned. %n will be replaced with the number of objects.
  84. *
  85. * The correct plural is determined by the plural_forms-function
  86. * provided by the po file.
  87. *
  88. */
  89. public function n($text_singular, $text_plural, $count, $parameters = array()) {
  90. $identifier = "_${text_singular}_::_${text_plural}_";
  91. if (isset($this->translations[$identifier])) {
  92. return (string) new \OC_L10N_String($this, $identifier, $parameters, $count);
  93. } else {
  94. if ($count === 1) {
  95. return (string) new \OC_L10N_String($this, $text_singular, $parameters, $count);
  96. } else {
  97. return (string) new \OC_L10N_String($this, $text_plural, $parameters, $count);
  98. }
  99. }
  100. }
  101. /**
  102. * Localization
  103. * @param string $type Type of localization
  104. * @param \DateTime|int|string $data parameters for this localization
  105. * @param array $options
  106. * @return string|int|false
  107. *
  108. * Returns the localized data.
  109. *
  110. * Implemented types:
  111. * - date
  112. * - Creates a date
  113. * - params: timestamp (int/string)
  114. * - datetime
  115. * - Creates date and time
  116. * - params: timestamp (int/string)
  117. * - time
  118. * - Creates a time
  119. * - params: timestamp (int/string)
  120. * - firstday: Returns the first day of the week (0 sunday - 6 saturday)
  121. * - jsdate: Returns the short JS date format
  122. */
  123. public function l($type, $data = null, $options = array()) {
  124. // Use the language of the instance
  125. $locale = $this->getLanguageCode();
  126. if ($locale === 'sr@latin') {
  127. $locale = 'sr_latn';
  128. }
  129. if ($type === 'firstday') {
  130. return (int) Calendar::getFirstWeekday($locale);
  131. }
  132. if ($type === 'jsdate') {
  133. return (string) Calendar::getDateFormat('short', $locale);
  134. }
  135. $value = new \DateTime();
  136. if ($data instanceof \DateTime) {
  137. $value = $data;
  138. } else if (is_string($data) && !is_numeric($data)) {
  139. $data = strtotime($data);
  140. $value->setTimestamp($data);
  141. } else if ($data !== null) {
  142. $value->setTimestamp($data);
  143. }
  144. $options = array_merge(array('width' => 'long'), $options);
  145. $width = $options['width'];
  146. switch ($type) {
  147. case 'date':
  148. return (string) Calendar::formatDate($value, $width, $locale);
  149. case 'datetime':
  150. return (string) Calendar::formatDatetime($value, $width, $locale);
  151. case 'time':
  152. return (string) Calendar::formatTime($value, $width, $locale);
  153. default:
  154. return false;
  155. }
  156. }
  157. /**
  158. * Returns an associative array with all translations
  159. *
  160. * Called by \OC_L10N_String
  161. * @return array
  162. */
  163. public function getTranslations() {
  164. return $this->translations;
  165. }
  166. /**
  167. * Returnsed function accepts the argument $n
  168. *
  169. * Called by \OC_L10N_String
  170. * @return string the plural form function
  171. */
  172. public function getPluralFormFunction() {
  173. if (is_null($this->pluralFormFunction)) {
  174. $this->pluralFormFunction = $this->factory->createPluralFunction($this->pluralFormString);
  175. }
  176. return $this->pluralFormFunction;
  177. }
  178. /**
  179. * @param $translationFile
  180. * @return bool
  181. */
  182. protected function load($translationFile) {
  183. $json = json_decode(file_get_contents($translationFile), true);
  184. if (!is_array($json)) {
  185. $jsonError = json_last_error();
  186. \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
  187. return false;
  188. }
  189. if (!empty($json['pluralForm'])) {
  190. $this->pluralFormString = $json['pluralForm'];
  191. }
  192. $this->translations = array_merge($this->translations, $json['translations']);
  193. return true;
  194. }
  195. }