mirror of
https://github.com/flarum/framework.git
synced 2025-02-06 22:03:01 +08: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:
parent
6b6fce878a
commit
13ce2d1e3d
|
@ -18,7 +18,7 @@ use Illuminate\Contracts\Container\Container;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
class LanguagePack implements ExtenderInterface
|
class LanguagePack implements ExtenderInterface, LifecycleInterface
|
||||||
{
|
{
|
||||||
public function extend(Container $container, Extension $extension = null)
|
public function extend(Container $container, Extension $extension = null)
|
||||||
{
|
{
|
||||||
|
@ -63,4 +63,14 @@ class LanguagePack implements ExtenderInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onEnable(Container $container, Extension $extension)
|
||||||
|
{
|
||||||
|
$container->make('flarum.locales')->clearCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onDisable(Container $container, Extension $extension)
|
||||||
|
{
|
||||||
|
$container->make('flarum.locales')->clearCache();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ use Flarum\Extension\Extension;
|
||||||
use Flarum\Locale\LocaleManager;
|
use Flarum\Locale\LocaleManager;
|
||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
class Locales implements ExtenderInterface
|
class Locales implements ExtenderInterface, LifecycleInterface
|
||||||
{
|
{
|
||||||
protected $directory;
|
protected $directory;
|
||||||
|
|
||||||
|
@ -46,4 +46,14 @@ class Locales implements ExtenderInterface
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onEnable(Container $container, Extension $extension)
|
||||||
|
{
|
||||||
|
$container->make('flarum.locales')->clearCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onDisable(Container $container, Extension $extension)
|
||||||
|
{
|
||||||
|
$container->make('flarum.locales')->clearCache();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,15 @@ class LocaleManager
|
||||||
|
|
||||||
protected $css = [];
|
protected $css = [];
|
||||||
|
|
||||||
public function __construct(Translator $translator)
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $cacheDir;
|
||||||
|
|
||||||
|
public function __construct(Translator $translator, string $cacheDir = null)
|
||||||
{
|
{
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
$this->cacheDir = $cacheDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLocale(): string
|
public function getLocale(): string
|
||||||
|
@ -106,4 +112,11 @@ class LocaleManager
|
||||||
{
|
{
|
||||||
$this->translator = $translator;
|
$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\Event\ConfigureLocales;
|
||||||
use Flarum\Foundation\AbstractServiceProvider;
|
use Flarum\Foundation\AbstractServiceProvider;
|
||||||
|
use Flarum\Foundation\Event\ClearingCache;
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
|
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
|
||||||
use Symfony\Component\Translation\MessageSelector;
|
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
|
||||||
class LocaleServiceProvider extends AbstractServiceProvider
|
class LocaleServiceProvider extends AbstractServiceProvider
|
||||||
|
@ -31,6 +31,10 @@ class LocaleServiceProvider extends AbstractServiceProvider
|
||||||
$locales->addLocale($this->getDefaultLocale(), 'Default');
|
$locales->addLocale($this->getDefaultLocale(), 'Default');
|
||||||
|
|
||||||
$events->dispatch(new ConfigureLocales($locales));
|
$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()
|
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->alias(LocaleManager::class, 'flarum.locales');
|
||||||
|
|
||||||
$this->app->singleton('translator', function () {
|
$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->setFallbackLocales(['en']);
|
||||||
$translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader());
|
$translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader());
|
||||||
|
|
||||||
|
@ -59,4 +75,9 @@ class LocaleServiceProvider extends AbstractServiceProvider
|
||||||
|
|
||||||
return $repo->get('default_locale', 'en');
|
return $repo->get('default_locale', 'en');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCacheDir(): string
|
||||||
|
{
|
||||||
|
return $this->app->storagePath().'/locale';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user