function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { /** @var ResponseInterface $response */ $response = $next(); $lang = $this->locale->locale(); // The check for app->translation is made here instead of conditionally adding this middleware // because loaded modules can change this setting. if (!$this->settings->translation || !$lang) { return $response; } if (!isset(self::$translation[$lang])) { // Load and merge all translation files now. self::$translation[$lang] = []; $trans =& self::$translation[$lang]; $folders = $this->settings->languageFolders; foreach ($folders as $folder) { $path = "{$folder}/{$lang}.ini"; $newTrans = fileExists($path) ? parse_ini_file($path) : null; if ($newTrans) { $trans = array_merge($trans, $newTrans); } } if (!$trans) { $paths = array_map(function ($path) { return "<li>" . ErrorConsole::shortFileName($path); }, $folders); throw new ConfigException("A translation file for language <b>{$lang}</b> was not found.\n<p>Search paths:\n<ul>" . implode('', $paths) . "</ul>", FlashType::ERROR); } } $out = preg_replace_callback(self::FIND_TRANS_KEY, function ($args) use($lang) { $a = $args[1]; return empty(self::$translation[$lang][$a]) ? '$' . $a : preg_replace('#\\r?\\n#', '<br>', self::$translation[$lang][$a]); }, $response->getBody()); return $response->withBody($this->responseFactory->makeBody($out)); }
protected function init() { parent::init(); $this->context->getAssetsService()->addInlineScript(<<<JS selenia.on ('languageChanged', function (lang) { \$ ('.LanguageSelector li').removeClass ('active'); \$ ('#btn-' + lang).addClass ('active'); }).setLang ('{$this->locale->locale()}'); JS , 'initLanguageSelector'); }
function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $mode = $this->settings->selectionMode(); $this->locale->selectionMode($mode); if ($mode == 'session') { $lang = $this->session->getLang() ?: $this->locale->defaultLang(); $this->locale->locale($lang); $this->session->setLang($lang); } if ($this->webConsole) { DebugConsole::logger('config')->inspect($this->locale); } return $next(); }
function Input(Input $input) { $input->props->apply(['lang' => $this->locale->locale()]); }