You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#### Get the most preferred locales from your visitor's browser.
6
11
@@ -10,85 +15,100 @@ This can be read by PHP, usually with the `$_SERVER["HTTP_ACCEPT_LANGUAGE"]` var
10
15
11
16
`BrowserLocale` parses this string and lets you access the preferred locales quickly and easily.
12
17
18
+
## Requirements
19
+
20
+
- PHP >= 7.0
21
+
22
+
## Install
23
+
24
+
```
25
+
composer require codezero/browser-locale
26
+
```
27
+
13
28
## Instantiate
14
29
15
-
```
16
-
use CodeZero\BrowserLocale\BrowserLocale;
17
-
$browser = new BrowserLocale();
30
+
#### For vanilla PHP:
31
+
32
+
```php
33
+
$browser = new \CodeZero\BrowserLocale\BrowserLocale($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
18
34
```
19
35
20
-
By default the class will look for `$_SERVER["HTTP_ACCEPT_LANGUAGE"]`. If you want to override this, you can pass a string to the constructor. That string should be formatted in the same way as described above.
36
+
#### For Laravel:
37
+
38
+
Laravel >= 5.5 will automatically register the ServiceProvider so you can get `BrowserLocale` from the IOC container.
To fetch the primary browser locale, you can call `getLocale()`. This will return an instance of `CodeZero\BrowserLocale\Locale` or `null` if no locale exists.
44
+
## Get Primary Locale
25
45
26
-
```
27
-
$instance = $browser->getLocale();
46
+
```php
47
+
$locale = $browser->getLocale();
28
48
```
29
49
30
-
If a locale is returned, you get access to a few properties:
50
+
This will return an instance of `\CodeZero\BrowserLocale\Locale` or `null` if no locale exists.
31
51
32
-
```
33
-
if ($instance !== null) {
34
-
$locale = $instance->locale; // Example: "en-US"
35
-
$language = $instance->language; // Example: "en"
36
-
$country = $instance->country; // Example: "US"
37
-
$weight = $instance->weight; // Example: 1.0
52
+
```php
53
+
if ($locale !== null) {
54
+
$full= $locale->full; // Example: "en-US"
55
+
$language = $locale->language; // Example: "en"
56
+
$country = $locale->country; // Example: "US"
57
+
$weight = $locale->weight; // Example: 1.0
38
58
}
39
59
```
40
60
41
-
## Get all locales
61
+
## Get All Locales
42
62
43
-
To fetch all locales that are configured in a visitor's browser, you can call `getLocales()`. This will return an array of `Locale` instances, sorted by weight. So the first array item is the most preferred locale. If no locales exist, an empty array will be returned.
44
-
45
-
```
63
+
```php
46
64
$locales = $browser->getLocales();
65
+
```
47
66
48
-
foreach ($locales as $instance) {
49
-
$locale = $instance->locale; // Example: "en-US"
50
-
$language = $instance->language; // Example: "en"
51
-
$country = $instance->country; // Example: "US"
52
-
$weight = $instance->weight; // Example: 1.0
67
+
This will return an array of `\CodeZero\BrowserLocale\Locale` instances, sorted by weight in descending order. So the first array item is the most preferred locale.
68
+
69
+
If no locales exist, an empty array will be returned.
70
+
71
+
```php
72
+
foreach ($locales as $locale) {
73
+
$full = $locale->full; // Example: "en-US"
74
+
$language = $locale->language; // Example: "en"
75
+
$country = $locale->country; // Example: "US"
76
+
$weight = $locale->weight; // Example: 1.0
53
77
}
54
78
```
55
79
56
-
## Get flattened array
80
+
## Get Flattened Array
57
81
58
-
Maybe you want to fetch a simple array with only the 2-letter language codes. Or maybe only the country codes. The `getLocales()` method accepts a property filter to allow for this. The filter can be the name of one of the following properties:
82
+
You can get a flattened array, containing only specific locale info. These arrays will always be sorted by weight in descending order. There will be no duplicate values! (e.g. `en` and `en-US` are both the language `en`)
59
83
60
-
```
61
-
$locales = $browser->getLocales('locales');
62
-
//=> Example: ['en-US', 'en', 'nl-BE', 'nl']
84
+
```php
85
+
$locales = $browser->getLocales('full');
86
+
//=> Result: ['en-US', 'en', 'nl-BE', 'nl']
63
87
64
88
$languages = $browser->getLocales('language');
65
-
//=> Example: ['en', 'nl']
89
+
//=> Result: ['en', 'nl']
66
90
67
91
$countries = $browser->getLocales('country');
68
-
//=> Example: ['US', 'BE']
92
+
//=> Result: ['US', 'BE']
69
93
70
94
$weights = $browser->getLocales('weight');
71
-
//=> Example: [1.0, 0.8, 0.6, 0.4]
95
+
//=> Result: [1.0, 0.8, 0.6, 0.4]
72
96
```
73
97
74
-
These arrays will always be sorted by preference (most preferred first).
75
-
76
-
There will be no duplicate values! (e.g. `en` and `en-US` are both the language `en`)
77
-
78
98
## Testing
79
99
80
100
```
81
-
$ vendor/bin/phpspec run
101
+
composer test
82
102
```
83
103
84
104
## Security
85
105
86
106
If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.
87
107
88
-
## License
108
+
## Changelog
89
109
90
-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
110
+
See a list of important changes in the [changelog](CHANGELOG.md).
0 commit comments