Skip to content

Commit

Permalink
Updating how we load language files from plugins and caching them
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Nov 27, 2024
1 parent 8b804d4 commit 87e3774
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
9 changes: 9 additions & 0 deletions app/Core/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ public function __(string $index, string $default = ''): string
return (string) $returnValue;
}

public function mergeLanguageArray($newLanguageArray)
{

if (is_array($newLanguageArray)) {
$this->ini_array = array_merge($this->ini_array, $newLanguageArray);
}

}

public function get(string $index, $default = '', $locale = '')
{
$contentReplacement = '';
Expand Down
4 changes: 3 additions & 1 deletion app/Domain/Plugins/Services/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ public function disablePlugin(int $id): bool

$pluginModel = $this->pluginRepository->getPlugin($id);

$result = $this->pluginRepository->disablePlugin($id);

if ($pluginModel->format == 'phar') {
$phar = new \Phar(
Str::finish($this->pluginDirectory, DIRECTORY_SEPARATOR)
Expand All @@ -337,7 +339,7 @@ public function disablePlugin(int $id): bool
}
}

return $this->pluginRepository->disablePlugin($id);
return $result;
}

/**
Expand Down
86 changes: 42 additions & 44 deletions app/Domain/Plugins/Services/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Leantime\Core\Configuration\Environment;
use Leantime\Core\Events\EventDispatcher;
use Leantime\Core\Language;
Expand Down Expand Up @@ -35,66 +34,63 @@ public function registerLanguageFiles(array $languages)

$pluginId = $this->pluginId;

EventDispatcher::add_event_listener('leantime.core.middleware.loadplugins.handle.pluginsEvents', function () use ($pluginId, $languages) {
EventDispatcher::add_event_listener('leantime.core.middleware.loadplugins.handle.pluginsEvents', function () use ($languages) {

$language = app()->make(Language::class);
$config = app()->make(Environment::class);
$currentUserLanguage = session('usersettings.language');

//At this point in the stack localization has already determined user language and set up the core language
//array in the language of the users choice. First we register english if it is in the array and then we
//override with the user language
if (in_array('en-US', $languages)) {
$pluginLangArray = $this->loadPluginLanguage('en-US');
$language->mergeLanguageArray($pluginLangArray);
}

try {
$languageArray = Cache::store('installation')->get($pluginId.'.languageArray', []);
} catch (\Exception $e) {
Log::error($e);
$languageArray = [];
//Now check the user language and override if needed
if (in_array($currentUserLanguage, $languages)) {
$pluginLangArray = $this->loadPluginLanguage($currentUserLanguage);
$language->mergeLanguageArray($pluginLangArray);
}

if (is_array($languageArray) && count($languageArray) > 0) {
$language->ini_array = array_merge($language->ini_array, $languageArray);
}, 5);

return;
}
}

//Always load en-Us as this is the default fallback language
if (! Cache::store('installation')->has($pluginId.'.language.en-US')) {
private function loadPluginLanguage($language): array|false
{

if (file_exists(app_path().'/Plugins/'.$pluginId.'/Language/en-US.ini')) {
$languageArray += parse_ini_file(
app_path().'/Plugins/'.$pluginId.'/Language/en-US.ini',
true
);
}
}
if (Cache::store('installation')->has($this->pluginId.'.language.'.$language)) {
return Cache::store('installation')->get($this->pluginId.'.language.'.$language);
}

if ((($userLanguage = session('usersettings.language') ?? $config->language) !== 'en-US') && in_array($userLanguage, $languages)) {
$pluginPath = APP_ROOT.'/app/Plugins/';

if (! Cache::store('installation')->has($pluginId.'.language.'.$userLanguage)) {
$pharPath = "phar://{$pluginPath}{$this->pluginId}/{$this->pluginId}.phar";
$regularPath = "{$pluginPath}{$this->pluginId}";

if (file_exists(app_path().'/Plugins/'.$pluginId.'/Language/'.$userLanguage.'.ini')) {
Cache::store('installation')->put(
$pluginId.'.language.'.$userLanguage,
parse_ini_file(app_path().'/Plugins/'.$pluginId.'/Language/'.$userLanguage.'.ini', true),
Carbon::now()->addDays(30)
);
}
}
$languagePath = "/Language/{$language}.ini";

$languageArray = array_merge($languageArray, Cache::store('installation')->get($pluginId.'.language.'.$language));
//Check phar first
if (file_exists($pharPath.$languagePath)) {
$completeLanguagePath = $pharPath.$languagePath;
} elseif (file_exists($regularPath.$languagePath)) {
$completeLanguagePath = $regularPath.$languagePath;
} else {
//Language file doesn't exist
Cache::store('installation')->set($this->pluginId.'.language.'.$language, false, Carbon::now()->addDays(7));

$cachedLangArr = Cache::store('installation')->get($pluginId.'.language.'.$language, []);
$languageArray = array_merge(
is_array($languageArray) ? $languageArray : [],
is_array($cachedLangArr) ? $cachedLangArr : []
);
}
return false;
}

try {
Cache::store('installation')->put($pluginId.'.languageArray', $languageArray);
} catch (\Exception $e) {
Log::error($e);
}
$languageArray = parse_ini_file($completeLanguagePath, true);

$language->ini_array = array_merge($language->ini_array, $languageArray);
//We're caching the results no matter what, language file is not going to magically appear.
//So even a false is valid as parse_ini_is too expensive to run every time
Cache::store('installation')->set($this->pluginId.'.language.'.$language, $languageArray, Carbon::now()->addDays(7));

}, 5);
return $languageArray;

}

Expand Down Expand Up @@ -154,4 +150,6 @@ public function addHeaderJs(array $middleware) {}
public function addFooterJs(array $middleware) {}

public function addCss(array $middleware) {}

protected function getPluginPath() {}
}

0 comments on commit 87e3774

Please sign in to comment.