ArrayMessageBundle.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Elgg\I18n;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Uses an array as a source for the message bundle.
  7. *
  8. * This is mostly useful for testing so we can configure translators
  9. * in-memory instead of going to the file system.
  10. *
  11. * @since 1.11
  12. *
  13. * @access private
  14. */
  15. final class ArrayMessageBundle implements MessageBundle {
  16. /** @var array */
  17. private $messages;
  18. /**
  19. * Constructor
  20. *
  21. * @param array $messages Map of locales to maps of keys to message-templates
  22. */
  23. public function __construct(array $messages) {
  24. $this->messages = $messages;
  25. }
  26. /** @inheritDoc */
  27. public function get($key, Locale $locale) {
  28. assert(is_string($key), '$key must be a string');
  29. if (!isset($this->messages["$locale"]) || !is_array($this->messages["$locale"])) {
  30. return null;
  31. }
  32. $messages = $this->messages["$locale"];
  33. if (!is_string($key) || !isset($messages[$key]) || !is_string($messages[$key])) {
  34. return null;
  35. }
  36. return new SprintfMessageTemplate($messages[$key]);
  37. }
  38. }