123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- declare(strict_types=1);
- namespace OC\L10N;
- use OCP\IL10N;
- use OCP\L10N\IFactory;
- use Psr\Log\LoggerInterface;
- use Punic\Calendar;
- use Symfony\Component\Translation\IdentityTranslator;
- class L10N implements IL10N {
-
- protected $factory;
-
- protected $app;
-
- protected $lang;
-
- protected $locale;
-
- private $identityTranslator;
-
- private $translations = [];
-
- 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);
- }
- }
-
- public function getLanguageCode(): string {
- return $this->lang;
- }
-
- public function getLocaleCode(): string {
- return $this->locale;
- }
-
- public function t(string $text, $parameters = []): string {
- if (!\is_array($parameters)) {
- $parameters = [$parameters];
- }
- return (string)new L10NString($this, $text, $parameters);
- }
-
- 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);
- }
-
- public function l(string $type, $data = null, array $options = []) {
- if ($this->locale === null) {
-
- $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;
- }
- }
-
- public function getTranslations(): array {
- return $this->translations;
- }
-
- public function getIdentityTranslator(): IdentityTranslator {
- if (\is_null($this->identityTranslator)) {
- $this->identityTranslator = new IdentityTranslator();
-
-
-
-
-
- $this->identityTranslator->setLocale($this->getLanguageCode());
- }
- return $this->identityTranslator;
- }
-
- protected function load(string $translationFile): bool {
- $json = json_decode(file_get_contents($translationFile), true);
- if (!\is_array($json)) {
- $jsonError = json_last_error();
- \OCP\Server::get(LoggerInterface::class)->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
- return false;
- }
- $this->translations = array_merge($this->translations, $json['translations']);
- return true;
- }
- }
|