0

First install OctoberCMS using composer and git and then install translate plugin with two language English and Spanish. But I have an error with the localePicker. When I choose a language, I get the following error;

AJAX handler 'onSwitchLocale' was not found.

enter image description here

According to this thread, is a October related issue. But I just want to be sure, how can I resolve it?

Please note that mysite.com/{langcode}/ works.

Thanks

Mitul Koradiya
  • 318
  • 2
  • 5
  • 19

2 Answers2

2

The AJAX handler onswitchlocale was not found simply means you did not attach the component of RainLab.Translate to your page or layout

Add to your layout :

[localePicker]
forceUrl = 1
Raja Khoury
  • 3,015
  • 1
  • 20
  • 19
0

This is a 2021 update: the Ajax handler will be added automatically if you add the Locale Picker component to your layout, not partials or page, if the latter are based on a layout. This point is important. See screenshot.

Inside of page that invokes the layout, my HTML implementation is:

 {% if activeLocale == 'en' %}<a href="#" data-request="onSwitchLocale" data-request-data="locale: 'fr'"><img src="flgfr.png" title="Version française" /></a>{% endif %}
 {% if activeLocale == 'fr' %}<a href="#" data-request="onSwitchLocale" data-request-data="locale: 'en'"><img src="flgen.png" title="English version" /></a>{% endif %}

To be noted that clicking the image will only force the URL to /fr or /en if you tick the "force URL schema" option. enter image description here

Fabien Haddadi
  • 1,814
  • 17
  • 22
  • This is a 2023 update With v3: The above was valid for v2, but since v3, localePicker component was renamed sitePicker. If you only need a backend business code to run upon locale change, then just use the `data-request` / `data-request-data` HTML attribute. But if you want a simple redirection to `/`, like `/en` or `/fr`, then remove both extra HTML attrs, and use `` and ``, for example. BTW, no behaviour change was noted upon setting forceUrl to 1 or 0 with 3.x version... – Fabien Haddadi Jul 07 '23 at 17:56
  • If you're using the href method for switching locale, you don't even need to include the `sitePicker` component in your layout. – Fabien Haddadi Jul 07 '23 at 18:03
  • The most important point in 3.x, which is NOT documented, is the fact that you SHOULD NOT try to remove the `'en'` locale, i.e; English (US). This is the default locale, 'en' is hard-coded everywhere in OctoberCMS, and if you're trying to remove it and place en-au instead for example, you WILL get an "EN-AU" index not found in array... Took me hours to figure that out. – Fabien Haddadi Jul 07 '23 at 18:14