https://github.com/scherersoftware/cake-language-switcher
https://github.com/scherersoftware/cake-language-switcher
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/scherersoftware/cake-language-switcher
- Owner: scherersoftware
- License: mit
- Created: 2016-11-23T12:07:41.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-08-19T11:43:29.000Z (over 5 years ago)
- Last Synced: 2025-11-13T14:17:39.472Z (4 months ago)
- Language: PHP
- Size: 291 KB
- Stars: 2
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://travis-ci.org/scherersoftware/cake-language-switcher)
[](LICENSE.txt)
## Installation
You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).
The recommended way to install composer packages is:
```
composer require scherersoftware/cake-language-switcher
```
The next step is to load the plugin inside your bootstrap.php:
```
bin/cake plugin load LanguageSwitcher
```
Add the Middleware to your Application.php:
```
$middleware->push(new \LanguageSwitcher\Middleware\LanguageSwitcherMiddleware());
```
Optionally, you can pass an array of options to overwrite the default ones:
```
$middleware->push(new \LanguageSwitcher\Middleware\LanguageSwitcherMiddleware([
'model' => 'Users',
'field' => 'language',
'Cookie' => [
'name' => 'ChoosenLanguage',
'expires' => '+1 year',
'domain' => 'foo.bar',
'canonicalizeLocale' => false
],
'availableLanguages' => [
'en_US' => 'en_US'
]
]));
```
Add the Helper to your AppView:
```
$this->loadHelper('LanguageSwitcher.LanguageSwitcher');
```
Optionally, you can pass an array of options:
```
$this->loadHelper('LanguageSwitcher.LanguageSwitcher', [
'availableLanguages' => [
'en_US' => 'en_US',
'de_DE' => 'de_DE'
],
'displayNames' => [
'en_US' => 'English',
'de_DE' => 'Deutsch'
],
'imageMapping' => [
'en_US' => 'United-States',
'de_DE' => 'Germany'
],
'renderToggleButtonDisplayName' => true,
'element' => 'LanguageSwitcher.language_switcher'
]);
```
To use the element:
```
= $this->LanguageSwitcher->renderLanguageSwitcher(); ?>
```
Next, you should migrate your database.
```
bin/cake migrations migrate -p LanguageSwitcher
```
Add the css file located under webroot/css to your layout file!
## Configuration Usage
Inside your app.php add the following to change configs of the plugin:
```
'LanguageSwitcher' => [
'model' => 'Users',
'field' => 'language',
'Cookie' => [
'name' => 'ChoosenLanguage',
'expires' => '+1 year',
'canonicalizeLocale' => false
],
'availableLanguages' => [
'en_US'
],
'displayNames' => [
'en_US' => 'English'
],
'imageMapping' => [
'en_US' => 'United-States'
],
'renderToggleButtonDisplayName' => true,
'beforeSaveCallback' => function ($user, $request, $response) {
$language = 'en_EN';
$user->language = $language;
}
]
```
- model: The model used in the migration
- field: The field in the model
- Cookie: Optionally you can change the cookie name, the expiration date of the cookie and define if the locale should be saved canonicalized.
- availableLanguages: Add language keys
- displayNames: Should contain the same keys as availableLanguages. Map language key with its Display Name
- imageMapping: Should contain the same keys as availableLanguages. Map language key with its flag image name. (For all possible flag names open webroot/img/flags)
- beforeSaveCallback: Optionally you can override the user entity to set e.g. the language field with a special value
- renderToggleButtonDisplayName: Optionally you can hide the language name in the dropdown toggle button
- element: Optionally you can override the element used for rendering the language switcher with your own.