activeId = $id; return true; } /** * @return Language */ public function getActive() { $active = $this->getActiveId(); // use `getAll` so inactive language can be used (for example tmp wpjshop domain with not active language) // validation should be done explicitly when remembering or loading language $supported = $this->getAll(); if (!isset($supported[$active])) { $active = key($supported); } $result = $supported[$active]; // TODO: Dočasně kontrolujeme, že je všude vyplněný language code if (isDevelopment() && empty($result->getLocale())) { throw new \Exception('Prázdný language code'); } return $result; } /** * @return Language[] */ public function getSupported() { if (!$this->supported) { $this->supported = $this->loadSupported(); } return $this->supported; } /** * @return array */ public function getAll(): array { if ($this->languages !== null) { return $this->languages; } $lang = $this->getDefaultFromConfig(); $locale = Config::get()->getFromArray('Lang')['language_code'] ?? 'cs_CZ'; return $this->languages = [$lang => (new Language())->setId($lang)->setName($lang)->setLocale($locale)]; } /** * @return bool */ protected function validate($id) { return array_key_exists($id, $this->getSupported()); } /** * @return Language[] */ protected function loadSupported() { return $this->getAll(); } protected function loadActive() { return $this->getDefaultFromConfig(); } public function getDefaultId(): string { return $this->getDefaultFromConfig(); } public function translationActive() { if ($this->getActiveId() == $this->getDefaultId()) { return false; } return true; } protected function getDefaultFromConfig() { return Config::get()->getFromArray('Lang')['language']; } public function getActiveId(): string { if (is_null($this->activeId)) { $this->activeId = $this->loadActive(); } return $this->activeId; } public function getInheritance(string $language): array { return [$language]; } public function clearCache(): void { $this->activeId = null; } public function isValid(string $id): bool { return $this->validate($id); } }