-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathDefaultPatternGenerationStrategy.php
More file actions
executable file
·76 lines (66 loc) · 2.71 KB
/
DefaultPatternGenerationStrategy.php
File metadata and controls
executable file
·76 lines (66 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace JMS\I18nRoutingBundle\Router;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Routing\Route;
/**
* The default strategy supports 3 different scenarios, and makes use of the
* Symfony2 Translator Component.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DefaultPatternGenerationStrategy implements PatternGenerationStrategyInterface
{
const STRATEGY_PREFIX = 'prefix';
const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default';
const STRATEGY_PREFIX_EXCEPT_DEFAULT_LISTENER = 'prefix_except_default_listener';
const STRATEGY_CUSTOM = 'custom';
private $strategy;
private $translator;
private $translationDomain;
private $locales;
private $cacheDir;
private $defaultLocale;
public function __construct($strategy, TranslatorInterface $translator, array $locales, $cacheDir, $translationDomain = 'routes', $defaultLocale = 'en')
{
$this->strategy = $strategy;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->locales = $locales;
$this->cacheDir = $cacheDir;
$this->defaultLocale = $defaultLocale;
}
/**
* {@inheritDoc}
*/
public function generateI18nPatterns($routeName, Route $route)
{
$patterns = array();
foreach ($route->getOption('i18n_locales') ?: $this->locales as $locale) {
// if no translation exists, we use the current pattern
if ($routeName === $i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale)) {
$i18nPattern = $route->getPattern();
}
// prefix with locale if requested
if (self::STRATEGY_PREFIX === $this->strategy
|| (self::STRATEGY_PREFIX_EXCEPT_DEFAULT === $this->strategy && $this->defaultLocale !== $locale) || (self::STRATEGY_PREFIX_EXCEPT_DEFAULT_LISTENER === $this->strategy && $this->defaultLocale !== $locale)) {
$i18nPattern = '/'.$locale.$i18nPattern;
}
$patterns[$i18nPattern][] = $locale;
}
return $patterns;
}
/**
* {@inheritDoc}
*/
public function addResources(RouteCollection $i18nCollection)
{
foreach ($this->locales as $locale) {
if (file_exists($metadata = $this->cacheDir.'/translations/catalogue.'.$locale.'.php.meta')) {
foreach (unserialize(file_get_contents($metadata)) as $resource) {
$i18nCollection->addResource($resource);
}
}
}
}
}