Add multiple language support
This commit is contained in:
parent
3763f335cb
commit
0094d7cbbd
5 changed files with 74 additions and 0 deletions
8
header.php
Normal file
8
header.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
require_once("language/language.php");
|
||||||
|
|
||||||
|
$lang = Language::getPreferredLanguage();
|
||||||
|
echo $lang->get("Test");
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
4
index.php
Normal file
4
index.php
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?php require_once("header.php"); ?>
|
||||||
|
|
||||||
|
<html lang="<?= $lang->languageCode ?>">
|
||||||
|
</html>
|
||||||
11
language/en.php
Normal file
11
language/en.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
require_once("language.php");
|
||||||
|
|
||||||
|
class English extends Language {
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct("en");
|
||||||
|
$this->keys = array("Test" => "Test EN");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
40
language/language.php
Normal file
40
language/language.php
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Language {
|
||||||
|
|
||||||
|
public $languageCode;
|
||||||
|
protected $keys;
|
||||||
|
|
||||||
|
public function __construct($languageCode) {
|
||||||
|
$this->languageCode = $languageCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key) {
|
||||||
|
return $this->keys[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPreferredLanguage() {
|
||||||
|
$language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
||||||
|
|
||||||
|
$languages = array(
|
||||||
|
"en",
|
||||||
|
"nl"
|
||||||
|
);
|
||||||
|
|
||||||
|
$preferredLanguage = Locale::lookup($languages, $language, true, "en");
|
||||||
|
|
||||||
|
switch ($preferredLanguage) {
|
||||||
|
case "nl":
|
||||||
|
require_once("nl.php");
|
||||||
|
return new Dutch();
|
||||||
|
case "en":
|
||||||
|
default:
|
||||||
|
require_once("en.php");
|
||||||
|
return new English();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
11
language/nl.php
Normal file
11
language/nl.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
require_once("language.php");
|
||||||
|
|
||||||
|
class Dutch extends Language {
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct("nl");
|
||||||
|
$this->keys = array("Test" => "Test NL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
Loading…
Reference in a new issue