summaryrefslogtreecommitdiffstats
path: root/lib/private/L10N/L10N.php
blob: fc50e7a8b43580ff641481946e0578a0da6a1698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author Georg Ehrke <oc.list@georgehrke.com>
 * @author Joas Schilling <coding@schilljs.com>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 * @author Thomas Citharel <nextcloud@tcit.fr>
 *
 * @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/>
 *
 */

namespace OC\L10N;

use OCP\IL10N;
use OCP\L10N\IFactory;
use Punic\Calendar;
use Symfony\Component\Translation\PluralizationRules;

class L10N implements IL10N {

	/** @var IFactory */
	protected $factory;

	/** @var string App of this object */
	protected $app;

	/** @var string Language of this object */
	protected $lang;

	/** @var string Locale of this object */
	protected $locale;

	/** @var string Plural forms (string) */
	private $pluralFormString = 'nplurals=2; plural=(n != 1);';

	/** @var string Plural forms (function) */
	private $pluralFormFunction = null;

	/** @var string[] */
	private $translations = [];

	/**
	 * @param IFactory $factory
	 * @param string $app
	 * @param string $lang
	 * @param string $locale
	 * @param array $files
	 */
	public function __construct(IFactory $factory, $app, $lang, $locale, array $files) {
		$this->factory = $factory;
		$this->app = $app;
		$this->lang = $lang;
		$this->locale = $locale;

		foreach ($files as $languageFile) {
			$this->load($languageFile);
		}
	}

	/**
	 * The code (en, de, ...) of the language that is used for this instance
	 *
	 * @return string language
	 */
	public function getLanguageCode(): string {
		return $this->lang;
	}

	/**
	 * The code (en_US, fr_CA, ...) of the locale that is used for this instance
	 *
	 * @return string locale
	 */
	public function getLocaleCode(): string {
		return $this->locale;
	}

	/**
	 * Translating
	 * @param string $text The text we need a translation for
	 * @param array|string $parameters default:array() Parameters for sprintf
	 * @return string Translation or the same text
	 *
	 * Returns the translation. If no translation is found, $text will be
	 * returned.
	 */
	public function t(string $text, $parameters = []): string {
		if (!\is_array($parameters)) {
			$parameters = [$parameters];
		}

		return (string) new L10NString($this, $text, $parameters);
	}

	/**
	 * Translating
	 * @param string $text_singular the string to translate for exactly one object
	 * @param string $text_plural the string to translate for n objects
	 * @param integer $count Number of objects
	 * @param array $parameters default:array() Parameters for sprintf
	 * @return string Translation or the same text
	 *
	 * Returns the translation. If no translation is found, $text will be
	 * returned. %n will be replaced with the number of objects.
	 *
	 * The correct plural is determined by the plural_forms-function
	 * provided by the po file.
	 *
	 */
	public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
		$identifier = "_${text_singular}_::_${text_plural}_";
		if (isset($this->translations[$identifier])) {
			return (string) new L10NString($this, $identifier, $parameters, $count);
		}

		if ($count === 1) {
			return (string) new L10NString($this, $text_singular, $parameters, $count);
		}

		return (string) new L10NString($this, $text_plural, $parameters, $count);
	}

	/**
	 * Localization
	 * @param string $type Type of localization
	 * @param \DateTime|int|string $data parameters for this localization
	 * @param array $options
	 * @return string|int|false
	 *
	 * Returns the localized data.
	 *
	 * Implemented types:
	 *  - date
	 *    - Creates a date
	 *    - params: timestamp (int/string)
	 *  - datetime
	 *    - Creates date and time
	 *    - params: timestamp (int/string)
	 *  - time
	 *    - Creates a time
	 *    - params: timestamp (int/string)
	 *  - firstday: Returns the first day of the week (0 sunday - 6 saturday)
	 *  - jsdate: Returns the short JS date format
	 */
	public function l(string $type, $data = null, array $options = []) {
		if (null === $this->locale) {
			// Use the language of the instance
			$this->locale = $this->getLanguageCode();
		}
		if ($this->locale === 'sr@latin') {
			$this->locale = 'sr_latn';
		}

		if ($type === 'firstday') {
			return (int) Calendar::getFirstWeekday($this->locale);
		}
		if ($type === 'jsdate') {
			return (string) Calendar::getDateFormat('short', $this->locale);
		}

		$value = new \DateTime();
		if ($data instanceof \DateTime) {
			$value = $data;
		} elseif (\is_string($data) && !is_numeric($data)) {
			$data = strtotime($data);
			$value->setTimestamp($data);
		} elseif ($data !== null) {
			$data = (int)$data;
			$value->setTimestamp($data);
		}

		$options = array_merge(['width' => 'long'], $options);
		$width = $options['width'];
		switch ($type) {
			case 'date':
				return (string) Calendar::formatDate($value, $width, $this->locale);
			case 'datetime':
				return (string) Calendar::formatDatetime($value, $width, $this->locale);
			case 'time':
				return (string) Calendar::formatTime($value, $width, $this->locale);
			case 'weekdayName':
				return (string) Calendar::getWeekdayName($value, $width, $this->locale);
			default:
				return false;
		}
	}

	/**
	 * Returns an associative array with all translations
	 *
	 * Called by \OC_L10N_String
	 * @return array
	 */
	public function getTranslations(): array {
		return $this->translations;
	}

	/**
	 * Returnsed function accepts the argument $n
	 *
	 * Called by \OC_L10N_String
	 * @return \Closure the plural form function
	 */
	public function getPluralFormFunction(): \Closure {
		if (\is_null($this->pluralFormFunction)) {
			$lang = $this->getLanguageCode();
			$this->pluralFormFunction = function ($n) use ($lang) {
				return PluralizationRules::get($n, $lang);
			};
		}

		return $this->pluralFormFunction;
	}

	/**
	 * @param string $translationFile
	 * @return bool
	 */
	protected function load(string $translationFile): bool {
		$json = json_decode(file_get_contents($translationFile), true);
		if (!\is_array($json)) {
			$jsonError = json_last_error();
			\OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
			return false;
		}

		if (!empty($json['pluralForm'])) {
			$this->pluralFormString = $json['pluralForm'];
		}
		$this->translations = array_merge($this->translations, $json['translations']);
		return true;
	}
}