diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 7ce181cbde18..dee30249d5f9 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -546,7 +546,24 @@ protected function compileEndunless($expression) */ protected function compileLang($expression) { - return "get$expression; ?>"; + if (is_null($expression)) { + return "startTranslation(); ?>"; + } elseif ($expression[1] === '[') { + return "startTranslation{$expression}; ?>"; + } else { + return "get$expression; ?>"; + } + } + + /** + * Compile the endlang statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compileEndlang() + { + return 'renderTranslation(); ?>'; } /** diff --git a/src/Illuminate/View/Factory.php b/src/Illuminate/View/Factory.php index 71a7b625d8ff..8ab6bc1b2d09 100755 --- a/src/Illuminate/View/Factory.php +++ b/src/Illuminate/View/Factory.php @@ -143,6 +143,13 @@ class Factory implements FactoryContract */ protected $pushStack = []; + /** + * The translation replacements for the translation being rendered. + * + * @var array + */ + protected $translationReplacements = []; + /** * The number of active rendering operations. * @@ -851,6 +858,31 @@ public function yieldPushContent($section, $default = '') return implode($this->pushes[$section]); } + /** + * Start a translation block. + * + * @param array $replacements + * @return void + */ + public function startTranslation($replacements = []) + { + ob_start(); + + $this->translationReplacements = $replacements; + } + + /** + * Render the current translation. + * + * @return string + */ + public function renderTranslation() + { + return $this->container->make('translator')->getFromJson( + trim(ob_get_clean()), $this->translationReplacements + ); + } + /** * Flush all of the section contents. * diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index 664e8da10d82..167d63b03de4 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -285,6 +285,20 @@ public function testComponentHandling() $this->assertEquals('title
component Taylor', $contents); } + public function testTranslation() + { + $container = new Illuminate\Container\Container; + $container->instance('translator', $translator = m::mock('StdClass')); + $translator->shouldReceive('getFromJson')->with('Foo', ['name' => 'taylor'])->andReturn('Bar'); + $factory = $this->getFactory(); + $factory->setContainer($container); + $factory->startTranslation(['name' => 'taylor']); + echo 'Foo'; + $string = $factory->renderTranslation(); + + $this->assertEquals('Bar', $string); + } + public function testSingleStackPush() { $factory = $this->getFactory();