MODX + Babel (with 3 languages - ua, ru. en)

Hello dear. I ask for your help. I have never worked with MODX before. I have worked a lot with other CMS. But now at the new job they asked to fix a very, very old site. There - MODX + Babel (for multilingualism). The default language was RU (web) (in the figure in the attachment - 1.2). And two additional languages ​​- Ukrainian and English (in the figure in attachments 1.1 and 1.3). They asked to make Ukrainian the default language. No prefix. And the URLs are English and Russian, respectively, with the prefixes / en / and / ru /.

I searched Google for a solution. In the context settings, I set the values ​​as seen in the picture. In the site settings, I set the main page “3”, as you can see in the attachment for part (2). This is the main context page for UA (1.1). In /index.php I registered $ modx-initialize (“ua”), that is, the Ukrainian context. That’s it, it doesn’t load at all. Timed Out. Moreover, now, if I return $ modx-initialize (“web”), the “Russian” site is loaded. But / ukr / is appended to the name. And it’s impossible to switch to Ukrainian at all. Website http://healthyfuture.com.ua/. Help me please. What am I doing wrong?

What plugin (LangRouter, XRouting) do you use to switch between the contexts?
If you use a custom plugin then maybe you have to change it too.

Thanks for your reply. I also looked at the self-written chunk, but the languages are displayed correctly and the links are also displayed correctly (on the picture below).

switcher

And why the prefix / ukr / is added - I remembered. At the beginning of the fix, I was advised to add the following lines to .htaccess:

Force language when requesting the root (/)

RewriteCond% {HTTP: Accept-Language} ^ ukr [NC]
RewriteRule ^ $ / ukr / [R = 301, L]

RewriteCond% {HTTP: Accept-Language}! ^ Ukr [NC]
RewriteRule ^ $ / ukr / [R = 301, L]

I added, nothing worked. I removed them. But the cache is stored somewhere :frowning: Complex CMS :frowning:

I wasn’t talking about a chunk!
There has to be a plugin (in the tab “Elements” under “Plugins”) that runs on the event OnHandleRequest to load the correct context.

Found. Some kind of unofficial plugin

The code is kind of hard to read (maybe you can post it in a code block and not in a picture), but I think you probably have to change it.

Make sure that when it finds no matching base_url, that it then switches to the “ua” context by default.

<?php
// We work only on the frontend and only with friendly URLs
if ($modx->event->name != 'OnHandleRequest' || $modx->context->key == 'mgr' || !$modx->getOption('friendly_urls')) {return;}

// We get the requested url
$alias = $modx->getOption('request_param_alias', null, 'alias', true);
$request = &$_REQUEST[$alias];

// Choosing contexts to the setting base_url
$q = $modx->newQuery('modContextSetting', array('key' => 'base_url', 'value:!=' => ''));
$q->select('context_key,value');

$contexts = array();
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
	//  Saves our query in the database
	$modx->queryTime += microtime(true) - $tstart;
	$modx->executedQueries++;
	// Parse the results
	while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
		$base_url = trim($row['value'], '/');
		$context = $row['context_key'];
		// ЕIf the request starts with the base_url of some context 
		if (preg_match('/^('.$base_url.')\//i', $request)) {
			// Then we switch to this context
			// Web is initialized in index.php - you don't need to switch to it
			if ($context != 'web') {
				$modx->switchContext($context);
			}
			// Cut out the base_url from the request so that MODX finds the resource by uri
			$request = preg_replace('/^'.$base_url.'\//', '', $request);
			// The deed is done - exit the loop
			break;
		}
	}
}

You could try changing the code like this. Maybe that works:

...
	$found_base_url = false; //<---- NEW
	while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
		$base_url = trim($row['value'], '/');
		$context = $row['context_key'];
		if (preg_match('/^('.$base_url.')\//i', $request)) {
		    $found_base_url = true; //<---- NEW
			if ($context != 'web') {
				$modx->switchContext($context);
			}
			$request = preg_replace('/^'.$base_url.'\//', '', $request);
			break;
		}
	}
	
	if (!$found_base_url){ //<---- NEW
	    $modx->switchContext('ua'); //switch to context 'ua' by default
	}
}

Thank you! You gave a good idea. Now everything is working.