Bi-Lingual website using Contexts and routing plugin

Summary

I am trying to set up a bi-lingual website for English and Macedonian (yes, you read that correctly. Our church has an outreach to North Macedonia.)

I am working on XAMPP 8.2.12 ( Apache 2.4.58; MariaDB 10.4.32; PHP 8.2.12 (VS16 X86 64bit thread safe) + PEAR; phpMyAdmin 5.2.1) on my Windows 11 laptop, and I have installed MODX 3.2.0.

I have read that there are two ways to do this, both using a different Context for each language, but one using a gateway plugin to switch contexts and the other using the XRouting plugin. I have tried both, and am experiencing a similar problem with both.

I’ll give the details of the XRouting attempt.

(NB. I had lots of screen dumps, but I was only allowed to use one!)

Step to reproduce

I have set up two Contexts, ‘eng’ and ‘mak’ in addition to ‘web’, with Context Settings as follows:

Under Area: (none)

base_url /en/ or /mk/

http_host localhost/bandf2

site_start 2 or 3

site_url http://_localhost/bandf2/en/ or …/mk/ {I inserted the underline here so that it did not create a link in this post}

Under Area: Lexicon and Language

cultureKey en or mk

I have created two subfolders, ‘en’ and ‘mk’, into which I have copied .htaccess, index.php, and config.core.php, and amended the English one as follows, with the equivalent for the Macedonian one:

.htaccess

RewriteBase /bandf2/en/

Index.php

if (!defined(‘MODX_CORE_PATH’)) {
define(‘MODX_CORE_PATH’, dirname(dirname(FILE)) . ‘/core/’);
}

/* Initialize a context - this one is for ‘eng’ English Language */
$contextKey = ‘eng’;

Config.core.php already had the correct core path and did not need to change

define(‘MODX_CORE_PATH’, ‘C:\xampp\htdocs\bandf2/core/’);

I set up a home page for each of the new Contexts: English no.2, Macedonian no. 3

In the Home(1) page I have entered a link to each of the other two home pages:

English home page - href=”[[~2]]

Macedonian Home Page - href=”[[~3]]

I have installed the current version of XRouting - 1.4.1-pl, with system events set including OnHandleRequest

Observed behavior

At this point, I would assume that we are ready to go.

· I have my Contexts

· I have my subfolders

· I have my home pages

· I have my routing plugin

However, when I go to my ‘web’ Home Page (1) and click on [view] I get the following:

The URL should be: localhost/bandf2/index

On the other test, where I was using the gateway plugin and amended .htaccess, it came up with a similar error, but the displayed URL was: localhost/mk

Expected behavior

The problem would appear to be that whatever is building the URL is doing it incorrectly.

I would suspect that there is something missing in my build, but I have followed the instructions that I found online for this:

Using XRouting for multilingual websites in MODX | MODX.today

Multilingual websites with MODX - Nathanael McMillan

Any hints, tips, or other guidance would be much appreciated.

Thanks in advance to anyone who can help.

Environment

MODX 3.2.0 running on XAMPP 8.2.12 ( Apache 2.4.58; MariaDB 10.4.32; PHP 8.2.12 (VS16 X86 64bit thread safe) + PEAR; phpMyAdmin 5.2.1) on my Windows 11 laptop.

I forgot to note that I set up Friendly URLs.

There are different ways to create a multi-lingual site in MODX, and it’s important not to mix several approaches.

Currently for a multi-lingual site, I would recommend using the LangRouter extra (and then Babel).

Use these instructions from the LangRouter documentation to set up the contexts:


Alternatively you could use SmartRouting (as a replacement for LangRouter). SmartRouting is a rewrite of XRouting, which is no longer maintained.

The context-setup is a bit different than for LangRouter:


It might help to set up a VirtualHost, so that you can access the site with http://somedomain.com/ locally, instead of using http://localhost/bandf2/, as the base_url setting may be used for the ‘virtual’ language subfolders in addition to the real subfolder bandf2.

For most approaches (including the ones with LangRouter and SmartRouting), you don’t need these subfolders.

1 Like

Thanks for this. I’ll look into those options.

I am now having some success, but still have an issue.

I have set up a fresh installation of MODX 3.2.0 on my online test site ( mytest.net /bandfg) and installed LangRouter.

Following the instruction for LangRouter, I have created my two new Contexts (‘eng’ and ‘mak’) and created a site_start Home Page in each one.

The system is correctly building the URL for each page, but when I click on the link to the ‘en’ or ‘mk’ Home Page, it re-displays the ‘web’ Home Page.

