1
0

L10NString.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\L10N;
  31. class L10NString implements \JsonSerializable {
  32. /** @var \OC\L10N\L10N */
  33. protected $l10n;
  34. /** @var string */
  35. protected $text;
  36. /** @var array */
  37. protected $parameters;
  38. /** @var integer */
  39. protected $count;
  40. /**
  41. * @param \OC\L10N\L10N $l10n
  42. * @param string|string[] $text
  43. * @param array $parameters
  44. * @param int $count
  45. */
  46. public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) {
  47. $this->l10n = $l10n;
  48. $this->text = $text;
  49. $this->parameters = $parameters;
  50. $this->count = $count;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function __toString() {
  56. $translations = $this->l10n->getTranslations();
  57. $text = $this->text;
  58. if(array_key_exists($this->text, $translations)) {
  59. if(is_array($translations[$this->text])) {
  60. $fn = $this->l10n->getPluralFormFunction();
  61. $id = $fn($this->count);
  62. $text = $translations[$this->text][$id];
  63. }
  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. }