Download the PHP package gugglegum/yii2-cookie-language-selector without Composer

On this page you can find all versions of the php package gugglegum/yii2-cookie-language-selector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii2-cookie-language-selector

Yii2 Cookie Language Selector

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:

(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:

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:

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:

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:

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


All versions of yii2-cookie-language-selector with dependencies

PHP Build Version
Package Version
Requires yiisoft/yii2 Version *
php Version >=5.3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package gugglegum/yii2-cookie-language-selector contains the following files

Loading the files please wait ....