L10N.php 6.8 KB

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