/** * testowanie tłumaczeń */ public function testTranslations() { $this->doc->title(''); $this->doc->addTranslation(array('LOREM_IPSUM' => 'Lorem Ipsum', 'FOO_BAR' => 'Foo Bar')); $this->doc->body('<trans>LOREM_IPSUM</trans> <trans>FOO_BAR</trans> <trans>UNKNOWN</trans>'); $this->assertEquals('Lorem Ipsum Foo Bar UNKNOWN', $this->doc->render()); $locator = new FileLocator(__DIR__ . '/tmp/config'); $loader = new TranslationLoader($locator, array('document' => $this->doc, 'trans_closure' => function ($text) { switch ($text) { case 'TRANS_TEST1': return 'Test Word 1'; case 'TRANS_TEST2': return 'Test Word 2'; } return $text; }, 'cache_dir' => __DIR__ . '/tmp/cache', 'cache_refresh' => true)); $loader->load('translations.yml'); $this->doc->body('<trans>TRANS_TEST1</trans> <trans>TRANS_TEST2</trans>'); $this->assertEquals('Test Word 1 Test Word 2', $this->doc->render()); }
/** * {@inheritdoc} */ public function createDocument(array $options = []) { $document = new HtmlDocument(); // used by Modernizr $document->element('html')->addClass('no-js'); // meta viewport $viewport = $this->params["meta_viewport"]; if (!empty($viewport)) { $meta = $document->element('viewport'); $meta->attr('content', $viewport); } // favicon $favicon = $document->element('favicon'); $favicon->enable($this->params["favicon_enable"]); $favicon->setBasePath($this->appPaths->url('web_theme') . '/favicons'); $favicon->setTileColor($this->params["favicon_tile_color"]); if (file_exists($this->appPaths->absolute('web_theme') . '/favicons/favicon.html')) { $favicon->setFaviconTemplate(file_get_contents($this->appPaths->absolute('web_theme') . '/favicons/favicon.html')); } // html lang $document->element('html')->attr('lang', $this->requestStack->getCurrentRequest()->getLocale()); // resources $utility = $this->getUtility($this->params); $utility->createResOnAdd($document, "javascript", "default"); $utility->createResOnAdd($document, "stylesheet", "default"); $document->resources("javascript")->chooseOnAdd("default"); $document->resources("stylesheet")->chooseOnAdd("default"); $configDir = $this->appPaths->getRootDir() . '/document'; if (file_exists($configDir . '/' . $this->theme->getName())) { $configDir .= '/' . $this->theme->getName(); } $locator = new FileLocator($configDir); $loader = $utility->createResourcesLoader($document, 'javascript', $locator, '/'); $loader->load('html_resources.yml', 'framework'); $loader->load('html_resources.yml', 'core'); $locator = new FileLocator($this->appPaths->absolute('private_theme')); $loader = $utility->createResourcesLoader($document, 'stylesheet', $locator, $this->appPaths->getThemePath() . '/src'); $loader->load('html_resources.yml', 'theme'); $loader = $utility->createResourcesLoader($document, 'javascript', $locator, $this->appPaths->getThemePath() . '/src'); $loader->load('html_resources.yml', 'theme'); // js initializer $jsloader = $this->getJsLoader(); $script = $document->element('script'); $twig = $this->twig; $env = $this->env; $document->setScriptOutput(function (JavaScriptResourceManager $manager, array $translations) use($jsloader, $script, $twig, $env) { $output = $jsloader->render('html'); if ($env === 'dev') { foreach ($jsloader->resources() as $resource) { if ($resource instanceof CombineResourceInterface && $resource->getCombineObject()->getException() instanceof \Exception) { throw $resource->getCombineObject()->getException(); } } } $output .= $twig->render('::head.html.twig', ["resources" => $manager->render('array'), "translations" => $translations]); if (!$script->isEmpty()) { $output .= '<script type="text/javascript">' . $script->render() . '</script>'; $script->update(function () { return ''; }); } return $output; }); // translations used in theme if (file_exists($this->appPaths->absolute("kernel_root") . '/theme/' . $this->appPaths->getThemeName() . '/translations.yml')) { $trans = $this->translator; $locator = new FileLocator($this->appPaths->absolute("kernel_root") . '/theme/' . $this->appPaths->getThemeName()); $loader = new TranslationLoader($locator, ['cache_dir' => $this->appPaths->absolute("kernel_cache") . "/vsymfo_document/translations", 'cache_refresh' => $this->env == "dev", 'document' => $document, 'trans_closure' => function ($text) use($trans) { return $trans->trans($text); }]); $loader->load('translations.yml'); } return $document; }