Skip to content

Course: Apply user language if course allows it - refs BT#22598 #6264

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

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
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
113 changes: 43 additions & 70 deletions src/CoreBundle/EventSubscriber/LocaleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,46 @@

use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Settings\SettingsManager;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Checks the portal listener depending of different settings:
* platform, user, course.
* Handles locale selection based on platform, user, and course settings.
*/
class LocaleSubscriber implements EventSubscriberInterface
{
protected string $defaultLocale;
protected ParameterBagInterface $parameterBag;
protected SettingsManager $settingsManager;

public function __construct(string $defaultLocale, SettingsManager $settingsManager, ParameterBagInterface $parameterBag)
{
$this->defaultLocale = $defaultLocale;
$this->settingsManager = $settingsManager;
$this->parameterBag = $parameterBag;
public function __construct(
private string $defaultLocale,
private SettingsManager $settingsManager,
private ParameterBagInterface $parameterBag,
private SettingsCourseManager $courseSettingsManager
) {
}

public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
/*if (!$request->hasPreviousSession()) {
return;
}*/

$installed = false;
if ($this->parameterBag->has('installed')) {
$installed = 1 === (int) $this->parameterBag->get('installed');
}

if (!$installed) {
return;
}

if (!$request->hasSession()) {
$installed = $this->parameterBag->has('installed') && 1 === (int) $this->parameterBag->get('installed');
if (!$installed || !$request->hasSession()) {
return;
}

$sessionHandler = $request->getSession();

// Override locale if forced via ?_locale=xx
if ($attrLocale = $request->query->get('_locale')) {
$sessionHandler->set('_selected_locale', $attrLocale);
}

// Determine locale based on priority logic
$locale = $this->getCurrentLanguage($request);
// if no explicit locale has been set on this request, use one from the session

// Apply locale to request and session
$request->setLocale($locale);
$sessionHandler->set('_locale', $locale);
}
Expand All @@ -68,73 +57,57 @@ public function getCurrentLanguage(Request $request): string
$sessionHandler = $request->getSession();
$localeList = [];

// 1. Check platform locale;
// 1. Platform default locale
if ($platformLocale = $this->settingsManager->getSetting('language.platform_language')) {
$localeList['platform_lang'] = $platformLocale;
}

// 2. Check user locale
// _locale_user is set when user logins the system check UserLocaleListener
// 2. User profile locale from session
if ($userLocale = $sessionHandler->get('_locale_user')) {
$localeList['user_profil_lang'] = $userLocale;
}

// 3. Check course locale
if ($request->query->getInt('cid')
|| $request->request->getInt('cid')
|| $request->attributes->getInt('cid')
) {
/** @var Course|null $course */
// 3. Check course locale
if ($course = $sessionHandler->get('course')) {
if ($courseLocale = $course->getCourseLanguage()) {
$localeList['course_lang'] = $courseLocale;
}
// 3. Course locale or user locale if course allows user language
$course = $sessionHandler->get('course');
if ($course instanceof Course) {
$userLocale = $localeList['user_profil_lang'] ?? null;
$courseLocale = $course->getCourseLanguage();

$this->courseSettingsManager->setCourse($course);
if ($this->courseSettingsManager->getCourseSettingValue('show_course_in_user_language') === '1' && $userLocale) {
$localeList['course_lang'] = $userLocale;
} elseif ($courseLocale) {
$localeList['course_lang'] = $courseLocale;
}
}

// 4. force locale if it was selected from the URL
// 4. Locale selected manually via URL
if ($localeFromUrl = $sessionHandler->get('_selected_locale')) {
$localeList['user_selected_lang'] = $localeFromUrl;
}

$priorityList = [
'language_priority_1',
'language_priority_2',
'language_priority_3',
'language_priority_4',
];

$locale = '';
foreach ($priorityList as $setting) {
$priority = $this->settingsManager->getSetting(\sprintf('language.%s', $setting));
// 5. Resolve locale based on configured language priorities
foreach ([
'language_priority_1',
'language_priority_2',
'language_priority_3',
'language_priority_4',
] as $settingKey) {
$priority = $this->settingsManager->getSetting("language.$settingKey");
if (!empty($priority) && !empty($localeList[$priority])) {
$locale = $localeList[$priority];

break;
return $localeList[$priority];
}
}

if (empty($locale)) {
// Use default order
$priorityList = [
'platform_lang',
'user_profil_lang',
'course_lang',
'user_selected_lang',
];
foreach ($priorityList as $setting) {
if (!empty($localeList[$setting])) {
$locale = $localeList[$setting];
}
// 6. Fallback order if priorities are not defined
foreach (['platform_lang', 'user_profil_lang', 'course_lang', 'user_selected_lang'] as $key) {
if (!empty($localeList[$key])) {
return $localeList[$key];
}
}

if (empty($locale)) {
$locale = $this->defaultLocale;
}

return $locale;
// 7. Final fallback to system default
return $this->defaultLocale;
}

public static function getSubscribedEvents(): array
Expand Down
48 changes: 47 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,52 @@ Encore.copyFiles({
to: "libs/select2/js/[name].[ext]",
})

const config = Encore.getWebpackConfig()
const fs = require("fs")
const path = require("path")
class CopyUnhashedAssetsPlugin {
apply(compiler) {

Choose a reason for hiding this comment

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

Function apply has 33 lines of code (exceeds 25 allowed). Consider refactoring.

compiler.hooks.afterEmit.tap("CopyUnhashedAssetsPlugin", (compilation) => {
const buildPath = path.resolve(__dirname, "public/build")

// === COPY legacy_document.js without hash ===
const legacyDocumentFile = fs.readdirSync(buildPath).find((f) =>
f.match(/^legacy_document\.[a-f0-9]+\.js$/)
)
if (legacyDocumentFile) {
fs.copyFileSync(
path.join(buildPath, legacyDocumentFile),
path.join(buildPath, "legacy_document.js")
)
}

// === COPY runtime.js without hash ===
const runtimeFile = fs.readdirSync(buildPath).find((f) =>
f.match(/^runtime\.[a-f0-9]+\.js$/)
)
if (runtimeFile) {
fs.copyFileSync(
path.join(buildPath, runtimeFile),
path.join(buildPath, "runtime.js")
)
}

// === COPY document.css without hash ===
const cssPath = path.join(buildPath, "css")
if (fs.existsSync(cssPath)) {
const documentCssFile = fs.readdirSync(cssPath).find((f) =>
f.match(/^document\.[a-f0-9]+\.css$/)
)
if (documentCssFile) {
fs.copyFileSync(
path.join(cssPath, documentCssFile),
path.join(cssPath, "document.css")
)
}
}
})
}
}

Encore.addPlugin(new CopyUnhashedAssetsPlugin())
const config = Encore.getWebpackConfig()
module.exports = config
Loading