-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AddTranslations.php
71 lines (58 loc) · 1.72 KB
/
AddTranslations.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\Frontend;
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Locale\LocaleManager;
use Illuminate\Support\Arr;
/**
* @internal
*/
class AddTranslations
{
/**
* @var LocaleManager
*/
protected $locales;
/**
* @var callable
*/
protected $filter;
public function __construct(LocaleManager $locales, callable $filter = null)
{
$this->locales = $locales;
$this->filter = $filter;
}
public function forFrontend(string $name)
{
$this->filter = function (string $id) use ($name) {
return preg_match('/^.+(?:\.|::)(?:'.$name.'|lib)\./', $id);
};
return $this;
}
public function to(Assets $assets)
{
$assets->localeJs(function (SourceCollector $sources, string $locale) {
$sources->addString(function () use ($locale) {
$translations = $this->getTranslations($locale);
return 'flarum.core.app.translator.addTranslations('.json_encode($translations).')';
});
});
}
private function getTranslations(string $locale)
{
$catalogue = $this->locales->getTranslator()->getCatalogue($locale);
$translations = $catalogue->all('messages');
while ($catalogue = $catalogue->getFallbackCatalogue()) {
$translations = array_replace($catalogue->all('messages'), $translations);
}
return Arr::only(
$translations,
array_filter(array_keys($translations), $this->filter)
);
}
}