ts-website/include/language.php

50 lines
1.1 KiB
PHP
Raw Normal View History

2016-07-25 14:14:04 +00:00
<?php
session_start();
header('Cache-control: private');
if (isset($_GET['lang'])) {
$langcode = $_GET['lang'];
$_SESSION['lang'] = $langcode;
setcookie('lang', $langcode, time() + (3600 * 24 * 60));
header("Location: {$_SERVER['PHP_SELF']}");
2016-07-25 14:14:04 +00:00
} else if (isset($_SESSION['lang'])) {
$langcode = $_SESSION['lang'];
} else if (isset($_COOKIE['lang'])) {
$langcode = $_COOKIE['lang'];
} else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
2016-07-25 14:14:04 +00:00
$langcode = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
} else {
$langcode = "en";
}
if (!file_exists(getLanguagePath($langcode)))
2016-07-25 14:14:04 +00:00
$langcode = "en";
require_once getLanguagePath($langcode);
function getLanguagePath($langcode) {
return __DIR__ . '/../config/languages/lang.' . $langcode . '.php';
}
function tl($pattern, $args = null) {
echo translate($pattern, $args);
}
function translate($pattern, $args = null) {
if (!$args) {
2016-07-25 14:14:04 +00:00
return $pattern;
}
$size = count($args);
for ($i = 0; $i < $size; $i++) {
$pattern = str_ireplace('{' . $i . '}', $args[$i], $pattern);
}
return $pattern;
}