surda / language-switcher
Language switcher control for Nette Framework
v1.3.1
2019-10-05 10:16 UTC
Requires
- php: >=7.1
- nette/di: ^3.0
- surda/themeable-controls: ^1.0
Requires (Dev)
- nette/tester: ^2.0
- ninjify/nunjuck: 0.3.x-dev
- phpstan/phpstan: ^0.11
- phpstan/phpstan-deprecation-rules: ^0.11
- phpstan/phpstan-nette: ^0.11
- phpstan/phpstan-strict-rules: ^0.11
This package is auto-updated.
Last update: 2025-01-05 22:34:06 UTC
README
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