Skip to content

Browser Preferred Language Detector 浏览器语言偏好侦测器

Notifications You must be signed in to change notification settings

douyasi/locator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Locator

Latest Stable Version Latest Unstable Version License Total Downloads

Description

Browser Preferred Language Detector, detect by browser Accept-Language request header.

Installation

Get Composer, then run in terminal:

cd /path/to/your-project
composer require "douyasi/locator:~1.0"

Usage

Example in Laravel

Route::get('test', function () {
    $detector = app('Douyasi\Locator\PreferredLanguageDetector');
    return $detector->detect(['zh-CN', 'en']);
    //return $detector->get();
    //return $detector->get_languages(['zh-CN,zh', 'en', 'zh-TW'], ['0.8', '0.6', '0.4']);
});

You can use $detector->detect(['zh-CN', 'en']) to replace Request::getPreferredLanguage(['zh-CN', 'en']) in Laravel.

API

Detect your browser preferred language:

$langs = ['zh-CN', 'en'];  //available languages (i18n array) in your web project
$detector->detect($langs);  //return 'zh-CN' or 'en', according to your browser language preference.

Get browser preferrend languages:

$detector->get();

You can get some data (json format) like blow:

[
    {
        "priority": 80,
        "tags": "zh-CN,zh",
        "locales": [
            {
                "language": "zh",
                "region": "CN",
                "script": "",
                "variant1": "",
                "variant2": "",
                "variant3": "",
                "private1": "",
                "private2": "",
                "private3": ""
            },
            {
                "language": "zh",
                "script": "",
                "region": "",
                "variant1": "",
                "variant2": "",
                "variant3": "",
                "private1": "",
                "private2": "",
                "private3": ""
            }
        ]
    },
    {
        "priority": 60,
        "tags": "en",
        "locales": [
            {
                "language": "en",
                "script": "",
                "region": "",
                "variant1": "",
                "variant2": "",
                "variant3": "",
                "private1": "",
                "private2": "",
                "private3": ""
            }
        ]
    },
]

Reference

Copyright

http://douyasi.com

Copyright (c) 2016 douyasi org by ycrao