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.

251 lines
6.9 KiB

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