mirror of
https://github.com/flarum/core.git
synced 2025-07-18 23:31:17 +02:00
Format code
- Early returns - Comments - Write variables only when needed Refs #2020.
This commit is contained in:
@@ -16,6 +16,7 @@ use Flarum\Locale\LocaleManager;
|
|||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
use SplFileInfo;
|
||||||
|
|
||||||
class LanguagePack implements ExtenderInterface, LifecycleInterface
|
class LanguagePack implements ExtenderInterface, LifecycleInterface
|
||||||
{
|
{
|
||||||
@@ -55,7 +56,7 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface
|
|||||||
|
|
||||||
$container->resolving(
|
$container->resolving(
|
||||||
LocaleManager::class,
|
LocaleManager::class,
|
||||||
function (LocaleManager $locales) use ($extension, $locale, $title, $container) {
|
function (LocaleManager $locales, Container $container) use ($extension, $locale, $title) {
|
||||||
$this->registerLocale($container, $locales, $extension, $locale, $title);
|
$this->registerLocale($container, $locales, $extension, $locale, $title);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -81,22 +82,42 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface
|
|||||||
$locales->addCssFile($locale, $file);
|
$locales->addCssFile($locale, $file);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var ExtensionManager $extensions */
|
|
||||||
$extensions = $container->make(ExtensionManager::class);
|
|
||||||
|
|
||||||
foreach (new DirectoryIterator($directory) as $file) {
|
foreach (new DirectoryIterator($directory) as $file) {
|
||||||
$slug = $file->getBasename(".{$file->getExtension()}");
|
if ($this->shouldLoad($file, $container)) {
|
||||||
|
|
||||||
if (
|
|
||||||
$file->isFile()
|
|
||||||
&& in_array($file->getExtension(), ['yml', 'yaml'], true)
|
|
||||||
&& (in_array($slug, self::CORE_LOCALE_FILES, true) || $extensions->isEnabled($slug))
|
|
||||||
) {
|
|
||||||
$locales->addTranslations($locale, $file->getPathname());
|
$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()}");
|
||||||
|
|
||||||
|
if (in_array($slug, self::CORE_LOCALE_FILES, true)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var ExtensionManager $extensions */
|
||||||
|
static $extensions;
|
||||||
|
$extensions = $extensions ?? $container->make(ExtensionManager::class);
|
||||||
|
|
||||||
|
return $extensions->isEnabled($slug);
|
||||||
|
}
|
||||||
|
|
||||||
public function onEnable(Container $container, Extension $extension)
|
public function onEnable(Container $container, Extension $extension)
|
||||||
{
|
{
|
||||||
$container->make('flarum.locales')->clearCache();
|
$container->make('flarum.locales')->clearCache();
|
||||||
|
Reference in New Issue
Block a user