string.php 2.16 KB
Newer Older
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
3
 * @author Bart Visscher <bartv@thisnet.nl>
4
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
5
6
7
8
9
10
 * @author Jakob Sack <mail@jakobsack.de>
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Jörn Friedrich Dreyer <jfd@butonic.de>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 *
Thomas Müller's avatar
Thomas Müller committed
11
 * @copyright Copyright (c) 2016, ownCloud, Inc.
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * 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|\OC\L10N\L10N */
30
	protected $l10n;
31

32
	/** @var string */
33
34
	protected $text;

35
	/** @var array */
36
37
	protected $parameters;

38
	/** @var integer */
39
40
	protected $count;

41
	/**
42
43
44
45
	 * @param \OC_L10N|\OC\L10N\L10N $l10n
	 * @param string|string[] $text
	 * @param array $parameters
	 * @param int $count
46
	 */
47
	public function __construct($l10n, $text, $parameters, $count = 1) {
48
49
50
		$this->l10n = $l10n;
		$this->text = $text;
		$this->parameters = $parameters;
51
		$this->count = $count;
52
53
	}

Thomas Mueller's avatar
Thomas Mueller committed
54
	public function __toString() {
55
		$translations = $this->l10n->getTranslations();
56
57

		$text = $this->text;
Thomas Mueller's avatar
Thomas Mueller committed
58
		if(array_key_exists($this->text, $translations)) {
59
			if(is_array($translations[$this->text])) {
60
61
				$fn = $this->l10n->getPluralFormFunction();
				$id = $fn($this->count);
62
				$text = $translations[$this->text][$id];
63
64
65
66
			}
			else{
				$text = $translations[$this->text];
			}
67
		}
68

69
70
71
		// Replace %n first (won't interfere with vsprintf)
		$text = str_replace('%n', $this->count, $text);
		return vsprintf($text, $this->parameters);
72
	}
73
74
75
76
77


	public function jsonSerialize() {
		return $this->__toString();
	}
78
}