Fix language loading order: prioritize native OpenCart language loader and handle numeric language_id

This commit is contained in:
nertyog 2026-08-01 06:11:09 +03:00
parent 8b1d657fa6
commit 4d4095eecf
2 changed files with 76 additions and 40 deletions

View File

@ -6,7 +6,19 @@ class ControllerModuleImgOpti extends Controller {
private $translations = array();
public function index() {
$data = $this->loadLanguageSafe();
$data = array();
if (isset($this->load) && method_exists($this->load, 'language')) {
try {
$lang_data = $this->load->language('module/img_opti');
if (is_array($lang_data)) {
$data = array_merge($data, $lang_data);
}
} catch (Exception $e) {}
}
$fallback_data = $this->loadLanguageSafe();
if (is_array($fallback_data)) {
$data = array_merge($fallback_data, $data);
}
$this->document->setTitle($this->language->get('heading_title_raw'));
$this->load->model('setting/setting');
@ -2108,6 +2120,15 @@ class ControllerModuleImgOpti extends Controller {
private function loadLanguageSafe() {
$data = array();
if (isset($this->language) && is_object($this->language)) {
if (method_exists($this->language, 'all')) {
$all = $this->language->all();
if (is_array($all)) {
$data = array_merge($data, $all);
}
}
}
$en_paths = array(
DIR_LANGUAGE . 'en-gb/module/img_opti.php',
DIR_LANGUAGE . 'english/module/img_opti.php'
@ -2116,60 +2137,75 @@ class ControllerModuleImgOpti extends Controller {
if (is_file($path)) {
$_ = array();
require($path);
$data = array_merge($data, $_);
$data = array_merge($_, $data);
break;
}
}
$lang = $this->config->get('config_admin_language');
if (!$lang) {
$lang = $this->config->get('config_language');
}
$lang = '';
if (isset($this->session->data['language'])) {
$lang = $this->session->data['language'];
}
if (!$lang && $this->config->get('config_admin_language')) {
$lang = $this->config->get('config_admin_language');
}
if (!$lang && $this->config->get('config_language')) {
$lang = $this->config->get('config_language');
}
if (!$lang) {
$lang = 'en-gb';
}
$lang_lower = strtolower((string)$lang);
if ($lang_lower && $lang_lower !== 'en-gb' && $lang_lower !== 'english') {
$paths = array(
DIR_LANGUAGE . $lang . '/module/img_opti.php',
DIR_LANGUAGE . $lang_lower . '/module/img_opti.php'
);
if (isset($this->db)) {
try {
$paths = array();
if ($lang && $lang !== 'en-gb' && $lang !== 'english') {
$paths[] = DIR_LANGUAGE . $lang . '/module/img_opti.php';
$paths[] = DIR_LANGUAGE . strtolower($lang) . '/module/img_opti.php';
}
if (isset($this->db)) {
try {
$query_lang = null;
if (is_numeric($lang)) {
$query_lang = $this->db->query("SELECT directory, code FROM `" . DB_PREFIX . "language` WHERE language_id = '" . (int)$lang . "' LIMIT 1");
} else if ($lang) {
$escaped_lang = $this->db->escape($lang);
$query_lang = $this->db->query("SELECT directory FROM `" . DB_PREFIX . "language` WHERE code = '" . $escaped_lang . "' OR directory = '" . $escaped_lang . "' LIMIT 1");
if ($query_lang && $query_lang->num_rows && !empty($query_lang->row['directory'])) {
$query_lang = $this->db->query("SELECT directory, code FROM `" . DB_PREFIX . "language` WHERE code = '" . $escaped_lang . "' OR directory = '" . $escaped_lang . "' LIMIT 1");
}
if ($query_lang && $query_lang->num_rows) {
if (!empty($query_lang->row['directory'])) {
$db_dir = $query_lang->row['directory'];
$paths[] = DIR_LANGUAGE . $db_dir . '/module/img_opti.php';
$paths[] = DIR_LANGUAGE . strtolower($db_dir) . '/module/img_opti.php';
}
} catch (Exception $e) {}
}
if (strpos($lang_lower, 'ru') !== false || strpos($lang_lower, 'rus') !== false) {
$paths[] = DIR_LANGUAGE . 'ru-ru/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'russian/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ru-RU/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ru/module/img_opti.php';
} elseif (strpos($lang_lower, 'uk') !== false || strpos($lang_lower, 'ua') !== false || strpos($lang_lower, 'ukr') !== false) {
$paths[] = DIR_LANGUAGE . 'uk-ua/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ukrainian/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk-UA/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ua/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk_ua/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk_UA/module/img_opti.php';
}
foreach ($paths as $path) {
if (is_file($path)) {
$_ = array();
require($path);
$data = array_merge($data, $_);
break;
if (!empty($query_lang->row['code'])) {
$lang = $query_lang->row['code'];
}
}
} catch (Exception $e) {}
}
$lang_lower = strtolower((string)$lang);
if (strpos($lang_lower, 'ru') !== false || strpos($lang_lower, 'rus') !== false) {
$paths[] = DIR_LANGUAGE . 'ru-ru/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'russian/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ru-RU/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ru/module/img_opti.php';
} elseif (strpos($lang_lower, 'uk') !== false || strpos($lang_lower, 'ua') !== false || strpos($lang_lower, 'ukr') !== false) {
$paths[] = DIR_LANGUAGE . 'uk-ua/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ukrainian/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk-UA/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'ua/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk_ua/module/img_opti.php';
$paths[] = DIR_LANGUAGE . 'uk_UA/module/img_opti.php';
}
foreach ($paths as $path) {
if (is_file($path)) {
$_ = array();
require($path);
$data = array_merge($data, $_);
break;
}
}
foreach ($data as $key => $value) {