string.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_L10N_String{
  9. /**
  10. * @var OC_L10N
  11. */
  12. protected $l10n;
  13. /**
  14. * @var string
  15. */
  16. protected $text;
  17. /**
  18. * @var array
  19. */
  20. protected $parameters;
  21. /**
  22. * @var integer
  23. */
  24. protected $count;
  25. public function __construct($l10n, $text, $parameters, $count = 1) {
  26. $this->l10n = $l10n;
  27. $this->text = $text;
  28. $this->parameters = $parameters;
  29. $this->count = $count;
  30. }
  31. public function __toString() {
  32. $translations = $this->l10n->getTranslations();
  33. $text = $this->text;
  34. if(array_key_exists($this->text, $translations)) {
  35. if(is_array($translations[$this->text])) {
  36. $fn = $this->l10n->getPluralFormFunction();
  37. $id = $fn($this->count);
  38. $text = $translations[$this->text][$id];
  39. }
  40. else{
  41. $text = $translations[$this->text];
  42. }
  43. }
  44. // Replace %n first (won't interfere with vsprintf)
  45. $text = str_replace('%n', $this->count, $text);
  46. return vsprintf($text, $this->parameters);
  47. }
  48. }