diff --git a/src/ViewTrait.php b/src/ViewTrait.php index c04c43519..4a5ad105f 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -189,6 +189,16 @@ public function withLocale(string $locale): static return $new; } + /** + * Get the specified locale code. + * + * @return string The locale code. + */ + public function getLocale(): string + { + return $this->localeState->getLocale(); + } + /** * Gets the base path to the view directory. * diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 074ea3812..00c45352b 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -586,6 +586,17 @@ public function testImmutability(): void $this->assertNotSame($view, $view->withFallbackExtension('tpl')); } + public function testGetLocale() + { + $view = TestHelper::createView(); + + $this->assertSame('en', $view->getLocale()); + + $view->setLocale('en-US'); + + $this->assertSame('en-US', $view->getLocale()); + } + private function createViewWithBasePath(string $basePath): View { return new View($basePath, new SimpleEventDispatcher());