string.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Morris Jobke <hey@morrisjobke.de>
  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. class OC_L10N_String implements JsonSerializable {
  29. /** @var \OC\L10N\L10N */
  30. protected $l10n;
  31. /** @var string */
  32. protected $text;
  33. /** @var array */
  34. protected $parameters;
  35. /** @var integer */
  36. protected $count;
  37. /**
  38. * @param \OC\L10N\L10N $l10n
  39. * @param string|string[] $text
  40. * @param array $parameters
  41. * @param int $count
  42. */
  43. public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) {
  44. $this->l10n = $l10n;
  45. $this->text = $text;
  46. $this->parameters = $parameters;
  47. $this->count = $count;
  48. }
  49. public function __toString() {
  50. $translations = $this->l10n->getTranslations();
  51. $text = $this->text;
  52. if(array_key_exists($this->text, $translations)) {
  53. if(is_array($translations[$this->text])) {
  54. $fn = $this->l10n->getPluralFormFunction();
  55. $id = $fn($this->count);
  56. $text = $translations[$this->text][$id];
  57. }
  58. else{
  59. $text = $translations[$this->text];
  60. }
  61. }
  62. // Replace %n first (won't interfere with vsprintf)
  63. $text = str_replace('%n', $this->count, $text);
  64. return vsprintf($text, $this->parameters);
  65. }
  66. public function jsonSerialize() {
  67. return $this->__toString();
  68. }
  69. }