Language detection


I have a multilanguage website (English, French, Spanish, Deutsch, Japeneese, Portugueese, Italian)

And i want the best solution to serve user with his native language or country language

By the way lang.detect is not working for me, this is my code

c::set('lang.detect', true);

c::set('languages', array(
    'code'    => 'fr',
    'name'    => 'Français',
    'locale'  => 'fr_FR',
    'url'     => '/fr',
    'lang' => 'fr',
    'code'    => 'en',
    'name'    => 'English',
    'locale'  => 'en_US',
    'default' => true,
    'url'     => '/en',
    'lang' => 'en',
    'code'    => 'es',
    'name'    => 'Español',
    'locale'  => 'es_ES',
    'url'     => '/es',
    'lang' => 'es',
    'code'    => 'it',
    'name'    => 'Italiano',
    'locale'  => 'it_IT',
    'url'     => '/it',
    'lang' => 'it',
    'code'    => 'jp',
    'name'    => '日本人',
    'locale'  => 'ja_JP',
    'url'     => '/jp',
    'lang' => 'ja',
    'code'    => 'de',
    'name'    => 'Deutsch',
    'locale'  => 'de_DE',
    'url'     => '/de',
    'lang' => 'de',
    'code'    => 'pt',
    'name'    => 'Portugês',
    'locale'  => 'pt_PT',
    'url'     => '/pt',
    'lang' => 'pt',

Thank you

The language detection is only run on the homepage of the default language (i.e. to allow users to manually switch their language on subpages.

Given that, there are two issues with your code:

  • The configuration option is called language.detect, not lang.detect
  • There is no lang key in the languages array, you can remove this as you have already included code

PS: Please use Markdown code blocks when posting code in the future. They use three backticks at the start and end on separate lines. I have corrected it above.

1 Like

Thank you for your answer

I’ve changed the option to language.detect, my browser languages by priority are : fr-FR, fr, en-US,

When i run homepage (/), its redirects to english version but should go to french in my case

Have you tried clearing your cookies or using a different browser for testing? Might be that the language was switched, which is remembered by Kirby for the current session.