How to set up language auto-redirecting

The best option would be in a catch-all route, which can be put into the /site/config/config.php file or into a plugin.

The alternative would be to rely on auto-detection

 'languages.detect' => true

(This would go into the return array in the config as well)

The result will then depended on the order of languages set by the user in the browser.

Thanks again for your help :slight_smile: Let me share you the spot with language.detect:

// language detection on the home page with / as URL
                if ($home && $kirby->url() !== $home->url()) {
                    if ($kirby->option('languages.detect') === true) {
                        return $kirby
                            ->response()
                            ->redirect($kirby->detectedLanguage()->url());
                    } else {
                        return $kirby
                            ->response()
                            ->redirect($kirby->site()->url());
                    }

And the spot with configure.php:

<?php
return [
  'languages'        => true,
  'languages.detect' => true,
  'debug' => false,
];

How could I add the condition to open the Spanish version with browsers set up in cat, basque or galician?

If you have automatic language detection set in the config, you don’t need anything else.

If you want to redirect to one URL (in this case /es) for more than one language, how do you do that? Now it only opens /es for Spanish

I’m sorry, somehow I don’t seem to understand the problem quite correctly. Let me try and explain what I understand.

You have two languages, Spanish and English.

And you want that visitors from all over Spain and probably Spanish speaking countries overseas land on the Spanish language version, i.e. yourdomain.com/es and the rest of world end up on the English URL yourdomain.com/en. Right?

With language.autodetect set, this should actually happen based on this information (from the source code)

 $this->ip($arguments['ip'] ?? $_SERVER['REMOTE_ADDR'] ?? '');
        $this->userAgent($arguments['userAgent'] ?? $_SERVER['HTTP_USER_AGENT'] ?? '');
        $this->acceptedLanguage($arguments['acceptedLanguage'] ?? $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '');
        $this->acceptedMimeType($arguments['acceptedMimeType'] ?? $_SERVER['HTTP_ACCEPT'] ?? '');

So if all goes well and you set your browser language to English and clear all cookies, you should end up on the English language version when you visit the home page.

Everything you said is correct with the exception that catalan, galizian and basque are different languages. We want to charge the url mydomain.com/es also for these three languages, instead of the /en, which is what happens now.

I know that these are all different languages. But what does that mean “charge the URL “es” for all three languages”? In my understanding that means that this URL and the language Spanish is presented to all Galician etc. speaking visitors.

But how do you test this? What is your browser language and region set to?

If automatic browser detection does not work, you need to set up a route with your custom logic.

  'routes' => [
    [
      'pattern' => '(:all)',
      'action'  => function () {
        // do something here
        // when the URL matches the pattern above
      }
    ],
  ]

The automatic language detection is working fine. The way the web is now set up is that if you have Spanish set in the browser, it opens the /es page. For any other language, it opens the /en site. What we want to change is, that when the browser has catalan, galizian or basque, it also opens the /es page.

So the logic function is:
If language.detected = ‘es’ or ‘cat’ or ‘eu’ or ‘gal’
Then open /es
Else open /en

My question is, which is the code I have to use and where must it be placed

Try this (not tested):

 'routes' => [
      [
          'pattern' => '(:all)',
          'action'  => function () {
              $kirby= kirby();

                if ( option('languages.detect') === true && in_array( $kirby->detectedLanguage()->code(), ['es', 'eu', 'ca', 'gl']) ) {
                
                  
                  return $kirby
                      ->response()
                      ->redirect(kirby()->url('es');
                }

                $this->next();
            }
        ]
    ]

This would go into the return array of the config

Hi there, your proposal worked fine! Thanks a lot. The exact code I’ve used is this one (I’ve integrated your function with the code I already had):
‘’’
// language detection on the home page with / as URL

            if ($home && $kirby->url() !== $home->url()) {

                if ($kirby->option('languages.detect') === true && in_array( $kirby->detectedLanguage()->code(), ['es','eu','ca','gl'])) {

                    return $kirby

                        ->response()

                        ->redirect(kirby()->url('es'));

                } else {

                    return $kirby

                        ->response()

                        ->redirect($kirby->site()->url());

                }

‘’’
Thanks for your support!