Skip to content

Instantly share code, notes, and snippets.

@foxycode
Last active July 31, 2018 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxycode/24505f2c5e6fb3c488f11764d511cbf3 to your computer and use it in GitHub Desktop.
Save foxycode/24505f2c5e6fb3c488f11764d511cbf3 to your computer and use it in GitHub Desktop.
Nette Multilang App
<?php declare(strict_types=1);
namespace App\Presenters;
use Kdyby\Translation\Translator;
use Nette\Application\Helpers;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
/**
* @var string
* @persistent
*/
public $locale;
/**
* @var Translator
*/
protected $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
public function canonicalize()
{
if (!$this->locale) {
$this->locale = $this->translator->getLocale();
$this->redirect('this');
}
parent::canonicalize();
}
public function formatLayoutTemplateFiles()
{
$layout = $this->layout ? $this->layout : 'layout';
$dir = dirname($this->getReflection()->getFileName());
return ["{$dir}/templates/@{$layout}_{$this->locale}.latte"];
}
public function formatTemplateFiles()
{
[, $presenter] = Helpers::splitName($this->getName());
$dir = dirname($this->getReflection()->getFileName());
return ["{$dir}/templates/{$presenter}/{$this->view}_{$this->locale}.latte"];
}
}
extensions:
translation: Kdyby\Translation\DI\TranslationExtension
translation:
default: en
whitelist: [cs, en, de, it]
fallback: [cs_CZ]
<?php declare(strict_type=2);
namespace App;
use Nette;
use Nette\Application\IRouter;
use Nette\Application\Routers\RouteList;
use Nette\Application\Routers\Route;
final class RouterFactory
{
use Nette\StaticClass;
public static function createRouter(): IRouter
{
$router = new RouteList;
$router[] = new Route('cs/<presenter>/<action>[/<id>]', [
'locale' => 'cs',
'presenter' => [
Route::VALUE => 'Homepage',
Route::FILTER_TABLE => [
'kontakt' => 'Contact',
'zakaznici' => 'Customers',
'vyrobci' => 'Manufacturers',
'produkty' => 'Products',
],
],
'action' => 'default',
'id' => NULL,
]);
$router[] = new Route('de/<presenter>/<action>[/<id>]', [
'locale' => 'de',
'presenter' => [
Route::VALUE => 'Homepage',
Route::FILTER_TABLE => [
'kontakt' => 'Contact',
'kundschaft' => 'Customers',
'hersteller' => 'Manufacturers',
'produkte' => 'Products',
],
],
'action' => 'default',
'id' => NULL,
]);
$router[] = new Route('it/<presenter>/<action>[/<id>]', [
'locale' => 'it',
'presenter' => [
Route::VALUE => 'Homepage',
Route::FILTER_TABLE => [
'contatto' => 'Contact',
'clienti' => 'Customers',
'produttori' => 'Manufacturers',
'prodotti' => 'Products',
],
],
'action' => 'default',
'id' => NULL,
]);
$router[] = new Route('[<locale cs|en|de|it>/]<presenter>/<action>[/<id>]', 'Homepage:default');
return $router;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment