Skip to content
Language switcher control for Nette Framework
PHP HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpstan.neon

README.md

Language switcher


Build Status Licence Latest stable PHPStan

Installation

The recommended way to is via Composer:

composer require surda/language-switcher

After that you have to register extension in config.neon:

extensions:
    languageSwitcher: Surda\LanguageSwitcher\DI\LanguageSwitcherExtension

Minimal configuration

languageSwitcher:
    locales:
        cs: Čeština
        en: English
    default: cs

List of all configuration options:

languageSwitcher:
    locales:
        cs: Čeština
        en: English
    default: cs
    locale: cs
    useAjax: false
    locale2Country:
        en: us
        cs: cz
    template: path/to/your/latte/file.latte
    # or
    templates:
        default: path/to/your/latte/file.latte
        navbar: path/to/your/latte/navbar.latte

Usage

Inject language switcher + $locale persistent property

use Nette\Application\UI\Presenter;
use Surda\LanguageSwitcher\TLanguageSwitcher;

class BasePresenter extends Presenter
{
    use TLanguageSwitcher;
}

Inject language switcher without $locale persistent property

use Nette\Application\UI\Presenter;
use Surda\LanguageSwitcher\TLanguageSwitcherWithoutLocaleProperty;

class BasePresenter extends Presenter
{
    use TLanguageSwitcherWithoutLocaleProperty;
    
    /** @persistent */
    public $locale = 'cs';
}

Custom

use Nette\Application\UI\Presenter;
use Surda\LanguageSwitcher\LanguageSwitcherFactory;
use Surda\LanguageSwitcher\LanguageSwitcherControl;

class BasePresenter extends Presenter
{
    /** @var LanguageSwitcherFactory */
    private $languageSwitcherFactory;

    /** @persistent */
    public $locale = 'cs';

    public function injectLanguageSwitcherFactory(LanguageSwitcherFactory $LanguageSwitcherFactory)
    {
        $this->languageSwitcherFactory = $LanguageSwitcherFactory;
    }
    
    /**
    * @return LanguageSwitcherControl
    */
    protected function createComponentLanguageSwitcher(): LanguageSwitcherControl
    {
        $control = $this->languageSwitcherFactory->create();
        $control->setLocale($this->locale);

        $control->onChange[] = function (LanguageSwitcherControl $control, string $locale): void {
            $this->redirect('this', ['locale' => $locale]);
        };

        return $control;
    }
}

Template

{control languageSwitcher} or {control languageSwitcher default}  

Set control template by type of template (see config.neon)

{control languageSwitcher navbar}  

Custom component options

class ProductPresenter extends Presenter
{
    /**
    * @return LanguageSwitcherControl
    */
    protected function createComponentLanguageSwitcher(): LanguageSwitcherControl
    {
        // Init items per page component
        $control = $this->languageSwitcherFactory->create();
        
        // All allowed locales 
        $control->setLocales(['cs' => 'Czech', 'en' => 'English']);

        // Default locale
        $control->setDefaultLocale('cs');

        // Current locale
        $control->setLocale('cs');

        // To use your own template (default type)
        $control->setTemplate('path/to/your/latte/file.latte');

        // To use your own template
        $control->setTemplates([
            'default' => 'path/to/your/latte/file.latte',
            'navbar' => 'path/to/your/latte/navbar.latte',
        ]);

        // Enable ajax (defult is disable)
        $control->enableAjax();
        
        // Disable ajax
        $control->disableAjax();
        
        return $control;
    }
}

Template file bootstrap4.nav-item.flag.latte using flags from https://github.com/lipis/flag-icon-css

You can’t perform that action at this time.