Skip to content

Instantly share code, notes, and snippets.

@NoTimeForHero
Last active June 6, 2018 23:01
Show Gist options
  • Save NoTimeForHero/58f99860cdcdfc137a4e1173f63d7b13 to your computer and use it in GitHub Desktop.
Save NoTimeForHero/58f99860cdcdfc137a4e1173f63d7b13 to your computer and use it in GitHub Desktop.
Simple website with 2 languages (using Twig, Phroute, php Gettext library)
<?php
require dirname(__FILE__). '/vendor/autoload.php';
use Gettext\Translator;
use Gettext\Translations;
$tplDir = dirname(__FILE__).'/templates/';
$tmpDir = dirname(__FILE__).'/tmp/cache/';
$loader = new Twig_Loader_Filesystem($tplDir);
//Create the translator instance
$I18N = new Translator();
// force auto-reload to always have the latest version of the template
$twig = new Twig_Environment($loader, array(
'cache' => $tmpDir,
'auto_reload' => true
));
// add Twig integration for language function
function twig_func_gettext($string) {
$I18N = $GLOBALS['I18N'];
return $I18N->gettext($string);
}
$function = new Twig_SimpleFunction('i18n', 'twig_func_gettext');
$twig->addFunction($function);
// Via: https://stackoverflow.com/a/25749660
function prefered_language(array $available_languages, $http_accept_language, $default_lang = "ru") {
$available_languages = array_flip($available_languages);
$langs = array();
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER);
foreach($matches as $match) {
list($a, $b) = explode('-', $match[1]) + array('', '');
$value = isset($match[2]) ? (float) $match[2] : 1.0;
if(isset($available_languages[$match[1]])) {
$langs[$match[1]] = $value;
continue;
}
if(isset($available_languages[$a])) {
$langs[$a] = $value - 0.1;
}
}
if (!$langs) return array($default_lang => 1.0);
arsort($langs);
return $langs;
}
$available_languages = array("en", "ru");
$langs = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"], "en");
// get first element of language array
@$lang = array_pop(array_reverse(array_keys($langs)));
// using cookie as preffered language source
if (isset($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
if (!in_array($lang, $available_languages)) $lang = 'en';
}
$locale = 'en_US';
if ($lang === 'ru') $locale = 'ru_RU';
// gettext have a strange bugs on Windows OS, no way to easy debug it
// so we using php library Gettext
$translations = Translations::fromPoFile(dirname(__FILE__)."/localization/$locale/LC_MESSAGES/localization.po");
$translations->toPhpArrayFile(dirname(__FILE__)."/tmp/$locale.php");
$I18N->loadTranslations(dirname(__FILE__)."/tmp/$locale.php");
$GLOBALS['LANGS'] = $available_languages;
$GLOBALS['I18N'] = $I18N;
$GLOBALS['TWIG'] = $twig;
$GLOBALS['LOCALE'] = $locale;
<?php
// All non-exists requests must be redirect to Index.php
// Packages must be installed via Composer
require ("config.php");
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$router = new RouteCollector();
$router->get('/lang/{lang}', function ($lang){
$available_languages = $GLOBALS['LANGS'];
if (!in_array($lang, $available_languages)) $lang = 'en';
setcookie('lang', $lang, time()+60*60, '/', NULL, false, true);
header("Location: /");
});
$router->get('/{page}?', function($page=null){
$twig = $GLOBALS['TWIG'];
$locale = $GLOBALS['LOCALE'];
if (!$page) return $twig->render('home.html',array('language' => $locale));
if ($twig->getLoader()->exists("$page.html")) {
return $twig->render("$page.html",array('language' => $locale));
}
// Not found case
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
return $twig->render('404.html',array('language' => $locale));
});
$dispatcher = new Dispatcher($router->getData());
try {
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
} catch (Phroute\Phroute\Exception\HttpRouteNotFoundException $ex) {
$response = $dispatcher->dispatch("GET", "/404.html");
}
echo $response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment