Skip to content

Commit bae02dc

Browse files
committed
Update README
1 parent 8fa33ad commit bae02dc

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

README.md

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# BrowserLocale
22

3-
[![GitHub release](https://img.shields.io/github/release/codezero-be/browser-locale.svg)]() [![License](https://img.shields.io/packagist/l/codezero/browser-locale.svg)]() [![Build Status](https://img.shields.io/travis/codezero-be/browser-locale.svg?branch=master)](https://travis-ci.org/codezero-be/browser-locale) [![Scrutinizer](https://img.shields.io/scrutinizer/g/codezero-be/browser-locale.svg)](https://scrutinizer-ci.com/g/codezero-be/browser-locale) [![Total Downloads](https://img.shields.io/packagist/dt/codezero/browser-locale.svg)](https://packagist.org/packages/codezero/browser-locale)
3+
[![GitHub release](https://img.shields.io/github/release/codezero-be/browser-locale.svg)]()
4+
[![License](https://img.shields.io/packagist/l/codezero/browser-locale.svg)]()
5+
[![Build Status](https://scrutinizer-ci.com/g/codezero-be/browser-locale/badges/build.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/browser-locale/build-status/master)
6+
[![Code Coverage](https://scrutinizer-ci.com/g/codezero-be/browser-locale/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/browser-locale/?branch=master)
7+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/codezero-be/browser-locale/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/browser-locale/?branch=master)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/codezero/browser-locale.svg)](https://packagist.org/packages/codezero/browser-locale)
49

510
#### Get the most preferred locales from your visitor's browser.
611

@@ -10,85 +15,100 @@ This can be read by PHP, usually with the `$_SERVER["HTTP_ACCEPT_LANGUAGE"]` var
1015

1116
`BrowserLocale` parses this string and lets you access the preferred locales quickly and easily.
1217

18+
## Requirements
19+
20+
- PHP >= 7.0
21+
22+
## Install
23+
24+
```
25+
composer require codezero/browser-locale
26+
```
27+
1328
## Instantiate
1429

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"]);
1834
```
1935

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.
2139

22-
## Get primary locale
40+
```php
41+
$browser = \App::make(\CodeZero\BrowserLocale\BrowserLocale::class);
42+
```
2343

24-
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
2545

26-
```
27-
$instance = $browser->getLocale();
46+
``` php
47+
$locale = $browser->getLocale();
2848
```
2949

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.
3151

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
3858
}
3959
```
4060

41-
## Get all locales
61+
## Get All Locales
4262

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
4664
$locales = $browser->getLocales();
65+
```
4766

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
5377
}
5478
```
5579

56-
## Get flattened array
80+
## Get Flattened Array
5781

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`)
5983

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']
6387

6488
$languages = $browser->getLocales('language');
65-
//=> Example: ['en', 'nl']
89+
//=> Result: ['en', 'nl']
6690

6791
$countries = $browser->getLocales('country');
68-
//=> Example: ['US', 'BE']
92+
//=> Result: ['US', 'BE']
6993

7094
$weights = $browser->getLocales('weight');
71-
//=> Example: [1.0, 0.8, 0.6, 0.4]
95+
//=> Result: [1.0, 0.8, 0.6, 0.4]
7296
```
7397

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-
7898
## Testing
7999

80100
```
81-
$ vendor/bin/phpspec run
101+
composer test
82102
```
83103

84104
## Security
85105

86106
If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.
87107

88-
## License
108+
## Changelog
89109

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).
91111

92-
---
112+
## License
93113

94-
[![Analytics](https://ga-beacon.appspot.com/UA-58876018-1/codezero-be/browser-locale)](https://github.com/igrigorik/ga-beacon)
114+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

0 commit comments

Comments
 (0)