Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getLocale() method #242

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Chg #232: Deprecate `ViewInterface::withDefaultExtension()` and `ViewInterface::getDefaultExtension()` in favor of
`withFallbackExtension()` and `getFallbackExtensions()` (@rustamwin)
- Bug #232: Fix render templates that contain dots in their name (@rustamwin)
- Enh #242: Add `ViewTrait::getLocale()` method (@Tigrov)
vjik marked this conversation as resolved.
Show resolved Hide resolved

## 8.0.0 February 16, 2023

Expand Down
10 changes: 10 additions & 0 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
vjik marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->localeState->getLocale();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where's localeState?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question.

It is inside View and WebView

view/src/WebView.php

Lines 39 to 45 in 9547650

final class WebView implements ViewInterface
{
use ViewTrait;
private WebViewState $state;
private LocaleState $localeState;

And state also

view/src/ViewTrait.php

Lines 228 to 231 in 9547650

public function getTheme(): ?Theme
{
return $this->state->getTheme();
}

This task for a new issue.

Copy link
Member

@vjik vjik Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task for a new issue.

What is task?

ViewTrait is internal trait that used in View and WebView only.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task for a new issue.

What is task?

ViewTrait is internal trait that used in View and WebView only.

Move localeState and state inside ViewTrait

}

/**
* Gets the base path to the view directory.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down