L10N.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\IdentityTranslator;
  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 IdentityTranslator */
  42. private $identityTranslator;
  43. /** @var string[] */
  44. private $translations = [];
  45. /**
  46. * @param IFactory $factory
  47. * @param string $app
  48. * @param string $lang
  49. * @param string $locale
  50. * @param array $files
  51. */
  52. public function __construct(IFactory $factory, $app, $lang, $locale, array $files) {
  53. $this->factory = $factory;
  54. $this->app = $app;
  55. $this->lang = $lang;
  56. $this->locale = $locale;
  57. foreach ($files as $languageFile) {
  58. $this->load($languageFile);
  59. }
  60. }
  61. /**
  62. * The code (en, de, ...) of the language that is used for this instance
  63. *
  64. * @return string language
  65. */
  66. public function getLanguageCode(): string {
  67. return $this->lang;
  68. }
  69. /**
  70. * The code (en_US, fr_CA, ...) of the locale that is used for this instance
  71. *
  72. * @return string locale
  73. */
  74. public function getLocaleCode(): string {
  75. return $this->locale;
  76. }
  77. /**
  78. * Translating
  79. * @param string $text The text we need a translation for
  80. * @param array|string $parameters default:array() Parameters for sprintf
  81. * @return string Translation or the same text
  82. *
  83. * Returns the translation. If no translation is found, $text will be
  84. * returned.
  85. */
  86. public function t(string $text, $parameters = []): string {
  87. if (!\is_array($parameters)) {
  88. $parameters = [$parameters];
  89. }
  90. return (string) new L10NString($this, $text, $parameters);
  91. }
  92. /**
  93. * Translating
  94. * @param string $text_singular the string to translate for exactly one object
  95. * @param string $text_plural the string to translate for n objects
  96. * @param integer $count Number of objects
  97. * @param array $parameters default:array() Parameters for sprintf
  98. * @return string Translation or the same text
  99. *
  100. * Returns the translation. If no translation is found, $text will be
  101. * returned. %n will be replaced with the number of objects.
  102. *
  103. * The correct plural is determined by the plural_forms-function
  104. * provided by the po file.
  105. *
  106. */
  107. public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
  108. $identifier = "_{$text_singular}_::_{$text_plural}_";
  109. if (isset($this->translations[$identifier])) {
  110. return (string) new L10NString($this, $identifier, $parameters, $count);
  111. }
  112. if ($count === 1) {
  113. return (string) new L10NString($this, $text_singular, $parameters, $count);
  114. }
  115. return (string) new L10NString($this, $text_plural, $parameters, $count);
  116. }
  117. /**
  118. * Localization
  119. * @param string $type Type of localization
  120. * @param \DateTime|int|string $data parameters for this localization
  121. * @param array $options
  122. * @return string|int|false
  123. *
  124. * Returns the localized data.
  125. *
  126. * Implemented types:
  127. * - date
  128. * - Creates a date
  129. * - params: timestamp (int/string)
  130. * - datetime
  131. * - Creates date and time
  132. * - params: timestamp (int/string)
  133. * - time
  134. * - Creates a time
  135. * - params: timestamp (int/string)
  136. * - firstday: Returns the first day of the week (0 sunday - 6 saturday)
  137. * - jsdate: Returns the short JS date format
  138. */
  139. public function l(string $type, $data = null, array $options = []) {
  140. if (null === $this->locale) {
  141. // Use the language of the instance
  142. $this->locale = $this->getLanguageCode();
  143. }
  144. if ($this->locale === 'sr@latin') {
  145. $this->locale = 'sr_latn';
  146. }
  147. if ($type === 'firstday') {
  148. return (int) Calendar::getFirstWeekday($this->locale);
  149. }
  150. if ($type === 'jsdate') {
  151. return (string) Calendar::getDateFormat('short', $this->locale);
  152. }
  153. $value = new \DateTime();
  154. if ($data instanceof \DateTime) {
  155. $value = $data;
  156. } elseif (\is_string($data) && !is_numeric($data)) {
  157. $data = strtotime($data);
  158. $value->setTimestamp($data);
  159. } elseif ($data !== null) {
  160. $data = (int)$data;
  161. $value->setTimestamp($data);
  162. }
  163. $options = array_merge(['width' => 'long'], $options);
  164. $width = $options['width'];
  165. switch ($type) {
  166. case 'date':
  167. return (string) Calendar::formatDate($value, $width, $this->locale);
  168. case 'datetime':
  169. return (string) Calendar::formatDatetime($value, $width, $this->locale);
  170. case 'time':
  171. return (string) Calendar::formatTime($value, $width, $this->locale);
  172. case 'weekdayName':
  173. return (string) Calendar::getWeekdayName($value, $width, $this->locale);
  174. default:
  175. return false;
  176. }
  177. }
  178. /**
  179. * Returns an associative array with all translations
  180. *
  181. * Called by \OC_L10N_String
  182. * @return array
  183. */
  184. public function getTranslations(): array {
  185. return $this->translations;
  186. }
  187. /**
  188. * @internal
  189. * @return IdentityTranslator
  190. */
  191. public function getIdentityTranslator(): IdentityTranslator {
  192. if (\is_null($this->identityTranslator)) {
  193. $this->identityTranslator = new IdentityTranslator();
  194. $this->identityTranslator->setLocale($this->getLocaleCode());
  195. }
  196. return $this->identityTranslator;
  197. }
  198. /**
  199. * @param string $translationFile
  200. * @return bool
  201. */
  202. protected function load(string $translationFile): bool {
  203. $json = json_decode(file_get_contents($translationFile), true);
  204. if (!\is_array($json)) {
  205. $jsonError = json_last_error();
  206. \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
  207. return false;
  208. }
  209. $this->translations = array_merge($this->translations, $json['translations']);
  210. return true;
  211. }
  212. }