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.

L10N.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 Psr\Log\LoggerInterface;
  31. use Punic\Calendar;
  32. use Symfony\Component\Translation\IdentityTranslator;
  33. class L10N implements IL10N {
  34. /** @var IFactory */
  35. protected $factory;
  36. /** @var string App of this object */
  37. protected $app;
  38. /** @var string Language of this object */
  39. protected $lang;
  40. /** @var string Locale of this object */
  41. protected $locale;
  42. /** @var IdentityTranslator */
  43. private $identityTranslator;
  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. } elseif (\is_string($data) && !is_numeric($data)) {
  158. $data = strtotime($data);
  159. $value->setTimestamp($data);
  160. } elseif ($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. * @internal
  190. * @return IdentityTranslator
  191. */
  192. public function getIdentityTranslator(): IdentityTranslator {
  193. if (\is_null($this->identityTranslator)) {
  194. $this->identityTranslator = new IdentityTranslator();
  195. // We need to use the language code here instead of the locale,
  196. // because Symfony does not distinguish between the two and would
  197. // otherwise e.g. with locale "Czech" and language "German" try to
  198. // pick a non-existing plural rule, because Czech has 4 plural forms
  199. // and German only 2.
  200. $this->identityTranslator->setLocale($this->getLanguageCode());
  201. }
  202. return $this->identityTranslator;
  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. \OCP\Server::get(LoggerInterface::class)->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
  213. return false;
  214. }
  215. $this->translations = array_merge($this->translations, $json['translations']);
  216. return true;
  217. }
  218. }