-
-
Notifications
You must be signed in to change notification settings - Fork 834
/
LocaleServiceProvider.php
71 lines (57 loc) · 2.24 KB
/
LocaleServiceProvider.php
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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Locale;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Paths;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use Symfony\Contracts\Translation\TranslatorInterface;
class LocaleServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
*/
public function register()
{
$this->container->singleton(LocaleManager::class, function (Container $container) {
$locales = new LocaleManager(
$container->make('translator'),
$this->getCacheDir($container)
);
$locales->addLocale($this->getDefaultLocale($container), 'Default');
$locales->addTranslations('en', __DIR__.'/../../locale/core.yml');
$locales->addTranslations('en', __DIR__.'/../../locale/validation.yml');
return $locales;
});
$this->container->alias(LocaleManager::class, 'flarum.locales');
$this->container->singleton('translator', function (Container $container) {
$translator = new Translator(
$this->getDefaultLocale($container),
null,
$this->getCacheDir($container),
$container['flarum.debug']
);
$translator->setFallbackLocales(['en']);
$translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader());
return $translator;
});
$this->container->alias('translator', Translator::class);
$this->container->alias('translator', TranslatorContract::class);
$this->container->alias('translator', TranslatorInterface::class);
}
private function getDefaultLocale(Container $container): string
{
$repo = $container->make(SettingsRepositoryInterface::class);
return $repo->get('default_locale', 'en');
}
private function getCacheDir(Container $container): string
{
return $container[Paths::class]->storage.'/locale';
}
}