定制 raindrop/locale-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

raindrop/locale-bundle

Composer 安装命令:

composer require raindrop/locale-bundle

包简介

Raindrop Locale Symfony2 Bundle

README 文档

README

Build Status

This bundle is a fork of LuneticsLocaleBundle which allows you to guess the Locale from different parameters in your Symfony2 applications.

Installation

Add the package to your dependencies

"require": {
    "raindrop/locale-bundle": "dev-master",
    ....
},

Register the bundle in your kernel

public function registerBundles()
    {
        $bundles = array(
            // ...
            new Raindrop\LocaleBundle\RaindropLocaleBundle(),
        );

Update your packages

php composer.phar update raindrop/locale-bundle

Configuration

Allowed locales for your application

You need to define at least one valid locale that is valid for your application

raindrop_locale:
  allowed_locales:
    - en
    - fr
    - de

Strict Mode

raindrop_locale:
  strict_mode: true # defaults to false

You can enable strict_mode, where only the exact allowed locales will be matched. For example:

  • If your user has a browser locale with de_DE and de, and you only explicitly allow de, the locale de will be chosen.
  • If your user has a browser locale with de and you only explicitly allow de_DE, no locale will be detected.

We encourage you to use the non-strict mode, that'll also choose the best region locale for your user.

Guessers

You need to activate and define the order of the locale guessers. This is done in one step in the configuration :

raindrop_locale:
  guessing_order:
    - session
    - cookie
    - browser
    - query
    - router

With the example above, the guessers will be called in the order you defined as 1. session 2. cookie 2. router 3. browser.

Note that the session and cookie guessers only retrieve previously identified and saved locales by the router or browser guesser.

If you use the _locale parameter as attribute/parameter in your routes, you should use the query and router guesser first.

raindrop_locale:
  guessing_order:
    - query
    - router
    - session
    - cookie
    - browser

Locale Cookies / Session

The session and cookie guesser is usually used when you do not use locales in the uri's and you guess it from the user browser preferences. When doing this,

it is good to set session and/or cookie as the first guesser to not try to detect the locale at each request.

Cookie

If you use the cookie guesser, it will be automatically read from the cookie and write changes into the cookie anytime the locale has changed (Even from another guesser)

raindrop_locale:
  cookie:
    set_on_change: true

This is most useful for unregistered and returning visitors.

Session

The session guesser will automatically save a previously identified locale into the session and retrieve it from the session. This guesser should always be first in your guessing_order configuration if you don't use the router guesser.

FilterLocaleSwitchEvent / LocaleUpdateListener

The LocaleGuesserManager dispatches a LocaleBundleEvents::onLocalChange if you use either the 'session' or cookie guesser. The LocaleUpdateListeners checks if the locale has changed and updates the session or cookie.

For example, if you don't use route / query parameters for locales, you could build an own listener for your user login, which dispatches a LocaleBundleEvents::onLocalChange event to set the locale for your user. You just have to use the FilterLocaleSwitchEvent and set the locale.

$locale = $user->getLocale();
$localeSwitchEvent = new FilterLocaleSwitchEvent($locale);
$this->dispatcher->dispatch(LocaleBundleEvents::onLocaleChange, $localeSwitchEvent);

Custom Guessers

Read more about creating your own guesser here:

Read the full documentation for creating a custom Locale Guesser

Switch to another locale

You can render a default locale switcher, simply by calling the twig function in your template :

{{ locale_switcher() }}

Read the full documentation for the switcher

raindrop/locale-bundle 适用场景与选型建议

raindrop/locale-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 167 次下载、GitHub Stars 达 3, 最近一次更新时间为 2013 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 167
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-06