Skip to content
Merged
Changes from all 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
197 changes: 195 additions & 2 deletions everblock.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function install()
$hook->description = 'This hook triggers before special event block is rendered';
$hook->save();
}
return (parent::install()
$installed = parent::install()
&& $this->registerHook('displayHeader')
&& $this->registerHook('actionAdminControllerSetMedia')
&& $this->registerHook('actionRegisterBlock')
Expand All @@ -197,8 +197,14 @@ public function install()
&& $this->installModuleTab('AdminEverBlockConfiguration', 'AdminEverBlockParent', $this->l('Configuration'))
&& $this->installModuleTab('AdminEverBlock', 'AdminEverBlockParent', $this->l('HTML Blocks'))
&& $this->installModuleTab('AdminEverBlockHook', 'AdminEverBlockParent', $this->l('Hooks'))
&& $this->installModuleTab('AdminEverBlockShortcode', 'AdminEverBlockParent', $this->l('Shortcodes')))
&& $this->installModuleTab('AdminEverBlockShortcode', 'AdminEverBlockParent', $this->l('Shortcodes'))
&& $this->installModuleTab('AdminEverBlockFaq', 'AdminEverBlockParent', $this->l('FAQ'));

if ($installed) {
$this->importLegacyTranslations();
}
Comment on lines 197 to +205

Choose a reason for hiding this comment

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

[P1] Migrate existing installations before switching to Symfony translator

The new install path calls importLegacyTranslations() only when the module is installed (lines 191‑205), yet l() now delegates to Symfony’s translator and looks up strings in the translation table (lines 243‑249). Existing shops upgrading from an earlier version will never execute install(), so none of the legacy translations/*.php entries are imported before the lookup mechanism changes. After the upgrade every localized label falls back to English because the database has no records. Consider running the migration from an upgrade routine or keeping the legacy loader as a fallback until the data has been migrated.

Useful? React with 👍 / 👎.


return $installed;
}

public function uninstall()
Expand Down Expand Up @@ -234,6 +240,193 @@ public function uninstall()
&& $this->uninstallModuleTab('AdminEverBlockFaq'));
}

public function l($string, $specific = null, $idLang = null)
{
return Context::getContext()->getTranslator()->trans(
$string,
[],
$this->getTranslationDomain($specific)
);
}

private function getTranslationDomain($specific = null)
{
$domainKey = $specific ? $specific : $this->name;

return sprintf('Modules.%s.%s', Tools::ucfirst($this->name), $this->normalizeDomainKey($domainKey));
}

private function normalizeDomainKey($key)
{
$key = trim((string) $key);

if ($key === '') {
$key = $this->name;
}

$key = str_replace(['-', '.'], '_', $key);
$key = preg_replace('/[^A-Za-z0-9_]/', '', $key);
$key = Tools::strtolower($key);

return Tools::ucfirst($key);
}

private function importLegacyTranslations()
{
$legacyDir = dirname(__FILE__) . '/translations';

if (!is_dir($legacyDir)) {
return;
}

$defaultMap = $this->loadDefaultLegacyTranslations($legacyDir);

if (empty($defaultMap)) {
return;
}

foreach (Language::getLanguages(false) as $language) {
$legacyFile = $this->resolveLegacyFileForIso($legacyDir, $language['iso_code']);

if (!$legacyFile) {
continue;
}

$legacyTranslations = $this->loadLegacyTranslationsFromFile($legacyFile);

if (empty($legacyTranslations)) {
continue;
}

foreach ($legacyTranslations as $legacyKey => $translatedValue) {
if (!isset($defaultMap[$legacyKey])) {
continue;
}

$domain = $this->buildDomainFromLegacyKey($legacyKey);

if (!$domain) {
continue;
}

$source = $defaultMap[$legacyKey];

if ($source === $translatedValue || $source === '') {
continue;
}

$this->upsertTranslation(
(int) $language['id_lang'],
$domain,
$source,
$translatedValue
);
}
}
}

private function loadDefaultLegacyTranslations($legacyDir)
{
$candidates = ['en.php', 'gb.php', 'us.php'];

foreach ($candidates as $candidate) {
$path = $legacyDir . '/' . $candidate;

if (is_file($path)) {
return $this->loadLegacyTranslationsFromFile($path);
}
}

foreach (glob($legacyDir . '/*.php') as $file) {
if (basename($file) === 'index.php') {
continue;
}

return $this->loadLegacyTranslationsFromFile($file);
}

return [];
}

private function resolveLegacyFileForIso($legacyDir, $isoCode)
{
$iso = Tools::strtolower($isoCode);
$candidates = [$iso . '.php'];

if ($iso === 'en') {
$candidates[] = 'gb.php';
$candidates[] = 'us.php';
}

foreach ($candidates as $candidate) {
$path = $legacyDir . '/' . $candidate;

if (is_file($path)) {
return $path;
}
}

return null;
}

private function loadLegacyTranslationsFromFile($path)
{
if (!is_file($path)) {
return [];
}

$backup = isset($GLOBALS['_MODULE']) ? $GLOBALS['_MODULE'] : null;
$GLOBALS['_MODULE'] = [];

include $path;

$translations = isset($GLOBALS['_MODULE']) && is_array($GLOBALS['_MODULE'])
? $GLOBALS['_MODULE']
: [];

if ($backup !== null) {
$GLOBALS['_MODULE'] = $backup;
} else {
unset($GLOBALS['_MODULE']);
}

return $translations;
}

private function buildDomainFromLegacyKey($legacyKey)
{
if (strpos($legacyKey, '>') === false) {
return null;
}

$parts = explode('>', $legacyKey, 2);

if (!isset($parts[1])) {
return null;
}

$domainPart = $parts[1];
$segments = explode('_', $domainPart);

if (empty($segments)) {
return null;
}

$domainKey = $segments[0];

return sprintf('Modules.%s.%s', Tools::ucfirst($this->name), $this->normalizeDomainKey($domainKey));
}

private function upsertTranslation($idLang, $domain, $source, $translation)
{
Db::getInstance()->execute(
'INSERT INTO `' . _DB_PREFIX_ . "translation` (`id_lang`, `domain`, `key`, `translation`, `theme`, `modified`)"
. " VALUES ("
. (int) $idLang . ", '" . pSQL($domain) . "', '" . pSQL($source) . "', '" . pSQL($translation, true) . "', NULL, 0)"
. " ON DUPLICATE KEY UPDATE `translation` = VALUES(`translation`), `modified` = VALUES(`modified`)"
);
}

protected function installModuleTab($tabClass, $parent, $tabName)
{
$tab = new Tab();
Expand Down
Loading