Skip to content

Commit

Permalink
Performance: Actually make use of the translator cache
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tobyzerner committed Dec 6, 2018
1 parent a8f5ca8 commit 0d16fac
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/Extend/LanguagePack.php
Expand Up @@ -18,7 +18,7 @@
use InvalidArgumentException;
use RuntimeException;

class LanguagePack implements ExtenderInterface
class LanguagePack implements ExtenderInterface, LifecycleInterface
{
public function extend(Container $container, Extension $extension = null)
{
Expand Down Expand Up @@ -63,4 +63,14 @@ public function extend(Container $container, Extension $extension = null)
}
}
}

public function onEnable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}

public function onDisable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
}
12 changes: 11 additions & 1 deletion src/Extend/Locales.php
Expand Up @@ -16,7 +16,7 @@
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;

class Locales implements ExtenderInterface
class Locales implements ExtenderInterface, LifecycleInterface
{
protected $directory;

Expand Down Expand Up @@ -46,4 +46,14 @@ public function extend(Container $container, Extension $extension = null)
);
}
}

public function onEnable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}

public function onDisable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
}
15 changes: 14 additions & 1 deletion src/Locale/LocaleManager.php
Expand Up @@ -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
Expand Down Expand Up @@ -106,4 +112,11 @@ public function setTranslator(Translator $translator)
{
$this->translator = $translator;
}

public function clearCache()
{
if ($this->cacheDir) {
array_map('unlink', glob($this->cacheDir.'/*'));
}
}
}
27 changes: 24 additions & 3 deletions src/Locale/LocaleServiceProvider.php
Expand Up @@ -13,10 +13,10 @@

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
Expand All @@ -31,18 +31,34 @@ public function boot(Dispatcher $events)
$locales->addLocale($this->getDefaultLocale(), 'Default');

$events->dispatch(new ConfigureLocales($locales));

$events->listen(ClearingCache::class, function () use ($locales) {
$locales->clearCache();
});
}

/**
* {@inheritdoc}
*/
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());

Expand All @@ -59,4 +75,9 @@ private function getDefaultLocale(): string

return $repo->get('default_locale', 'en');
}

private function getCacheDir(): string
{
return $this->app->storagePath().'/locale';
}
}

1 comment on commit 0d16fac

@franzliedke
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful, thank you! I can take this off my list now. :)

Please sign in to comment.