How to set up language auto-redirecting

Hi there,

I’m struggling with how to set up various Spanish languages to charge the es-version of the website (common Spanish->Castilian, Galego, Catalan, Basque…). All other languages should be redirected to the English version.

So the English version has this set up and works fine:

<?php

return array (

  'code'      => 'en',

  'default'   => false,

  'direction' => 'ltr',

  'locale'    => 'en_US',

  'name'      => 'English',

  'url'       => '/en',

  'translations' => [

    'email-placeholder' => 'yourname@email.com',

    'footer-help'       => 'Help & Support',

  ]

);

For the Spanish version I've tried with these code lines, but the Web does not work fine with that:

<?php

return array (

  'code'      => 'es',

  'default'   => true,

  'direction' => 'ltr',

  'locale'    => 'es_ES',

  'name'      => 'Spanish',

  'url'       => '/es',

  'translations' => [

    'email-placeholder' => 'tunombre@email.com',

    'footer-help'       => 'Ayuda & Soporte',

  ]

  );

  return array (

    'code'      => 'eu',

    'default'   => true,

    'direction' => 'ltr',

    'locale'    => 'eu_ES',

    'name'      => 'Basque',

    'url'       => '/es',

    'translations' => [

      'email-placeholder' => 'tunombre@email.com',

      'footer-help'       => 'Ayuda & Soporte',

    ]

    );

  

  return array (

    'code'      => 'ca',

    'default'   => true,

    'direction' => 'ltr',

    'locale'    => 'ca_ES',

    'name'      => 'Catalan',

    'url'       => '/es',

    'translations' => [

      'email-placeholder' => 'tunombre@email.com',

      'footer-help'       => 'Ayuda & Soporte',

    ]

    );

  

  return array (

    'code'      => 'gl',

    'default'   => true,

    'direction' => 'ltr',

    'locale'    => 'gl_ES',

    'name'      => 'Galician',

    'url'       => '/es',

    'translations' => [

      'email-placeholder' => 'tunombre@email.com',

      'footer-help'       => 'Ayuda & Soporte',

    ]

  );

Only with the first command lines for “es”, it does work fine. I have an issue when I try to add the other Spanish languages…

What do you mean?

I don’t really understand the problem because these are languages, not Spanish and have their own language codes?

If I only put these code lines, it works fine auto-charge the Spanish version in browser with Spanish language set up:

<?php

return array (

  'code'      => 'es',

  'default'   => true,

  'direction' => 'ltr',

  'locale'    => 'es_ES',

  'name'      => 'Spanish',

  'url'       => '/es',

  'translations' => [

    'email-placeholder' => 'tunombre@email.com',

    'footer-help'       => 'Ayuda & Soporte',

  ]

  );

The problem shows up when I try to add Catalan, Basque and Galician…

Could you post your language file for e.g. Catalan?

On a side note: You probably want to use the target language language names here, so Español etc., at least if you want to use those language names in a language navigation.

I don’t see any difference between the two language files in your last post? Or did you post twice accidentally?

I only just realized that there was a lot more code…

You have to put each language definition into it’s own file, not all into one…

So ca.php, gl.php, eu.php

I posted twice by accident, sorry. I’m realizing that I may create an additional file ‘cat.php’ with these code lines, instead of having all of them in the ‘es.php’

Ok, great! Thank you!

Little tip for the future:

Wrap each code block within three backticks on a separate line before and after the code block.
Bildschirmfoto 2021-01-12 um 20.23.45

Ok, I’ll do so!

1 Like

Hi again, I’ve tried to separate each language in a individual file, but the error persists. This is the code lines for Catalan, file cat.php:

<?php

return array (
    'code'      => 'ca',
    'default'   => true,
    'direction' => 'ltr',
    'locale'    => 'ca_ES',
    'name'      => 'Catalan',
    'url'       => '/es',
    'translations' => [
        'email-placeholder' => 'tunombre@email.com',
        'footer-help'       => 'Ayuda & Soporte',
    ]
    );

Should be ca.php not cat.php, I think. The name of the file has to be the same as the language code.

And the URL must be /ca

You cannot use the same URL, all URLs have to be unique

Although I’m no longer sure what exactly you want to achieve? Don’t you want to set these languages up as translations?

No, I want to redirect the Spanish users with detectedLanguage function into the Spanish version with URL /es . That means that browsers with Spanish, Catalan, Basque and Galician should open the Spanish URL. The rest of languages detected will open the English version

Ah, ok, then setting up the languages doesn’t make sense at all. These are for actual translations

You would have to redirect either on browser language/region or set up an intro page where users can actively select their language, ikea.com etc. style.

Ok, how could I add Catalan, Galician and Basque into the condition of opening the Spanish URL /es?

As I said, either using information from the browser or even based on IP addresses for something automatic, or let the user choose.

Ok thanks, could you please show me in which file can I set up this condition using the browser information?