Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to use psr-3 messages. #1372

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions application/src/I18n/Translator.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
namespace Omeka\I18n;

use Omeka\Stdlib\Message;
use Laminas\I18n\Translator\TranslatorAwareInterface;
use Laminas\I18n\Translator\TranslatorInterface;
use Omeka\Stdlib\MessageInterface;

class Translator implements TranslatorInterface
{
Expand All @@ -21,14 +22,17 @@ public function __construct(TranslatorInterface $translator)

public function translate($message, $textDomain = 'default', $locale = null)
{
if (!$message instanceof Message) {
return $this->translator->translate($message, $textDomain, $locale);
if ($message instanceof TranslatorAwareInterface) {
$message->setTranslator($this->translator, $textDomain);
return $message->translate($textDomain, $locale);
}
$translation = $this->translator->translate($message->getMessage(), $textDomain, $locale);
if ($message->hasArgs()) {
$translation = sprintf($translation, ...$message->getArgs());

if ($message instanceof MessageInterface) {
$translation = $this->translator->translate($message->getMessage(), $textDomain, $locale);
return $message->interpolate($translation, $message->getContext());
}
return $translation;

return $this->translator->translate($message, $textDomain, $locale);
}

public function translatePlural($singular, $plural, $number, $textDomain = 'default', $locale = null)
Expand Down
31 changes: 24 additions & 7 deletions application/src/Stdlib/Message.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace Omeka\Stdlib;

class Message implements \JsonSerializable
/**
* Manage a message with a a list of placeholders formatted for sprintf().
*/
class Message implements MessageInterface, \JsonSerializable
{
/**
* @var string
Expand Down Expand Up @@ -30,18 +33,26 @@ public function __construct($message, ...$args)
$this->args = $args;
}

/**
* Get the message string.
*
* @return string
*/
public function getMessage()
{
return $this->message;
}

public function getContext()
{
return $this->args;
}

public function hasContext()
{
return (bool) $this->args;
}

/**
* Get the message arguments.
*
* @deprecated 1.4.0 Use getContext() instead.
* @return array
*/
public function getArgs()
{
Expand All @@ -51,6 +62,7 @@ public function getArgs()
/**
* Does this message have arguments?
*
* @deprecated 1.4.0 Use hasContext() instead.
* @return bool
*/
public function hasArgs()
Expand All @@ -68,9 +80,14 @@ public function escapeHtml()
return $this->escapeHtml;
}

public function interpolate($message, array $context = [])
{
return (string) sprintf($message, ...$context);
}

public function __toString()
{
return (string) sprintf($this->getMessage(), ...$this->getArgs());
return $this->interpolate($this->getMessage(), $this->getContext());
}

public function jsonSerialize()
Expand Down
45 changes: 45 additions & 0 deletions application/src/Stdlib/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace Omeka\Stdlib;

/**
* Message interface.
*/
interface MessageInterface
{
/**
* Get the message string.
*
* @return string
*/
public function getMessage();

/**
* Get the context of the message (the arguments, if any).
*
* @return array
*/
public function getContext();

/**
* Does this message have a context (arguments)?
*
* @return bool
*/
public function hasContext();

/**
* Indicate if the message should be escaped for html.
*
* @return bool
*/
public function escapeHtml();

/**
* Get the interpolated message string with context.
*
* @param string $message
* @param array $context
* @return string
*/
public function interpolate($message, array $context = []);
}
20 changes: 20 additions & 0 deletions application/src/Stdlib/PsrInterpolateInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Omeka\Stdlib;

/**
* Interpolate a PSR-3 message with a context into a string.
*/
interface PsrInterpolateInterface
{
/**
* Interpolates context values into the PSR-3 message placeholders.
*
* Keys that are not stringable are kept as class or type.
*
* @param string $message Message with PSR-3 placeholders.
* @param array $context Associative array with placeholders and strings.
* @return string
*/
public function interpolate($message, array $context = null): string;
}
53 changes: 53 additions & 0 deletions application/src/Stdlib/PsrInterpolateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Omeka\Stdlib;

/**
* Interpolate a PSR-3 message with a context into a string.
*/
trait PsrInterpolateTrait
{
/**
* Interpolates context values into the PSR-3 message placeholders.
*
* Keys that are not stringable are kept as class or type.
*
* @see https://www.php-fig.org/psr/psr-3/
*
* @param string $message Message with PSR-3 placeholders.
* @param array $context Associative array with placeholders and strings.
* @return string
*/
public function interpolate($message, array $context = null): string
{
$message = (string) $message;

if (empty($context)) {
return $message;
}

if (strpos($message, '{') === false) {
return $message;
}

$replacements = [];
foreach ($context as $key => $val) {
if (is_null($val)
|| is_scalar($val)
|| (is_object($val) && method_exists($val, '__toString'))
) {
$replacements['{' . $key . '}'] = $val;
} elseif (is_array($val)) {
$replacements['{' . $key . '}'] = 'array' . @json_encode($val);
} elseif (is_object($val)) {
$replacements['{' . $key . '}'] = '[object ' . get_class($val) . ']';
} elseif (is_resource($val)) {
$replacements['{' . $key . '}'] = '[resource ' . get_resource_type($val) . ']';
} else {
$replacements['{' . $key . '}'] = '[' . gettype($val) . ']';
}
}

return strtr($message, $replacements);
}
}
141 changes: 141 additions & 0 deletions application/src/Stdlib/PsrMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php declare(strict_types=1);

namespace Omeka\Stdlib;

use Laminas\I18n\Translator\TranslatorAwareInterface;
use Laminas\I18n\Translator\TranslatorAwareTrait;

/**
* Manage a message with a context list of placeholders formatted as psr-3.
*
* Copy of Omeka Message, except the constructor, that requires an array, and
* the possibility to translate automatically when the translator is enabled.
* Generally, the translator is not set, as it is usually managed internally.
*
* ```
* // To get a translator in a controller:
* $translator = $this->getEvent()->getApplication()->getServiceManager()->get('MvcTranslator');
* // or:
* $translator = $this->viewHelpers()->get('translate')->getTranslator();
*
* // To get translator in a view:
* $translator = $this->plugin('translate')->getTranslator();
*
* // To set the translator:
* $psrMessage->setTranslator($translator);
* // To disable the translation when the translator is set:
* $psrMessage->setTranslatorEnabled(false);
* ```
*
* @see \Omeka\Stdlib\Message
*/
class PsrMessage implements MessageInterface, TranslatorAwareInterface, \JsonSerializable, PsrInterpolateInterface
{
use PsrInterpolateTrait;
use TranslatorAwareTrait;

/**
* @var string
*/
protected $message;

/**
* @var array
*/
protected $context = [];

/**
* @var bool
*/
protected $escapeHtml = true;

/**
* Set the message string and its context. The plural is not managed.
*/
public function __construct($message, array $context = [])
{
$this->message = $message;
$this->context = $context;
}

/**
* Get the message string.
*/
public function getMessage(): string
{
return (string) $this->message;
}

/**
* Get the message context.
*/
public function getContext(): array
{
return $this->context;
}

/**
* Does this message have context?
*/
public function hasContext(): bool
{
return (bool) $this->context;
}

/**
* Get the message arguments for compatibility purpose only.
*
* @deprecated Use hasContext() instead.
* @return array Non-associative array in order to comply with sprintf.
*/
public function getArgs()
{
return array_values($this->getContext());
}

/**
* Does this message have arguments? For compatibility purpose only.
*
* @deprecated Use hasContext() instead.
* @return bool
*/
public function hasArgs()
{
return $this->hasContext();
}

public function setEscapeHtml($escapeHtml): self
{
$this->escapeHtml = (bool) $escapeHtml;
return $this;
}

public function escapeHtml(): bool
{
return $this->escapeHtml;
}

public function __toString()
{
return $this->isTranslatorEnabled()
? $this->translate()
: $this->interpolate($this->getMessage(), $this->getContext());
}

/**
* Translate the message with the context.
*
* Same as TranslatorInterface::translate(), but the message is the current one.
*/
public function translate($textDomain = 'default', $locale = null): string
{
return $this->hasTranslator()
? $this->interpolate($this->translator->translate($this->getMessage(), $textDomain, $locale), $this->getContext())
: $this->interpolate($this->getMessage(), $this->getContext());
}

public function jsonSerialize()
{
return (string) $this;
}
}
Loading