L10NString.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\L10N;
  29. class L10NString implements \JsonSerializable {
  30. /** @var L10N */
  31. protected $l10n;
  32. /** @var string */
  33. protected $text;
  34. /** @var array */
  35. protected $parameters;
  36. /** @var integer */
  37. protected $count;
  38. /**
  39. * @param L10N $l10n
  40. * @param string|string[] $text
  41. * @param array $parameters
  42. * @param int $count
  43. */
  44. public function __construct(L10N $l10n, $text, array $parameters, int $count = 1) {
  45. $this->l10n = $l10n;
  46. $this->text = $text;
  47. $this->parameters = $parameters;
  48. $this->count = $count;
  49. }
  50. public function __toString(): string {
  51. $translations = $this->l10n->getTranslations();
  52. $identityTranslator = $this->l10n->getIdentityTranslator();
  53. // Use the indexed version as per \Symfony\Contracts\Translation\TranslatorInterface
  54. $identity = $this->text;
  55. if (array_key_exists($this->text, $translations)) {
  56. $identity = $translations[$this->text];
  57. }
  58. if (is_array($identity)) {
  59. $pipeCheck = implode('', $identity);
  60. if (str_contains($pipeCheck, '|')) {
  61. return 'Can not use pipe character in translations';
  62. }
  63. $identity = implode('|', $identity);
  64. } elseif (str_contains($identity, '|')) {
  65. return 'Can not use pipe character in translations';
  66. }
  67. $beforeIdentity = $identity;
  68. $identity = str_replace('%n', '%count%', $identity);
  69. $parameters = [];
  70. if ($beforeIdentity !== $identity) {
  71. $parameters = ['%count%' => $this->count];
  72. }
  73. // $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface
  74. $text = $identityTranslator->trans($identity, $parameters);
  75. return vsprintf($text, $this->parameters);
  76. }
  77. public function jsonSerialize(): string {
  78. return $this->__toString();
  79. }
  80. }