-
-
Notifications
You must be signed in to change notification settings - Fork 880
Expand file tree
/
Copy pathLanguagePack.php
More file actions
134 lines (109 loc) · 4.1 KB
/
Copy pathLanguagePack.php
File metadata and controls
134 lines (109 loc) · 4.1 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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\Extend;
use DirectoryIterator;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Translation\MessageCatalogueInterface;
class LanguagePack implements ExtenderInterface, LifecycleInterface
{
private const CORE_LOCALE_FILES = [
'core',
'validation',
];
private $path;
/**
* LanguagePack constructor.
*
* @param string $path: Path to yaml language files.
*/
public function __construct(string $path = '/locale')
{
$this->path = $path;
}
public function extend(Container $container, Extension $extension = null)
{
if (is_null($extension)) {
throw new InvalidArgumentException(
'I need an extension instance to register a language pack'
);
}
$locale = $extension->composerJsonAttribute('extra.flarum-locale.code');
$title = $extension->composerJsonAttribute('extra.flarum-locale.title');
if (! isset($locale, $title)) {
throw new RuntimeException(
'Language packs must define "extra.flarum-locale.code" and "extra.flarum-locale.title" in composer.json.'
);
}
$container->resolving(
LocaleManager::class,
function (LocaleManager $locales, Container $container) use ($extension, $locale, $title) {
$this->registerLocale($container, $locales, $extension, $locale, $title);
}
);
}
private function registerLocale(Container $container, LocaleManager $locales, Extension $extension, $locale, $title)
{
$locales->addLocale($locale, $title);
$directory = $extension->getPath().$this->path;
if (! is_dir($directory)) {
throw new RuntimeException(
'Expected to find "'.$this->path.'" directory in language pack.'
);
}
if (file_exists($file = $directory.'/config.js')) {
$locales->addJsFile($locale, $file);
}
if (file_exists($file = $directory.'/config.css')) {
$locales->addCssFile($locale, $file);
}
foreach (new DirectoryIterator($directory) as $file) {
if ($this->shouldLoad($file, $container)) {
$locales->addTranslations($locale, $file->getPathname());
}
}
}
private function shouldLoad(SplFileInfo $file, Container $container)
{
if (! $file->isFile()) {
return false;
}
// We are only interested in YAML files
if (! in_array($file->getExtension(), ['yml', 'yaml'], true)) {
return false;
}
// Some language packs include translations for many extensions
// from the ecosystems. For performance reasons, we should only
// load those that belong to core, or extensions that are enabled.
// To identify them, we compare the filename (without the YAML
// extension) with the list of known names and all extension IDs.
$slug = $file->getBasename(".{$file->getExtension()}");
// Ignore ICU MessageFormat suffixes.
$slug = str_replace(MessageCatalogueInterface::INTL_DOMAIN_SUFFIX, '', $slug);
if (in_array($slug, self::CORE_LOCALE_FILES, true)) {
return true;
}
/** @var ExtensionManager|null $extensions */
static $extensions;
$extensions = $extensions ?? $container->make(ExtensionManager::class);
return $extensions->isEnabled($slug);
}
public function onEnable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
public function onDisable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
}