https://github.com/reachweb/locale-lander
Statamic addon that redirects the user to the site in his browser's locale.
https://github.com/reachweb/locale-lander
Last synced: about 1 year ago
JSON representation
Statamic addon that redirects the user to the site in his browser's locale.
- Host: GitHub
- URL: https://github.com/reachweb/locale-lander
- Owner: reachweb
- License: mit
- Created: 2024-01-23T10:41:41.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-01T11:02:25.000Z (over 1 year ago)
- Last Synced: 2025-04-18T20:07:29.208Z (about 1 year ago)
- Language: PHP
- Homepage: https://www.reach.gr
- Size: 147 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

# Locale Lander
Locale Lander is a simple addon for Statamic, particularly useful for multi-site setups involving multiple languages. It redirects users to the **default language of their browser** during their first visit or displays a banner informing the user that the content is available in their native language.
## How to Install
Install using composer:
```bash
composer require reachweb/locale-lander
```
## How to Use (Redirection)
To use the redirection feature, no additional configuration is needed after installation. The addon should function right out of the box.
## How to Use (Banner)
To use the banner feature, Locale Lander provides a tag called, you guessed it, `locale-lander`.
First, you must disable automatic redirection. Publish the config file:
```bash
php artisan vendor:publish --tag=locale-lander-config
```
and in `config/locale-lander.php` disable redirection:
```php
'enable_redirection' => false,
```
Then, you can use the tag by adding the necessary code to a file that loads on all pages of your website, usually `layout.antlers.html`.
```
{{ locale_banner }}
{{ if entry }}
{{ partial src="vendor/locale-lander/banner" }}
{{ /if }}
{{ /locale_banner }}
```
The tag will return an `array` that always contains an 'entry' key. If the Entry is unavailable in this locale, `entry` will be false. Otherwise, it will contain the `title` and the `url` of the entry. A `site` key is also provided with information about the user's locale site.
As you might have guessed, the addon contains an example banner template, styled with *Tailwind CSS*, that works out of the box.
Publish the template:
```bash
php artisan vendor:publish --tag=locale-lander-views
```
and you should be good to go. If you are not using Tailwind CSS, you will need to style it yourself.
In case you want to implement your own version, note that you should set a cookie named `locale_banner_closed` to prevent showing the banner to users who have closed it.
## How it Works
This addon automatically applies a middleware in the `statamic.web` middleware group. Here's the process:
- It checks the user's **browser language** and the **sites** defined in `statamic/sites.php`.
- If the current site's locale **differs** from the user's browser, it verifies if a site for that locale exists.
- If such a site exists and the current entry is **available** in that locale, it redirects the user there.
- A **session variable** is set to avoid repetitive redirection. This prevents redirect loops and accommodates users who prefer a different locale than their browser’s default.
## Common Issues
- The addon functions with URLs resolving to an `Entry`. It won't work if the first page a user visits isn't an Entry. (Future improvements are planned for this issue.)
- If the site's default locale matches the user's, or if the user initially visits the site in their locale but chooses to visit a different one, they will be redirected back to their original locale **once**. I am currently working to resolve this issue, but it may require modifications to the language switcher.