mirror of
https://github.com/flarum/core.git
synced 2025-08-14 20:34:10 +02:00
Performance: Actually make use of the translator cache
We had added a `storage/locale` directory to our skeleton, but we had forgotten to hook it up with the translator. Enabling caching saves parsing that locale YAML files on every pageload which should be good for performance. The locale cache will be cleared whenever an extension that uses the `Locales` or `LanguagePack` extenders is enabled/disabled. If debug mode is ON, then the caching mechanism will automatically check if any of the loaded YAML files are dirty and update accordingly.
This commit is contained in:
@@ -24,9 +24,15 @@ class LocaleManager
|
||||
|
||||
protected $css = [];
|
||||
|
||||
public function __construct(Translator $translator)
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $cacheDir;
|
||||
|
||||
public function __construct(Translator $translator, string $cacheDir = null)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
$this->cacheDir = $cacheDir;
|
||||
}
|
||||
|
||||
public function getLocale(): string
|
||||
@@ -106,4 +112,11 @@ class LocaleManager
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function clearCache()
|
||||
{
|
||||
if ($this->cacheDir) {
|
||||
array_map('unlink', glob($this->cacheDir.'/*'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -13,10 +13,10 @@ namespace Flarum\Locale;
|
||||
|
||||
use Flarum\Event\ConfigureLocales;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Foundation\Event\ClearingCache;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
|
||||
use Symfony\Component\Translation\MessageSelector;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class LocaleServiceProvider extends AbstractServiceProvider
|
||||
@@ -31,6 +31,10 @@ class LocaleServiceProvider extends AbstractServiceProvider
|
||||
$locales->addLocale($this->getDefaultLocale(), 'Default');
|
||||
|
||||
$events->dispatch(new ConfigureLocales($locales));
|
||||
|
||||
$events->listen(ClearingCache::class, function () use ($locales) {
|
||||
$locales->clearCache();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,11 +42,23 @@ class LocaleServiceProvider extends AbstractServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton(LocaleManager::class);
|
||||
$this->app->singleton(LocaleManager::class, function () {
|
||||
return new LocaleManager(
|
||||
$this->app->make('translator'),
|
||||
$this->getCacheDir()
|
||||
);
|
||||
});
|
||||
|
||||
$this->app->alias(LocaleManager::class, 'flarum.locales');
|
||||
|
||||
$this->app->singleton('translator', function () {
|
||||
$translator = new Translator($this->getDefaultLocale(), new MessageSelector());
|
||||
$translator = new Translator(
|
||||
$this->getDefaultLocale(),
|
||||
null,
|
||||
$this->getCacheDir(),
|
||||
$this->app->inDebugMode()
|
||||
);
|
||||
|
||||
$translator->setFallbackLocales(['en']);
|
||||
$translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader());
|
||||
|
||||
@@ -59,4 +75,9 @@ class LocaleServiceProvider extends AbstractServiceProvider
|
||||
|
||||
return $repo->get('default_locale', 'en');
|
||||
}
|
||||
|
||||
private function getCacheDir(): string
|
||||
{
|
||||
return $this->app->storagePath().'/locale';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user