/** * index action * * @return string */ public function indexAction() { $Module = new \System\Libraries\Modules($this->Silexapp); $Module->bindTextDomainForModule('cms'); // load functions $this->Loader->loadFunctions('language'); // list languages for language selector. $LanguagesDb = new \System\Core\Models\LanguagesDb($this->Silexapp['Db']); $language_file = $LanguagesDb->getConfigFile(); if (is_file($language_file)) { $languages_config = (include $language_file); } unset($LanguagesDb, $language_file); if (isset($languages_config) && is_array($languages_config) && array_key_exists('languages', $languages_config)) { $data['languages'] = $languages_config['languages']; } else { $data['languages'] = []; } unset($languages_config); $data['Uri'] = new \System\Libraries\Uri(); $data['framework_name'] = _t('Agni Framework', 'cms'); $data['page_content'] = $this->Theme->render('front/templates/index/index_v', $data); return $this->Theme->render('front/templates/body_v', $data); }
/** * setup i18n URI and detect the language in URI. */ public function i18nUri() { if ($this->Profiler != null) { $this->Profiler->Console->timeload('Initialize the language locale uri.', __FILE__, __LINE__); $this->Profiler->Console->memoryUsage('Initialize the language locale uri.', __FILE__, __LINE__ - 1); } $configdb = new \System\Core\Models\ConfigDb($this->Silexapp['Db']); if ($configdb->getConfigValue('site_lang_method', 'uri') == 'cookie') { // if detect language method is using cookie. $language_cookie_name = $_SERVER['HTTP_HOST'] . '_CMS_LANGUAGE_LOCALE_URI'; $request = new \Symfony\Component\HttpFoundation\Request($_GET, $_POST, [], $_COOKIE); if ($request->cookies->has($language_cookie_name)) { $this->language_locale_uri = $request->cookies->get($language_cookie_name); } else { $LangDb = new \System\Core\Models\LanguagesDb($this->Silexapp['Db']); $this->language_locale_uri = $LangDb->getDefaultLanguageUri(); $cookie = new \Symfony\Component\HttpFoundation\Cookie($language_cookie_name, $LangDb->getDefaultLanguageUri(), time() + 60 * 60 * 24 * 30, '/', null, isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? true : false); $response = new \Symfony\Component\HttpFoundation\Response(); $response->headers->setCookie($cookie); $response->send(); } unset($configdb, $cookie, $LangDb, $language_cookie_name, $request, $response); } else { // if detect language method is using URI. $uri = new \System\Libraries\Uri(); $uris_arr = $uri->getUriSegments(true); // get language locale uri from REQUEST_URI. this does not check the exists language yet. if (isset($uris_arr[1])) { $language_locale_uri = $uris_arr[1]; } else { $language_locale_uri = $uris_arr[count($uris_arr) - 1]; } // check that language locale uri is in language list or not. non-match or empty language locale uri will be use default language locale uri. $LangDb = new \System\Core\Models\LanguagesDb($this->Silexapp['Db']); $this->language_locale_uri = $LangDb->getLanguageUri($language_locale_uri); // check and detect language redirect. if ($this->language_locale_uri != $language_locale_uri) { // if checked language locale URI is not match the detected one. example: th != page, en-US != fr-FR where page, or fr-FR is not exists in language db. // set language locale uri to global variable $_SERVER. $_SERVER['CMS_LANGUAGE_LOCALE_URI'] = $this->language_locale_uri; if ($configdb->getConfigValue('site_lang_uri_default_visible', '1') == '1') { // if settings was set to force show language URI, redirect to new url with language prefixed. $uris_arr[0] = $this->language_locale_uri; $new_uri = implode('/', $uris_arr); unset($configdb, $LangDb, $language_locale_uri, $uris_arr); // redirect from EX. http://domain.tld/myapp/index.php to http://domain.tld/myapp/index.php/th, http://domain.tld/myapp/index.php/page/subpage -> http://domain.tld/index.php/th/page/subpage header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $uri->createUrl($new_uri, 'auto', false)); exit; } } else { // if checked language locale URI is match detected one. // set language locale uri to global variable $_SERVER. $_SERVER['CMS_LANGUAGE_LOCALE_URI'] = $language_locale_uri; if ($configdb->getConfigValue('site_lang_uri_default_visible', '1') == '0') { // if default language is set to not visible, redirect to the URL where it has no language locale URI. $default_language_locale_uri = $LangDb->getDefaultLanguageUri(); if ($language_locale_uri == $default_language_locale_uri) { // if detected language locale uri is default. example: default is 'th'; url is http://domain.tld/myapp/index.php/th/page/subpage; this language locale uri is default language! unset($_SERVER['CMS_LANGUAGE_LOCALE_URI']); $uris_arr = $uri->getUriSegments(true); if (isset($uris_arr[1]) && $uris_arr[1] != null) { unset($uris_arr[1]); } $new_uri = implode('/', $uris_arr); unset($configdb, $LangDb, $language_locale_uri, $uris_arr); // redirect from IE. http://domain.tld/myapp/index.php/th to http://domain.tld/myapp/index.php where th is default language. header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $uri->createUrl($new_uri, 'auto', false)); exit; } } } // endif check and detect language redirect. // re-setting request uri key. -------------------------------------------------------------- // in case that the url contain language locale uri. for example: uri is http://domain.tld/myapp/index.php/th/page/subpage; the original REQUEST_URI will be /myapp/index.php/th/page/subpage // with this re-setting request uri key, the modified REQUEST_URI will be /myapp/index.php/page/subpage. so the routes can works properly. $index_file = $uri->getIndexFile(); $uris_arr = $uri->getUriSegments(true); if (isset($uris_arr[1]) && $uris_arr[1] == $this->language_locale_uri) { unset($uris_arr[1]); } $uri_string = implode('/', $uris_arr); $_SERVER['REQUEST_URI'] = $uri->getBaseUri() . ($index_file != null ? '/' . $index_file : '') . $uri_string; unset($configdb, $default_language_locale_uri, $index_file, $language_locale_uri, $uri, $uris_arr, $uri_string); } // endif; site_lang_method // set share variable via Silex\Application. $this->Silexapp['language_locale_uri'] = $this->language_locale_uri; $this->Silexapp->register(new \Silex\Provider\TranslationServiceProvider(), ['locale' => $LangDb->getLanguageData($this->language_locale_uri), 'locale_fallbacks' => [$LangDb->getLanguageData('')]]); putenv('LC_ALL=' . $this->language_locale_uri); setlocale(LC_ALL, explode(',', str_replace(', ', ',', $LangDb->getLanguageData($this->language_locale_uri)))); // start to working with php-gettext. $this->Silexapp['Language'] = $this->Silexapp->share(function () { return new \System\Libraries\Language($this->language_locale_uri); }); unset($LangDb); if ($this->Profiler != null) { $this->Profiler->Console->timeload('Finished the language locale uri.', __FILE__, __LINE__); $this->Profiler->Console->memoryUsage('Finished the language locale uri.', __FILE__, __LINE__ - 1); } }