4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\Context\Reader;
13 use Behat\Behat\Context\Environment\ContextEnvironment;
14 use Behat\Behat\Context\Exception\UnknownTranslationResourceException;
15 use Behat\Behat\Context\TranslatableContext;
16 use Symfony\Component\Translation\Translator;
19 * Reads translation resources from translatable contexts.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 final class TranslatableContextReader implements ContextReader
33 * @param Translator $translator
35 public function __construct(Translator $translator)
37 $this->translator = $translator;
43 * @see TranslatableContext
45 public function readContextCallees(ContextEnvironment $environment, $contextClass)
47 $reflClass = new \ReflectionClass($contextClass);
49 if (!$reflClass->implementsInterface('Behat\Behat\Context\TranslatableContext')) {
53 $assetsId = $environment->getSuite()->getName();
54 foreach (call_user_func(array($contextClass, 'getTranslationResources')) as $path) {
55 $this->addTranslationResource($path, $assetsId);
62 * Adds translation resource.
65 * @param string $assetsId
67 * @throws UnknownTranslationResourceException
69 private function addTranslationResource($path, $assetsId)
71 switch ($ext = pathinfo($path, PATHINFO_EXTENSION)) {
73 $this->addTranslatorResource('yaml', $path, basename($path, '.' . $ext), $assetsId);
76 $this->addTranslatorResource('xliff', $path, basename($path, '.' . $ext), $assetsId);
79 $this->addTranslatorResource('php', $path, basename($path, '.' . $ext), $assetsId);
82 throw new UnknownTranslationResourceException(sprintf(
83 'Can not read translations from `%s`. File type is not supported.',
90 * Adds resource to translator instance.
94 * @param string $language
95 * @param string $assetsId
97 private function addTranslatorResource($type, $path, $language, $assetsId)
99 $this->translator->addResource($type, $path, $language, $assetsId);