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.

89 lines
2.4 KiB

3 years ago
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jakob Sack <mail@jakobsack.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\L10N;
  32. class L10NString implements \JsonSerializable {
  33. /** @var \OC\L10N\L10N */
  34. protected $l10n;
  35. /** @var string */
  36. protected $text;
  37. /** @var array */
  38. protected $parameters;
  39. /** @var integer */
  40. protected $count;
  41. /**
  42. * @param \OC\L10N\L10N $l10n
  43. * @param string|string[] $text
  44. * @param array $parameters
  45. * @param int $count
  46. */
  47. public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) {
  48. $this->l10n = $l10n;
  49. $this->text = $text;
  50. $this->parameters = $parameters;
  51. $this->count = $count;
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function __toString() {
  57. $translations = $this->l10n->getTranslations();
  58. $text = $this->text;
  59. if (array_key_exists($this->text, $translations)) {
  60. if (is_array($translations[$this->text])) {
  61. $fn = $this->l10n->getPluralFormFunction();
  62. $id = $fn($this->count);
  63. $text = $translations[$this->text][$id];
  64. } else {
  65. $text = $translations[$this->text];
  66. }
  67. }
  68. // Replace %n first (won't interfere with vsprintf)
  69. $text = str_replace('%n', (string)$this->count, $text);
  70. return vsprintf($text, $this->parameters);
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function jsonSerialize() {
  76. return $this->__toString();
  77. }
  78. }