gugglegum/yii2-cookie-language-selector 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

gugglegum/yii2-cookie-language-selector

Composer 安装命令:

composer require gugglegum/yii2-cookie-language-selector

包简介

Yii2 language selector component based on cookies

README 文档

README

This is very simple component which adds language switch functionality to your site using cookies mechanism. It's not a widget, it doesn't offers any GUI like lajax/yii2-language-picker. This may be useful because every real web-site has your own design and HTML markup. Some of web-sites uses <select>, some uses <ul> dropdown list with onclick event handler which submits hidden POST form. With this component you're free to use any language selection method. You just need a something able to send POST when needed to switch language and add very simple handler of this POST request.

Installation

Installation is very easy and contains following 5 steps.

1. Install component itself

Execute following command in directory of your Yii2 application:

composer require gugglegum/yii2-cookie-language-selector

(you may need to use composer.phar instead of composer). This will install the gugglegum/yii2-cookie-language-selector component into your application.

2. Add component to your config

Go to your frontend's config and add component config into components section:

        'cookieLanguageSelector' => [
            'class' => 'gugglegum\Yii2\Extension\CookieLanguageSelector\Component',
            'defaultLanguage' => 'en-US',
            'validLanguages' => ['en-US', 'ru-RU', 'de-DE'],
        ],

The defaultLanguage is language used by default before language selection. The validLanguages is the list of languages which can be selected. Additionally you may define name of cookie to store language (cookieName) and it's expiration period (cookieExpire). By default cookie name is "lang" and it's expires after 7776000 seconds (90 days).

3. Add component in bootstrap

Your application stores current language in Yii::$app->language. So you need to add this component to bootstrap section of your frontend's config to init this component at every application start. You should get something like this:

    'bootstrap' => ['log', 'cookieLanguageSelector'],

The initialization of cookieLanguageSelector will just set Yii::$app->language to language retrieved from component. Please use long language format like "en-US", not just "en".

4. Create POST form on your web-site

You should create HTML form with POST method or ajax with POST to send new language to the server. Your form may contain only language parameter. Additionally you may add redirectTo parameter with URL your user should be redirected after switching language (usually the same URL where user was before). The names of these parameters (language and redirectTo) is not predefined, it's completely up to you and they are used only in you handler action described in step 5.

Please avoid use GET method of HTTP for switching language. As described in RFC-2616 the GET method should not be used in case different from retrieving information. Your browser may prefetch links on your page and switch language randomly. On the other hand request with GET method can be cached on proxy, so switch may not work. Please always use POST method for action which doing something.

Here is very simple example of language switching form. Add this to some view or layout:

        <?= Html::beginForm(['site/switch-language'], 'post') ?>
        <?= Html::hiddenInput('redirectTo', \yii\helpers\Url::to(Yii::$app->request->url)) ?>
        <?= Html::beginTag('select', ['name' => 'language', 'onchange' => 'this.form.submit();']) ?>
        <?= Html::renderSelectOptions(\Yii::$app->language, [
            'en-US' => 'English',
            'de-DE' => 'Deutsch',
            'ru-RU' => 'Russian',
        ]) ?>
        <?= Html::endTag('select') ?>
        <?= Html::endForm() ?>
        <p>Current language is <?= Html::encode(\Yii::$app->language) ?> </p>

Of course you may use more customized variant. For example, you may use CSS stylized dropdown list which submits hidden POST form.

5. Create handler action

Add to your SiteController.php following action-method:

    /**
     * Switches the language and redirects back
     */
    public function actionSwitchLanguage()
    {
        Yii::$app->cookieLanguageSelector->setLanguage(Yii::$app->request->post('language'));
        $this->redirect(Yii::$app->request->post('redirectTo', ['site/index']));
    }

That's all! Your comments and suggestions are welcome.

gugglegum/yii2-cookie-language-selector 适用场景与选型建议

gugglegum/yii2-cookie-language-selector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 664 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 gugglegum/yii2-cookie-language-selector 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 664
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-03