diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 87cc4b2..5af8c84 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -8,6 +8,7 @@ services: autowire: true arguments: $menu_data: [] + $trans: "@translator" Catalyst\MenuBundle\EventListener\MenuAnnotationListener: arguments: diff --git a/Service/Generator.php b/Service/Generator.php index 401626a..894ab09 100644 --- a/Service/Generator.php +++ b/Service/Generator.php @@ -7,6 +7,7 @@ use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Config\ConfigCache; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Translation\TranslatorInterface; use Catalyst\MenuBundle\Menu\Collection; use Catalyst\MenuBundle\Menu\Item; @@ -18,12 +19,14 @@ class Generator protected $menu; protected $menu_data; + protected $translator; - public function __construct($menu_data) + public function __construct($menu_data, TranslatorInterface $trans) { $this->index = new Collection(); $this->menu = new Collection(); $this->menu_data = $this->processConfigs($menu_data); + $this->translator = $trans; } public function getMenu($menu_key) @@ -123,8 +126,27 @@ class Generator return $pdata; } + protected function translate($text) + { + // find text enclosed in [] + $regex = '/\[(.*)\]/U'; + preg_match_all($regex, $text, $matches); + + // replace text enclodes in [] with the trnaslation of it + $final_text = $text; + $all_terms = $matches[1]; + foreach ($all_terms as $term) + { + $final_text = str_replace('[' . $term . ']', $this->translator->trans($term), $final_text); + } + + return $final_text; + } + protected function newItem($index, $id, $label, $icon = null) { + $trans_label = $this->translate($label); + $mi = new Item(); $mi->setID($id) ->setLabel($label)