Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin tranlations loading #18699

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4681,6 +4681,12 @@
'count' => 2,
'path' => __DIR__ . '/src/Plugin.php',
];
$ignoreErrors[] = [
Copy link
Contributor

Choose a reason for hiding this comment

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

baseline is only present so we do not have to fax all existing issues; we should not add new ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code is a copy/paste from what's in src/Session.php, which is also in the baseline, that's why I tried to ignore it in the same way, to see if it's accepted, but apparently not 😢

Copy link
Contributor

Choose a reason for hiding this comment

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

I was not aware we already have an occurrence of that... But, the fact a code already exists does not mean it's correct :D
Keep in mind we'll have to fix phpstan issues one day.

'message' => '#^Property Laminas\\\\I18n\\\\Translator\\\\Translator\\:\\:\\$cache \\(Laminas\\\\Cache\\\\Storage\\\\StorageInterface\\|null\\) does not accept Glpi\\\\Cache\\\\I18nCache\\|null\\.$#',
'identifier' => 'assign.propertyType',
'count' => 1,
'path' => __DIR__ . '/src/Plugin.php',
];
$ignoreErrors[] = [
'message' => '#^Call to function method_exists\\(\\) with \\$this\\(Printer\\) and \'prepareGroupFields\' will always evaluate to true\\.$#',
'identifier' => 'function.alreadyNarrowedType',
Expand Down
11 changes: 11 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

use Glpi\Application\View\TemplateRenderer;
use Glpi\Cache\CacheManager;
use Glpi\Cache\I18nCache;
use Glpi\Dashboard\Grid;
use Glpi\Marketplace\Controller as MarketplaceController;
use Glpi\Marketplace\View as MarketplaceView;
Expand Down Expand Up @@ -476,6 +477,16 @@ public static function loadLang($plugin_key, $forcelang = '', $coretrytoload = '
}
}

$i18n_cache = !defined('TU_USER') ? new I18nCache((new CacheManager())->getTranslationsCacheInstance()) : null;
$TRANSLATE = new class ($i18n_cache) extends Laminas\I18n\Translator\Translator {
public function __construct(?I18nCache $cache)
{
$this->cache = $cache;
}
};

$TRANSLATE->setLocale($trytoload);

if ($mofile !== false) {
$TRANSLATE->addTranslationFile(
'gettext',
Expand Down
Loading