承接 codezero/browser-locale 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codezero/browser-locale

Composer 安装命令:

composer require codezero/browser-locale

包简介

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

README 文档

README

GitHub release License Build Status Code Coverage Code Quality Total Downloads

ko-fi

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

Every browser has a setting for preferred website locales.

This can be read by PHP, usually with the $_SERVER["HTTP_ACCEPT_LANGUAGE"] variable.

$_SERVER["HTTP_ACCEPT_LANGUAGE"] will return a comma separated list of language codes. Each language code MAY have a "relative quality factor" attached ("nl;q=0.8") which determines the order of preference. For example: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4. If no relative quality factor is present, the value is by default 1.0.

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

Requirements

  • PHP >= 7.0

Install

composer require codezero/browser-locale

Instantiate

For vanilla PHP:

$browser = new \CodeZero\BrowserLocale\BrowserLocale($_SERVER["HTTP_ACCEPT_LANGUAGE"]);

For Laravel:

Laravel >= 5.5 will automatically register the ServiceProvider so you can get BrowserLocale from the IOC container.

$browser = \App::make(\CodeZero\BrowserLocale\BrowserLocale::class);

Get Primary Locale

$locale = $browser->getLocale();

This will return an instance of \CodeZero\BrowserLocale\Locale or null if no locale exists.

if ($locale !== null) {
    $full     = $locale->locale;   // Example: "en-US"
    $language = $locale->language; // Example: "en"
    $country  = $locale->country;  // Example: "US"
    $weight   = $locale->weight;   // Example: 1.0
}

Get All Locales

$locales = $browser->getLocales();

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.

If no locales exist, an empty array will be returned.

foreach ($locales as $locale) {
    $full     = $locale->locale;   // Example: "en-US"
    $language = $locale->language; // Example: "en"
    $country  = $locale->country;  // Example: "US"
    $weight   = $locale->weight;   // Example: 1.0  
}

Filter Locale Info

You can get a flattened array with only specific Locale information. 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)

LocaleFilter

Returns an array of every locale found in the input string.

$browser = new \CodeZero\BrowserLocale\BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6');
$filter = new \CodeZero\BrowserLocale\Filters\LocaleFilter;
$locales = $browser->filter($filter);
//=> Result: ['en-US', 'en', 'nl-BE']

CombinedFilter

Returns an array of every locale found in the input string, while making sure the 2-letter language version of the locale is always present.

$browser = new \CodeZero\BrowserLocale\BrowserLocale('en-US,nl;q=0.8');
$filter = new \CodeZero\BrowserLocale\Filters\CombinedFilter;
$locales = $browser->filter($filter);
//=> Result: ['en-US', 'en', 'nl']

LanguageFilter

Returns an array of only the 2-letter language codes found in the input string. Language codes are also extracted from full locales and added to the results array.

$browser = new \CodeZero\BrowserLocale\BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6');
$filter = new \CodeZero\BrowserLocale\Filters\LanguageFilter;
$languages = $browser->filter($filter);
//=> Result: ['en', 'nl']

CountryFilter

Returns an array of only the 2-letter country codes found in the input string. Locales that only contain a 2-letter language code will be skipped.

$browser = new \CodeZero\BrowserLocale\BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6,nl;q=0.4');
$filter = new \CodeZero\BrowserLocale\Filters\CountryFilter;
$countries = $browser->filter($filter);
//=> Result: ['US', 'NL']

WeightFilter

Returns an array of all relative quality factors found in the input string. The default of 1.0 is also included.

$browser = new \CodeZero\BrowserLocale\BrowserLocale('en-US,en;q=0.8,nl-NL;q=0.6,nl;q=0.4');
$filter = new \CodeZero\BrowserLocale\Filters\WeightFilter;
$weights = $browser->filter($filter);
//=> Result: [1.0, 0.8, 0.6, 0.4]

You can create your own filters by implementing the \CodeZero\BrowserLocale\Filters\Filter interface.

Testing

composer test

Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

Changelog

A complete list of all notable changes to this package can be found on the releases page.

License

The MIT License (MIT). Please see License File for more information.

codezero/browser-locale 适用场景与选型建议

codezero/browser-locale 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.07M 次下载、GitHub Stars 达 16, 最近一次更新时间为 2015 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「browser」 「locale」 「language」 「detect」 「country」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codezero/browser-locale 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codezero/browser-locale 我们能提供哪些服务?
定制开发 / 二次开发

基于 codezero/browser-locale 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.07M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 33
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-13