public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/web/inc/lib/vendor/symfony/translation/LocaleSwitcher.php
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
<?php
3
/*
4
* This file is part of the Symfony package.
5
*
6
* (c) Fabien Potencier <[email protected]>
7
*
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
10
*/
12
namespace Symfony\Component\Translation;
14
use Symfony\Component\Routing\RequestContext;
15
use Symfony\Contracts\Translation\LocaleAwareInterface;
17
/**
18
* @author Kevin Bond <[email protected]>
19
*/
20
class LocaleSwitcher implements LocaleAwareInterface
21
{
22
private string $defaultLocale;
24
/**
25
* @param LocaleAwareInterface[] $localeAwareServices
26
*/
27
public function __construct(
28
private string $locale,
29
private iterable $localeAwareServices,
30
private ?RequestContext $requestContext = null,
31
) {
32
$this->defaultLocale = $locale;
33
}
35
public function setLocale(string $locale): void
36
{
37
if (class_exists(\Locale::class)) {
38
\Locale::setDefault($locale);
39
}
40
$this->locale = $locale;
41
$this->requestContext?->setParameter('_locale', $locale);
43
foreach ($this->localeAwareServices as $service) {
44
$service->setLocale($locale);
45
}
46
}
48
public function getLocale(): string
49
{
50
return $this->locale;
51
}
53
/**
54
* Switch to a new locale, execute a callback, then switch back to the original.
55
*
56
* @template T
57
*
58
* @param callable(string $locale):T $callback
59
*
60
* @return T
61
*/
62
public function runWithLocale(string $locale, callable $callback): mixed
63
{
64
$original = $this->getLocale();
65
$this->setLocale($locale);
67
try {
68
return $callback($locale);
69
} finally {
70
$this->setLocale($original);
71
}
72
}
74
public function reset(): void
75
{
76
$this->setLocale($this->defaultLocale);
77
}
78
}