12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace OC\L10N;
- class L10NString implements \JsonSerializable {
-
- protected $l10n;
-
- protected $text;
-
- protected $parameters;
-
- protected $count;
-
- public function __construct(L10N $l10n, $text, array $parameters, int $count = 1) {
- $this->l10n = $l10n;
- $this->text = $text;
- $this->parameters = $parameters;
- $this->count = $count;
- }
- public function __toString(): string {
- $translations = $this->l10n->getTranslations();
- $identityTranslator = $this->l10n->getIdentityTranslator();
-
- $identity = $this->text;
- if (array_key_exists($this->text, $translations)) {
- $identity = $translations[$this->text];
- }
- if (is_array($identity)) {
- $pipeCheck = implode('', $identity);
- if (str_contains($pipeCheck, '|')) {
- return 'Can not use pipe character in translations';
- }
- $identity = implode('|', $identity);
- } elseif (str_contains($identity, '|')) {
- return 'Can not use pipe character in translations';
- }
- $beforeIdentity = $identity;
- $identity = str_replace('%n', '%count%', $identity);
- $parameters = [];
- if ($beforeIdentity !== $identity) {
- $parameters = ['%count%' => $this->count];
- }
-
- $text = $identityTranslator->trans($identity, $parameters);
- return vsprintf($text, $this->parameters);
- }
- public function jsonSerialize(): string {
- return $this->__toString();
- }
- }
|