After some trial and error with several configurations, most of which made matters even worse, I have done the following:

· As ‘web’ is the babel.contextDefault, I have given it the ‘en’ contextKey and reset the ‘eng’ contextKey to ‘de’.

· In Settings, I have updated ‘langrouter.contextKeys’ to: web,eng,mak

· I have created three additional test pages, one under each Context, with links to all other pages.

The result now is as follows:

· I can successfully access the ‘web’ Home Page.

· I can successfully access the three Test Pages (‘web’, ‘eng’, and ‘mak’).

· When I try to access the ‘eng’ or ‘mak’ Home Pages I get a “too many redirects” error message.

I tried entering the cultureKeyAliases parameter in each Context, but that made the situation worse.

Any ideas?

Update: My English Home Page is (2) and Test Page is (5). Under the Context, I changed site_start from 2 to 5. I can now access Home Page (2), but get the redirect message when trying to access (5).

It’s quite hard to determine from your description what exactly goes wrong.


So you created a VirtualHost? Why do you still access the site via the bandfg subfolder (http://mytest.net/bandfg) and didn’t use the bandfg folder as the DocumentRoot for the virtual host?

When you install MODX in a subfolder, the system setting base_url is set to the subfolder → /bandfg/. (See the value of MODX_BASE_URL in the file core/config/config.inc.php.)
Now if you set base_url in the context-settings to a different value, you might create problems (as the context-setting overwrites the system-setting with the same key).


I don’t quite understand why you have 3 contexts. If your main page is in english, just add one new context for macedonian.


In the LangRouter documentation there is this warning about redirects:

Please don’t activate the friendly_urls_strict MODX system setting, if you use LangRouter. That can cause nasty redirect loops.

This you check that system setting?

Also maybe the langrouter.debug setting can give you more insight into why the redirects occur.

Firstly, apologies for not communicating as clearly as I should have, but I wanted to be brief.

  1. I didn’t create a virtual host. I have a test website called ‘webmattics{.}net’, so I created a subfolder called ‘bandfg’ there for this test. (I said ‘mytest{.}net’ because it seems to be a thing to not give your actual website address on a forum like this.)
  2. friendly_urls_strict was NOT set.
  3. Using ‘en’ under ‘web’ and ‘de’ under ‘eng’ was an experiment. I tried using ‘fr’ under ‘web’, but it made matters worse, but when I made ‘web’ = ‘en’ and changed ‘eng’ to something else, it worked.
  4. Having ‘eng’ and ‘mak’ as well as ‘web’ is a personal preference, but if it won’t work properly that way, then I’ll go for ‘web’ with ‘en’ and ‘mak’ with ‘mk’.
  5. I set the LangRouter debug to Yes and got the error log. It showed the re-routing, but I’m still not clear on why it’s happening.
  6. Finally, I had a brainwave at 4 o’clock this morning. I have set the ‘site_start’ for ALL Contexts to ‘1’. I can now access ALL pages seamlessly.
  7. The only problem would be if someone requests webmattics{.}net/bandfg/mk, which renders “page not found”, or webmattics{.}net/bandfg/mk/, which renders the redirects message. webmattics{.}net/mk/mk-index.html successfully renders the ‘mak’ home page.

Is there any benefit in showing you the debug report? Would you be able to work out from it what’s going on behind the scenes?

Thanks again for your help.

​​​

​​​​​​​​​

I’m not sure but I suspect that LangRouter expects a different language for every context. So if you use “web”, “eng” and “mak” what’s the cultureKey for “web”? Is it the same as for “eng”? How can LangRouter separate the two contexts?

Using a universal router plugin (like LangRouter or SmartRouting) is great, because you don’t have to code the router plugin yourself. But these tools require specific setups (context-settings) to work properly. For unusual requirements/setups, you maybe have to code a router plugin yourself.

I thought you were testing it locally with XAMPP, and not on a server.
The problem still remains, that having MODX installed in a subfolder, messes up the proposed context-setting for base_url you see in the documentation/tutorials.
So what value do you use for base_url in the context-settings of your 3 contexts?

Probably not.
I guess it would be more helpful to know all the context-settings you added for the 3 contexts.

Problem solved.

I have started from scratch again with a fresh install, using ‘web’ Context with ‘en’ Culture Key and ‘mak’ Context with ‘mk’ Culture Key.

The installation is in a subfolder: {domain-name}/bandfg

In the ‘mak’ Context, I have set base_url as /bandfg/mk/

This works perfectly.

Thanks to Harry for guidance and suggestions.

Onward and upward!

2 Likes

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.