2016-01-15 12:30:13 +00:00
|
|
|
<?php
|
2018-02-21 13:32:04 +00:00
|
|
|
declare(strict_types=1);
|
2016-01-15 12:30:13 +00:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 15:07:57 +00:00
|
|
|
*
|
2017-11-06 19:15:27 +00:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-07-05 15:37:12 +00:00
|
|
|
* @author Thomas Citharel <tcit@tcit.fr>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 15:07:57 +00:00
|
|
|
*
|
2016-01-15 12:30:13 +00:00
|
|
|
* @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;
|
2017-08-07 13:50:00 +00:00
|
|
|
use Symfony\Component\Translation\PluralizationRules;
|
2016-01-15 12:30:13 +00:00
|
|
|
|
|
|
|
class L10N implements IL10N {
|
|
|
|
|
|
|
|
/** @var IFactory */
|
|
|
|
protected $factory;
|
|
|
|
|
|
|
|
/** @var string App of this object */
|
|
|
|
protected $app;
|
|
|
|
|
|
|
|
/** @var string Language of this object */
|
|
|
|
protected $lang;
|
|
|
|
|
2017-07-05 15:37:12 +00:00
|
|
|
/** @var string Locale of this object */
|
|
|
|
protected $locale;
|
|
|
|
|
2016-01-15 12:30:13 +00:00
|
|
|
/** @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
|
2017-07-05 15:37:12 +00:00
|
|
|
* @param string $locale
|
2016-01-15 12:30:13 +00:00
|
|
|
* @param array $files
|
|
|
|
*/
|
2017-07-05 15:37:12 +00:00
|
|
|
public function __construct(IFactory $factory, $app, $lang, $locale, array $files) {
|
2016-01-15 12:30:13 +00:00
|
|
|
$this->factory = $factory;
|
|
|
|
$this->app = $app;
|
|
|
|
$this->lang = $lang;
|
2017-07-05 15:37:12 +00:00
|
|
|
$this->locale = $locale;
|
2016-01-15 12:30:13 +00:00
|
|
|
|
|
|
|
foreach ($files as $languageFile) {
|
|
|
|
$this->load($languageFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The code (en, de, ...) of the language that is used for this instance
|
|
|
|
*
|
|
|
|
* @return string language
|
|
|
|
*/
|
2018-02-21 13:32:04 +00:00
|
|
|
public function getLanguageCode(): string {
|
2016-01-15 12:30:13 +00:00
|
|
|
return $this->lang;
|
|
|
|
}
|
|
|
|
|
2017-07-05 15:37:12 +00:00
|
|
|
/**
|
|
|
|
* The code (en_US, fr_CA, ...) of the locale that is used for this instance
|
|
|
|
*
|
|
|
|
* @return string locale
|
|
|
|
*/
|
2018-06-28 15:37:16 +00:00
|
|
|
public function getLocaleCode(): string {
|
2017-07-05 15:37:12 +00:00
|
|
|
return $this->locale;
|
|
|
|
}
|
|
|
|
|
2016-01-15 12:30:13 +00:00
|
|
|
/**
|
|
|
|
* Translating
|
|
|
|
* @param string $text The text we need a translation for
|
2018-02-22 11:37:29 +00:00
|
|
|
* @param array|string $parameters default:array() Parameters for sprintf
|
2016-01-15 12:30:13 +00:00
|
|
|
* @return string Translation or the same text
|
|
|
|
*
|
|
|
|
* Returns the translation. If no translation is found, $text will be
|
|
|
|
* returned.
|
|
|
|
*/
|
2018-02-22 11:37:29 +00:00
|
|
|
public function t(string $text, $parameters = []): string {
|
|
|
|
if (!\is_array($parameters)) {
|
|
|
|
$parameters = [$parameters];
|
|
|
|
}
|
|
|
|
|
2017-07-24 18:17:20 +00:00
|
|
|
return (string) new L10NString($this, $text, $parameters);
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2018-02-21 13:32:04 +00:00
|
|
|
public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
|
2016-01-15 12:30:13 +00:00
|
|
|
$identifier = "_${text_singular}_::_${text_plural}_";
|
|
|
|
if (isset($this->translations[$identifier])) {
|
2017-07-24 18:17:20 +00:00
|
|
|
return (string) new L10NString($this, $identifier, $parameters, $count);
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
2018-02-21 13:32:04 +00:00
|
|
|
|
|
|
|
if ($count === 1) {
|
|
|
|
return (string) new L10NString($this, $text_singular, $parameters, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) new L10NString($this, $text_plural, $parameters, $count);
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-02-21 13:32:04 +00:00
|
|
|
public function l(string $type, $data = null, array $options = []) {
|
2017-07-05 15:37:12 +00:00
|
|
|
if (null === $this->locale) {
|
|
|
|
// Use the language of the instance
|
|
|
|
$this->locale = $this->getLanguageCode();
|
|
|
|
}
|
|
|
|
if ($this->locale === 'sr@latin') {
|
|
|
|
$this->locale = 'sr_latn';
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === 'firstday') {
|
2017-07-05 15:37:12 +00:00
|
|
|
return (int) Calendar::getFirstWeekday($this->locale);
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
|
|
|
if ($type === 'jsdate') {
|
2017-07-05 15:37:12 +00:00
|
|
|
return (string) Calendar::getDateFormat('short', $this->locale);
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = new \DateTime();
|
|
|
|
if ($data instanceof \DateTime) {
|
|
|
|
$value = $data;
|
2018-02-21 13:32:04 +00:00
|
|
|
} else if (\is_string($data) && !is_numeric($data)) {
|
2016-01-15 12:30:13 +00:00
|
|
|
$data = strtotime($data);
|
|
|
|
$value->setTimestamp($data);
|
|
|
|
} else if ($data !== null) {
|
2018-02-21 18:53:44 +00:00
|
|
|
$data = (int)$data;
|
2016-01-15 12:30:13 +00:00
|
|
|
$value->setTimestamp($data);
|
|
|
|
}
|
|
|
|
|
2018-02-21 13:32:04 +00:00
|
|
|
$options = array_merge(['width' => 'long'], $options);
|
2016-01-15 12:30:13 +00:00
|
|
|
$width = $options['width'];
|
|
|
|
switch ($type) {
|
|
|
|
case 'date':
|
2017-07-05 15:37:12 +00:00
|
|
|
return (string) Calendar::formatDate($value, $width, $this->locale);
|
2016-01-15 12:30:13 +00:00
|
|
|
case 'datetime':
|
2017-07-05 15:37:12 +00:00
|
|
|
return (string) Calendar::formatDatetime($value, $width, $this->locale);
|
2016-01-15 12:30:13 +00:00
|
|
|
case 'time':
|
2017-07-05 15:37:12 +00:00
|
|
|
return (string) Calendar::formatTime($value, $width, $this->locale);
|
2017-11-03 09:48:04 +00:00
|
|
|
case 'weekdayName':
|
2017-07-05 15:37:12 +00:00
|
|
|
return (string) Calendar::getWeekdayName($value, $width, $this->locale);
|
2016-01-15 12:30:13 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an associative array with all translations
|
|
|
|
*
|
|
|
|
* Called by \OC_L10N_String
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-02-21 13:32:04 +00:00
|
|
|
public function getTranslations(): array {
|
2016-01-15 12:30:13 +00:00
|
|
|
return $this->translations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returnsed function accepts the argument $n
|
|
|
|
*
|
|
|
|
* Called by \OC_L10N_String
|
2018-02-21 18:46:29 +00:00
|
|
|
* @return \Closure the plural form function
|
2016-01-15 12:30:13 +00:00
|
|
|
*/
|
2018-02-21 18:46:29 +00:00
|
|
|
public function getPluralFormFunction(): \Closure {
|
2018-02-21 13:32:04 +00:00
|
|
|
if (\is_null($this->pluralFormFunction)) {
|
2017-08-07 13:50:00 +00:00
|
|
|
$lang = $this->getLanguageCode();
|
|
|
|
$this->pluralFormFunction = function($n) use ($lang) {
|
|
|
|
return PluralizationRules::get($n, $lang);
|
|
|
|
};
|
2016-01-15 12:30:13 +00:00
|
|
|
}
|
2017-08-07 13:50:00 +00:00
|
|
|
|
2016-01-15 12:30:13 +00:00
|
|
|
return $this->pluralFormFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-21 13:32:04 +00:00
|
|
|
* @param string $translationFile
|
2016-01-15 12:30:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-21 13:32:04 +00:00
|
|
|
protected function load(string $translationFile): bool {
|
2016-01-15 12:30:13 +00:00
|
|
|
$json = json_decode(file_get_contents($translationFile), true);
|
2018-02-21 13:32:04 +00:00
|
|
|
if (!\is_array($json)) {
|
2016-01-15 12:30:13 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|