diff --git a/src/DefaultHtmlTagFactory.php b/src/DefaultHtmlTagFactory.php index c4397be..962dbb4 100644 --- a/src/DefaultHtmlTagFactory.php +++ b/src/DefaultHtmlTagFactory.php @@ -25,6 +25,13 @@ public function createTag(MessageElement $element) ])); } + if ($element->is('ul', 'ol', 'li')) { + return Html::el($element->getName(), $element->getAttributes([ + 'class', + 'title', + ])); + } + if ($element->is('br')) { return Html::el('br'); } diff --git a/src/TextTranslator.php b/src/TextTranslator.php index 9c940da..3282f84 100644 --- a/src/TextTranslator.php +++ b/src/TextTranslator.php @@ -63,16 +63,34 @@ private function translateMessage(MessageId $messageId, array $parameters) return $messageId->toString(); } + return ltrim($this->formatElements($message->getElements())); + } + + + /** + * @param array $elements + * @return string + */ + private function formatElements(array $elements) + { $res = ''; - foreach ($message->getElements() as $element) { + foreach ($elements as $element) { if ($element instanceof MessageElement) { if ($element->is('br')) { $res .= "\n"; continue; } - $res .= $element->toText(); + if ($element->is('ul')) { + $res .= $this->formatList($element->getChildren(), FALSE); + + } elseif ($element->is('ol')) { + $res .= $this->formatList($element->getChildren(), TRUE); + + } else { + $res .= $this->formatElements($element->getChildren()); + } } else { $res .= $element; @@ -81,4 +99,55 @@ private function translateMessage(MessageId $messageId, array $parameters) return $res; } + + + /** + * @param array $elements + * @param bool $isOrdered + * @param non-negative-int $level + * @return string + */ + private function formatList( + array $elements, + $isOrdered, + $level = 0 + ) + { + $res = "\n"; + $levelPrefix = str_repeat("\t", $level); + $counter = 0; + + foreach ($elements as $element) { + if ($element instanceof MessageElement) { + if ($element->is('ul')) { + $res .= $this->formatList($element->getChildren(), FALSE, $level++); + continue; + } + + if ($element->is('ol')) { + $res .= $this->formatList($element->getChildren(), FALSE, $level++); + continue; + } + + $res .= $levelPrefix; + + if ($element->is('li')) { + if ($isOrdered) { + $counter++; + $res .= "\n" . $counter . '. '; + + } else { + $res .= "\n- "; + } + } + + $res .= $this->formatElements($element->getChildren()); + + } else { + $res .= $levelPrefix . $element; + } + } + + return $res . "\n"; + } } diff --git a/tests/Translator/HtmlTranslator.phpt b/tests/Translator/HtmlTranslator.phpt index 1d3aa12..6e6e41c 100644 --- a/tests/Translator/HtmlTranslator.phpt +++ b/tests/Translator/HtmlTranslator.phpt @@ -20,6 +20,8 @@ function createTranslator($lang) 'homepage.unknow' => 'Hello, lorem {$name|lower}!', 'homepage.br' => 'Hello
friends', 'homepage.entity' => 'A entity', + 'homepage.ul' => '', + 'homepage.ol' => '
  1. A
  2. B
', ]), ]), new Translator\Processors\TagProcessor @@ -62,6 +64,13 @@ test('MessageId', function () { }); +test('Lists', function () { + $translator = createTranslator('en'); + Assert::same("", (string) $translator->translate('homepage.ul')); + Assert::same("
  1. A
  2. B
", (string) $translator->translate('homepage.ol')); +}); + + test('Entities', function () { $translator = createTranslator('en'); Assert::same("A\xC2\xA0entity", (string) $translator->translate('homepage.entity')); diff --git a/tests/Translator/TextTranslator.phpt b/tests/Translator/TextTranslator.phpt index 746f620..a168280 100644 --- a/tests/Translator/TextTranslator.phpt +++ b/tests/Translator/TextTranslator.phpt @@ -24,6 +24,8 @@ function createTranslator($lang) 'homepage.hello2' => 'Hello, {$name}!', 'homepage.br' => 'Hello
friends', 'homepage.entity' => 'A entity', + 'homepage.ul' => '', + 'homepage.ol' => '
  1. A
  2. B
', ]), ]), new Translator\Processors\TagProcessor @@ -77,6 +79,13 @@ test('MessageId', function () { }); +test('Lists', function () { + $translator = createTranslator('en'); + Assert::same("- A\n- B\n", (string) $translator->translate('homepage.ul')); + Assert::same("1. A\n2. B\n", (string) $translator->translate('homepage.ol')); +}); + + test('Entities', function () { $translator = createTranslator('en'); Assert::same("A\xC2\xA0entity", (string) $translator->translate('homepage.entity'));