mirror of
https://github.com/Mowie/Multilanguage.git
synced 2024-06-15 17:04:08 +00:00
Initial Commit
This commit is contained in:
parent
ec6faf695c
commit
5faeabf1f9
58
README.md
58
README.md
|
@ -1,2 +1,56 @@
|
|||
# Multilanguage
|
||||
A Framework to make your project multilanguagual.
|
||||
#Mowie Multilanguage
|
||||
|
||||
A Class which takes care of deploying your site in multiple languages.
|
||||
|
||||
##Usage
|
||||
|
||||
```php
|
||||
$lang = new lang($defaultLanguage);
|
||||
```
|
||||
|
||||
The default language, which will be used, if the users language is not found, is english. However, you can change this to any language you want.
|
||||
|
||||
```php
|
||||
$lang->setLangFolder('../langs');
|
||||
$lang->setLang($langfile, $lang);
|
||||
$lang->setLang('langfile.en.php', 'en');
|
||||
```
|
||||
|
||||
You can either define multiple languagefiles, which contain the languagestrings, or define a folder, where the different files are placed. If you do a folder, the files need to be named clearly.
|
||||
For example `lang.en.php`.
|
||||
|
||||
The script will find the file and use it.
|
||||
|
||||
You can also define multiple langfiles to get the strings of (see example.php)
|
||||
|
||||
To output a string, simply run the following command:
|
||||
```php
|
||||
$lang->get($identifier);
|
||||
$lang->get('Home');
|
||||
```
|
||||
Where `$identifier` is the Key _**OR**_ the value defined in the file.
|
||||
|
||||
##Reserved Identifiers
|
||||
|
||||
The two idetifiers `__Lang__` and `__Countrycode__` are reserved identifiers and used by the script to output a list of all available languages to the user.
|
||||
|
||||
##Langfile
|
||||
|
||||
A languagefile consists of the following code:
|
||||
|
||||
```php
|
||||
$lang = [];
|
||||
$lang['__Lang__'] = 'English (English)';
|
||||
$lang['__Countrycode__'] = 'en_US';
|
||||
|
||||
$lang['Home'] = 'Home is where your wifi connects automatically';
|
||||
$lang['About'] = 'About us';
|
||||
$lang['Contact'] = 'Contact us';
|
||||
...
|
||||
```
|
||||
|
||||
**NOTE:** To make sure all your languages are displayed correctly, set the file encoding to `UTF-8`.
|
||||
|
||||
##Examples
|
||||
|
||||
Examples can be found in the `examples` folder.
|
17
examples/example.php
Normal file
17
examples/example.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
require_once '../lang.class.php';
|
||||
|
||||
$lang = new lang();
|
||||
$lang->setLangFolder('langs/');
|
||||
$lang->setLang('lang.es.php', 'es');
|
||||
|
||||
echo $lang->get('Home').'<br/>';
|
||||
echo $lang->get('something');
|
||||
|
||||
|
||||
$lang->setLang('lang.de_extra.php', 'de');
|
||||
echo $lang->get('fail');
|
||||
|
||||
echo '<pre>';
|
||||
print_r($lang->getAll());
|
||||
echo '</pre>';
|
16
examples/lang.de_extra.php
Normal file
16
examples/lang.de_extra.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
/*
|
||||
* Mowie Language Class
|
||||
*
|
||||
* -----------------
|
||||
* LANGUAGE: German
|
||||
* -----------------
|
||||
*/
|
||||
$lang = [];
|
||||
$lang['__Lang__'] = 'German (Deutsch)';
|
||||
$lang['__LangCode__'] = 'de';
|
||||
$lang['__Countrycode__'] = 'de_DE';
|
||||
|
||||
$lang['Home'] = 'Zuhause';
|
||||
$lang['fail'] = 'Fehler';
|
||||
$lang['success'] = 'Erfolgreich';
|
16
examples/lang.es.php
Normal file
16
examples/lang.es.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
/*
|
||||
* Mowie Language Class
|
||||
*
|
||||
* -----------------
|
||||
* LANGUAGE: SPANISH
|
||||
* -----------------
|
||||
*/
|
||||
$lang = [];
|
||||
$lang['__Lang__'] = 'Spanish (Espagnol)';
|
||||
$lang['__LangCode__'] = 'es';
|
||||
$lang['__Countrycode__'] = 'es_es';
|
||||
|
||||
$lang['Home'] = 'Home is where your wifi connects automatically';
|
||||
$lang['About'] = 'About us';
|
||||
$lang['Contact'] = 'Contact us';
|
16
examples/langs/lang.de.php
Normal file
16
examples/langs/lang.de.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
/*
|
||||
* Mowie Language Class
|
||||
*
|
||||
* -----------------
|
||||
* LANGUAGE: German
|
||||
* -----------------
|
||||
*/
|
||||
$lang = [];
|
||||
$lang['__Lang__'] = 'German (Deutsch)';
|
||||
$lang['__LangCode__'] = 'de';
|
||||
$lang['__Countrycode__'] = 'de_DE';
|
||||
|
||||
$lang['Home'] = 'Zuhause ist, wo sich dein WLAN automatisch verbindet';
|
||||
$lang['About'] = 'Über uns';
|
||||
$lang['Contact'] = 'Kontakt';
|
13
examples/langs/lang.de_more.php
Normal file
13
examples/langs/lang.de_more.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
/*
|
||||
* Mowie Language Class
|
||||
*
|
||||
* -----------------
|
||||
* LANGUAGE: German
|
||||
* -----------------
|
||||
*/
|
||||
$lang['__Lang__'] = 'German (Deutsch)';
|
||||
$lang['__LangCode__'] = 'de';
|
||||
$lang['__Countrycode__'] = 'de_DE';
|
||||
|
||||
$lang['something'] = 'BAUM';
|
17
examples/langs/lang.en.php
Normal file
17
examples/langs/lang.en.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
/*
|
||||
* Mowie Language Class
|
||||
*
|
||||
* -----------------
|
||||
* LANGUAGE: ENGLISH
|
||||
* -----------------
|
||||
*/
|
||||
$lang = [];
|
||||
$lang['__Lang__'] = 'English (English)';
|
||||
$lang['__LangCode__'] = 'en';
|
||||
$lang['__Countrycode__'] = 'en_US';
|
||||
|
||||
$lang['Home'] = 'Home is where your wifi connects automatically';
|
||||
$lang['About'] = 'About us';
|
||||
$lang['Contact'] = 'Contact us';
|
||||
$lang['something'] = 'Tree';
|
107
lang.class.php
Normal file
107
lang.class.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
class lang
|
||||
{
|
||||
private $lang;
|
||||
private $langfiles;
|
||||
private $default;
|
||||
|
||||
//Determine the user's language
|
||||
function __construct($default = 'en')
|
||||
{
|
||||
$this->default = $default;
|
||||
$this->lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
|
||||
$this->langfiles = [];
|
||||
}
|
||||
|
||||
//Get Langfiles by Folder
|
||||
public function setLangFolder($folder)
|
||||
{
|
||||
//If langfolder exists...
|
||||
if (file_exists($folder) && is_dir($folder))
|
||||
{
|
||||
//Loop through it...
|
||||
if ($handle = opendir($folder))
|
||||
{
|
||||
while (false !== ($langfile = readdir($handle)))
|
||||
{
|
||||
if (strpos($langfile, 'lang') !== false)
|
||||
{
|
||||
//and put all langfiles in an array
|
||||
$lang = [];
|
||||
require $folder . $langfile;
|
||||
if (isset($this->langfiles[$lang['__LangCode__']]['langstrings'])) $langstrings = $this->langfiles[$lang['__LangCode__']]['langstrings'];
|
||||
foreach ($lang as $langKey => $langString)
|
||||
{
|
||||
$langstrings[$langKey] = $langString;
|
||||
}
|
||||
|
||||
//echo '<pre>'.print_r($langstrings, true).'</pre>';
|
||||
$this->langfiles[$lang['__LangCode__']] = ['Lang' => $lang['__Lang__'], 'countrycode' => $lang['__Countrycode__'], 'file' => $folder . $langfile, 'langstrings' => $langstrings];
|
||||
}
|
||||
}
|
||||
closedir($handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Get Langfiles by direct declaration
|
||||
public function setLang($langfile, $langC)
|
||||
{
|
||||
$lang = [];
|
||||
require $langfile;
|
||||
if (isset($this->langfiles[$lang['__LangCode__']]['langstrings'])) $langstrings = $this->langfiles[$lang['__LangCode__']]['langstrings'];
|
||||
foreach ($lang as $langKey => $langString)
|
||||
{
|
||||
$langstrings[$langKey] = $langString;
|
||||
}
|
||||
|
||||
$this->langfiles[$lang['__LangCode__']] = ['Lang' => $lang['__Lang__'], 'countrycode' => $lang['__Countrycode__'], 'file' => $langfile, 'langstrings' => $langstrings];
|
||||
}
|
||||
|
||||
//Get all available languages
|
||||
public function getAll()
|
||||
{
|
||||
return $this->langfiles;
|
||||
}
|
||||
|
||||
//Get langstrings
|
||||
public function get($identifier)
|
||||
{
|
||||
//If Langueage exists...
|
||||
if (array_key_exists($this->lang, $this->langfiles))
|
||||
{
|
||||
//...and a value for the corresponding key
|
||||
if (array_key_exists($identifier, $this->langfiles[$this->lang]['langstrings']))
|
||||
{
|
||||
//Outpout it
|
||||
return $this->langfiles[$this->lang]['langstrings'][$identifier];
|
||||
}
|
||||
else
|
||||
{
|
||||
//If not, fallback to the default language
|
||||
if (array_key_exists($identifier, $this->langfiles[$this->default]['langstrings']))
|
||||
{
|
||||
return $this->langfiles[$this->default]['langstrings'][$identifier];
|
||||
}
|
||||
else
|
||||
{
|
||||
//If this doesn't exists, return the identifier
|
||||
return $identifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
else//Fallback to the default language
|
||||
{
|
||||
if (array_key_exists($identifier, $this->langfiles[$this->default]['langstrings']))
|
||||
{
|
||||
return $this->langfiles[$this->default]['langstrings'][$identifier];
|
||||
}
|
||||
else
|
||||
{
|
||||
//If this doesn't exists, return the identifier
|
||||
return $identifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user