PHP code example of sjaakp / yii2-polyglot

1. Go to this page and download the library: Download sjaakp/yii2-polyglot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sjaakp / yii2-polyglot example snippets


	
	'components' => [
        // ... other components, like 'cache' and 'errorHandler'
        'polyglot' => [
            'class' => 'sjaakp\polyglot\Polyglot',
            'languages' => [
                'en-US' => 'English',
                'de-DE' => 'Deutsch',
                'fr-FR' => 'Français',
                // ... more languages
            ]
        ],
        // ... even more components
        
The `language` property is an array of the languages (or more correctly the *locale*s)
the web site supports. The keys of this array are the names of the locales in 
the ICU-format, just like [Yii recommends](https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n#locale "Yii").
**One of the keys should be the same as the
 [`language`-property](https://www.yiiframework.com/doc/api/2.0/yii-base-application#$language-detail "Yii") of the application.**

The values of the array should be one of the following:

- `string` human-readable name of the language. It appears as popup tooltip.

- `array` with two keys:
    - `"label"` the human-readable language name;
    - `"flag"` the name of the flag that will be displayed, without the `.png`-part.

So, to show the Dutch language with the Belgian flag, we would use:

    <php 
	
	'components' => [
        // ... other components, like 'cache' and 'errorHandler'
        'polyglot' => [
            'class' => 'sjaakp\polyglot\Polyglot',
            'languages' => [
                'en-US' => 'English',
                // ... more languages
                'nl-NL' => [
                    'label' => 'Nederlands',
                    'flag' => 'be'
                ]
            ]
        ],
        // ... even more components

If the array value is just a `string`, **Polyglot** tries to devise the flag name itself.

A bunch of flags (247 of them) can be found in **yii2-polyglot**'s `assets/flags` directory.
They are made by [famfamfam](http://www.famfamfam.com/lab/icons/flags/), by the way.

## Bootstrap ##

**Polyglot** has to be bootstrapped. Do this by adding the following to the
application configuration array:

    <php
    
    'bootstrap' => [
        'polyglot',
    ]

There probably already is a `bootstrap` property in your configuration file; just
add `'polyglot'` to it.

#### Polyglot options ####

Apart from the **languages**-settings, **Polyglot** has three more options:

 - **useCookie** `bool` If set to `false`, **Polyglot** will *not* store the preferred
 language in a cookie, but in the PHP session. As a consequence, the language choice
 will only persist for one session. Default: `true`.
 - **cookieName** `string` Also used as the session key if **useCookie** is `false`.
 Default: `"polyglot"`.
 - **cookieStamina** `integer` Cookie expiration time in seconds. Default: `31536000` (one year). 

## Widgets ## 

There are two widgets in the **yii2-polyglot** package. **PolyglotButtons** displays the
language options as a row of flag buttons. **PolyglotDropdown** is a dropdown menu.
Rendering a **yii2-polyglot** widget anywhere in a `View` is just a matter of:

	
	use sjaakp\polyglot\PolyglotButtons;
	

	use sjaakp\polyglot\PolyglotDropdown;