Skip to content

Commit

Permalink
Add locale detection guide
Browse files Browse the repository at this point in the history
  • Loading branch information
richardDobron committed Mar 3, 2024
1 parent c2fdcba commit f1b6dbf
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions docs/locale_detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
id: locale_detection
title: Locale detection
sidebar_label: Locale detection
---

# How to detect the user's locale from the browser?

## Installation
To implement locale detection in your PHP project, you can use the `HTTP2` library. You can install it via Composer:

```shell
composer require pear/http2
```

# Usage
After installing the `HTTP2 library`, you can utilize it in your PHP code to detect the user's locale. Here's a basic example:

```php
<?php

namespace App\Http\Middleware;

use Closure;
use fbt\FbtConfig;
use fbt\Runtime\Shared\FbtHooks;
use Illuminate\Http\Request;

class RegisterFbt
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($request->user()) {
// Notice: locale of logged-in user is always preferred
FbtConfig::set('viewerContext', $request->user());
} else {
$supportedLanguages = [
'en' => 'en_US',
'en-UK' => 'en_UK',
'de' => 'de_DE',
'de-AT' => 'de_AT',
'cs' => 'cs_CZ',
'sk' => 'sk_SK',
];

$http = new \HTTP2();

// Detect the user's language
$clientLanguage = $http->negotiateLanguage($supportedLanguages, $fallbackLanguage = 'en');

// Get the locale for the detected language
$locale = $supportedLanguages[$clientLanguage];

// Set the detected locale
FbtConfig::set('locale', $locale);
}

return $next($request);
}
}
```

then register the middleware in `app/Http/Kernel.php`:

```php
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\RegisterFbt::class,
],
// ...
];
```

# Explanation

## HTTP Accept-Language Header
The `negotiateLanguage()` method works by parsing the HTTP `Accept-Language` header sent by the user's browser. This header contains information about the user's preferred languages in order of priority. The method then matches these languages against the provided list and returns the best match.

## Fallback Locale
In case the user's preferred language is not available or cannot be determined, it's essential to have a fallback locale. This ensures that your application always defaults to a suitable language/locale even when the detection process fails.

0 comments on commit f1b6dbf

Please sign in to comment.