Craft plugin for detecting user language preferences and redirecting to the appropriate locale.
PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
localeredirector
LICENSE
README.md
releases.json

README.md

Locale Redirector plugin for Craft CMS

Locale Redirector detects the language preferences set by the user, via cookie or browser settings, and redirects to the appropriate localized version of the page requested, if the locale has been enabled for the site.

  1. Cookie: If the user's language preference has been saved in a cookie named locale, and the requested url is for a different locale currently enabled for the site, the user will be redirected to the locale saved in the cookie, for the requested url. For example:
Requested URL: http://site.com/blog/article-title
Cookie Present: es
Configured Site Locales: en, fr, de, pt, es
Redirects To: http://site.com/es/blog/article-title
  1. Browser Language Settings: If the language preference has not yet been saved to a cookie, then the plugin will attempt to find a match between the browser's preferred languages and the site's enabled locales. If a match is found, the user will be redirected to the matched language setting. For example:
Requested URL: http://site.com/blog/article-title
Cookie Present: n/a
Browser Languages: es, en
Configured Site Locales: en, fr, de, pt, es
Redirects To: http://site.com/es/blog/article-title

Installation

To install Locale Redirector, follow these steps:

  1. Download & unzip the file and place the localeredirector directory into your craft/plugins directory
  2. -OR- do a git clone https://github.com/vigetlabs/localeredirector.git directly into your craft/plugins folder. You can then update it with git pull
  3. Install plugin in the Craft Control Panel under Settings > Plugins
  4. The plugin folder should be named localeredirector for Craft to see it. GitHub recently started appending -master (the branch name) to the name of the folder for zip file downloads.

Locale Redirector works on Craft 2.5.x and Craft 2.6.x.

Important Notes

This plugin assumes that language links are available to the user. You can build this list of links any way you want, using custom template code with advanced logic, but the simplest approach is to use a craft plugin called Language Link. It provides an easy way to switch between languages by automatically outputting the correct links to the same page in different locales.

You can then set a locale cookie when one of these links is clicked, via JavaScript. You can use document.cookie for this, or use the js-cookie utility, which couldn't be simpler. To set a locale cookie to "es" and have it exipre in 1 year:

Cookies.set('locale', 'es', { expires: 365 });

Roadmap

Some things to do, and ideas for potential features:

  • Make locale cookie name and expiration configurable

Changelog

1.0.4 -- 2018.03.02

  • Redirects now occur correctly for single sections

1.0.3 -- 2018.02.01

  • Fixed date formatting in releases.json to ensure future updates show up in the Craft admin

1.0.2 -- 2017.11.30

  • Ensures that correct redirects happen when entry slugs are different

1.0.1 -- 2017.03.20

  • Fixes improper language matches

1.0.0 -- 2016.10.03

  • Initial release
Code At Viget

Visit code.viget.com to see more projects from Viget.