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.

string.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. class OC_L10N_String implements JsonSerializable {
  28. /**
  29. * @var OC_L10N
  30. */
  31. protected $l10n;
  32. /**
  33. * @var string
  34. */
  35. protected $text;
  36. /**
  37. * @var array
  38. */
  39. protected $parameters;
  40. /**
  41. * @var integer
  42. */
  43. protected $count;
  44. /**
  45. * @param OC_L10N $l10n
  46. */
  47. public function __construct($l10n, $text, $parameters, $count = 1) {
  48. $this->l10n = $l10n;
  49. $this->text = $text;
  50. $this->parameters = $parameters;
  51. $this->count = $count;
  52. }
  53. public function __toString() {
  54. $translations = $this->l10n->getTranslations();
  55. $text = $this->text;
  56. if(array_key_exists($this->text, $translations)) {
  57. if(is_array($translations[$this->text])) {
  58. $fn = $this->l10n->getPluralFormFunction();
  59. $id = $fn($this->count);
  60. $text = $translations[$this->text][$id];
  61. }
  62. else{
  63. $text = $translations[$this->text];
  64. }
  65. }
  66. // Replace %n first (won't interfere with vsprintf)
  67. $text = str_replace('%n', $this->count, $text);
  68. return vsprintf($text, $this->parameters);
  69. }
  70. public function jsonSerialize() {
  71. return $this->__toString();
  72. }
  73. }