Commit edf04705 authored by M.Nowocyn's avatar M.Nowocyn

Reworking the language system. Support Browserlanguage parameter and cookies

parent 97db8863
......@@ -16,8 +16,9 @@ use PDO;
class Language extends Mixed implements iLanguage
{
protected static $cache;
protected static $cache_exist;
protected static $cache_args = array();
protected static $cache = array();
protected static $cache_exist = array();
public static function get ($id = 0, $reload = false): iLanguage
{
......
......@@ -23,6 +23,7 @@ namespace Iko\Language;
use Iko\Config;
use Iko\Core;
use Iko\Exception;
use Iko\iLanguage;
use Iko\Language;
use Iko\Lib\Singleton\Instance;
......@@ -31,173 +32,196 @@ use Iko\User;
class System extends Instance implements iSystem
{
const DEFAULT = "english";
const TABLE = "{prefix}translation";
const LANG_TABLE = "{prefix}languages";
const LANG_NAME = "language";
const NAME = "translation_key";
const FIXED = array (
"german",
"english");
protected static $instance = null;
protected static $args = null;
public static function getInstance ($args = null, $reload = false, $property = true): iSystem
{
return parent::getInstance(
$args,
$reload,
$property
);
}
private $config = null;
private $current = null;
private $languages = array ();
protected function __construct ($args)
{
if ($args !== null && Language::exist($args)) {
$lang = $args;
}
else {
$lang = null;
if (isset($_GET['lang']) && Language::exist($_GET['lang'])) {
$lang = htmlspecialchars($_GET['lang']);
}
else {
if (Session::isLoggedIn()) {
$lang = User::getSession()->getLanguage();
}
if ($lang == null || $lang == "") {
$this->config = Config::load(
"pdo",
"language"
);
$lang = $this->config->default_language ?? self::DEFAULT;
}
}
}
$this->current = Language::get($lang);
$sql = "SHOW COLUMNS FROM " . self::TABLE;
$statement = Core::PDO()->query($sql);
$fetch_all = $statement->fetchAll();
foreach ($fetch_all as $item) {
if ($item["Field"] != self::NAME) {
array_push(
$this->languages,
Language::get($item["Field"])
);
}
}
}
public function getCurrent (): iLanguage
{
return $this->current;
}
public function isSupportedLanguage ($lang): bool
{
if (!($lang instanceof iLanguage)) {
$lang = Language::get($lang);
}
if (array_search(
$lang,
$this->languages
) !== false) {
return true;
}
else {
return false;
}
}
public function getLanguages (): array
{
return $this->languages;
}
private function change (string $func, $name): bool
{
if ($name instanceof iLanguage) {
$name = $name->getName();
}
$name = strtolower($name);
if (($func == "add" && !$this->isSupportedLanguage(
$name
)) || ($func == "remove" && $this->isSupportedLanguage($name) && array_search(
$name,
self::FIXED,
true
) === false)) {
if (User::getSession()->hasPermission("iko.language." . $func . ".lang")) {
if ($func == "add") {
$sql = "ALTER TABLE " . self::TABLE . " ADD :lang TEXT";
$sql_update = "INSERT INTO " . Language::TABLE . " ( " . Language::NAME . ",status) VALUE(:lang, '" . $this->config->default_status . "')";
}
else {
$sql_update = "DELETE FROM " . Language::TABLE . " WHERE " . Language::NAME . " = :lang";
$sql = "ALTER TABLE " . self::TABLE . " DROP :lang";
}
$statement = Core::PDO()->prepare($sql);
$statement->bindParam(
":lang",
$name
);
$statement->execute();
if ($statement > 0) {
$statement_update = Core::PDO()->prepare($sql_update);
$statement_update->bindParam(
":lang",
$name
);
$statement_update->execute();
if ($statement_update > 0) {
if ($func == "add") {
array_push(
$this->languages,
Language::get(
$name,
true
)
);
}
else {
unset(
$this->languages[ array_search(
$name,
$this->languages,
true
) ]
);
}
return true;
}
}
}
}
return false;
}
public function add ($name): bool
{
return $this->change(
__FUNCTION__,
$name
);
}
public function remove ($name): bool
{
return $this->change(
__FUNCTION__,
$name
);
}
const DEFAULT = "en";
const TABLE = "{prefix}translation";
const LANG_TABLE = "{prefix}languages";
const LANG_NAME = "language";
const NAME = "translation_key";
const FIXED = array (
"de",
"en");
protected static $instance = null;
protected static $args = null;
public static function getInstance ($args = null, $reload = false, $property = true): iSystem
{
return parent::getInstance(
$args,
$reload,
$property
);
}
private $config = null;
private $current = null;
private $languages = array ();
//Todo: change loading
protected function __construct ($args)
{
if ($args !== null && Language::exist($args)) {
$lang = $args;
}
else {
$lang = null;
if (isset($_GET['lang']) && Language::exist($_GET['lang'])) {
$lang = htmlspecialchars($_GET['lang']);
if(isset($_COOKIE[self::COOKIE])) {
$delete = setcookie(self::COOKIE, "nn", time()- 3600);
}
$set = setcookie(self::COOKIE, $lang, time()+60*60*24*30, "", "", false);
if(!$set) {
throw new Exception("language | error | 1001 | Can not set cookie properly.");
}
}
else {
if (Session::isLoggedIn()) {
$lang = User::getSession()->getLanguage();
}
if ($lang == null || $lang == "") {
$this->config = Config::load(
"pdo",
"language"
);
$browser = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$browser = substr(
$browser,
0,
2
);
$cookie = $_COOKIE[self::COOKIE] ?? null;
if($_COOKIE !== NULL && Language::exist($cookie)) {
$lang = $cookie;
}
else if (Language::exist($browser)) {
$lang = $browser;
}
else {
$lang = $this->config->default_language ?? self::DEFAULT;
}
}
}
}
$this->current = Language::get($lang);
$sql = "SHOW COLUMNS FROM " . self::TABLE;
$statement = Core::PDO()->query($sql);
$fetch_all = $statement->fetchAll();
foreach ($fetch_all as $item) {
if ($item["Field"] != self::NAME) {
array_push(
$this->languages,
Language::get($item["Field"])
);
}
}
}
public function getCurrent (): iLanguage
{
return $this->current;
}
public function isSupportedLanguage ($lang): bool
{
if (!($lang instanceof iLanguage)) {
$lang = Language::get($lang);
}
if (array_search(
$lang,
$this->languages
) !== false) {
return true;
}
else {
return false;
}
}
public function getLanguages (): array
{
return $this->languages;
}
private function change (string $func, $name): bool
{
if ($name instanceof iLanguage) {
$name = $name->getName();
}
$name = strtolower($name);
if (($func == "add" && !$this->isSupportedLanguage(
$name
)) || ($func == "remove" && $this->isSupportedLanguage($name) && array_search(
$name,
self::FIXED,
true
) === false)) {
if (User::getSession()->hasPermission("iko.language." . $func . ".lang")) {
if ($func == "add") {
$sql = "ALTER TABLE " . self::TABLE . " ADD :lang TEXT";
$sql_update = "INSERT INTO " . Language::TABLE . " ( " . Language::NAME . ",status) VALUE(:lang, '" . $this->config->default_status . "')";
}
else {
$sql_update = "DELETE FROM " . Language::TABLE . " WHERE " . Language::NAME . " = :lang";
$sql = "ALTER TABLE " . self::TABLE . " DROP :lang";
}
$statement = Core::PDO()->prepare($sql);
$statement->bindParam(
":lang",
$name
);
$statement->execute();
if ($statement > 0) {
$statement_update = Core::PDO()->prepare($sql_update);
$statement_update->bindParam(
":lang",
$name
);
$statement_update->execute();
if ($statement_update > 0) {
if ($func == "add") {
array_push(
$this->languages,
Language::get(
$name,
true
)
);
}
else {
unset(
$this->languages[ array_search(
$name,
$this->languages,
true
) ]
);
}
return true;
}
}
}
}
return false;
}
public function add ($name): bool
{
return $this->change(
__FUNCTION__,
$name
);
}
public function remove ($name): bool
{
return $this->change(
__FUNCTION__,
$name
);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Matze
* Date: 29.12.2018
* Time: 23:25
*/
namespace Iko\Language;
class User {
//Todo: Module - handler last hinzufügen ..
/*
* Funktionsweise: Das Modul wird aufgerufen und anhand von dem Browser wird die Sprache ermittelt und direkt hinterlegt bzw. das Cookie, dass dazu genutzt wird hinterlegt.
*/
public static function registration($name, $data, $pre) {
}
}
\ No newline at end of file
......@@ -23,6 +23,7 @@ use Iko\iLanguage;
interface iSystem
{
const COOKIE = "language";
public function isSupportedLanguage ($lang ): bool;
public function getCurrent (): iLanguage;
......
<?php
/**
* Created by PhpStorm.
* User: Marcel
* Date: 19.11.2018
* Time: 12:33
*/
$sql = array (
"UPDATE " . \Iko\Language\System::LANG_TABLE . " Set " . Iko\Language\System::LANG_NAME . " = 'en' WHERE " . Iko\Language::NAME . " = 'english'",
"UPDATE " . \Iko\Language\System::LANG_TABLE . " Set " . Iko\Language\System::LANG_NAME . " = 'de' WHERE " . Iko\Language::NAME . " = 'german'",
"ALTER TABLE " . Iko\Language\System::TABLE . " CHANGE COLUMN 'english' 'en' TEXT NOT NULL, CHANGE COLUMN 'german' 'de' TEXT NOT NULL");
$config = \Iko\Config::load(
"pdo",
"language"
);
if ($config->get("default_language") == "german") {
$config->set(
"default_language",
"de"
);
}
elseif ($config->get("default_language") == "english") {
$config->set(
"default_language",
"en"
);
}
$default = $config->get("default_language");
array_push(
$sql,
"UPDATE " . \Iko\User::TABLE . " Set user_language = '" . $default . "'"
);
foreach ($sql as $item) {
\Iko\Core::PDO()->exec($item);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment