From 5faeabf1f9907a89397c8953ff9dbfb69ab98859 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 16 Jun 2016 13:02:23 +0200 Subject: [PATCH] Initial Commit --- README.md | 58 ++++++++++++++++- examples/example.php | 17 +++++ examples/lang.de_extra.php | 16 +++++ examples/lang.es.php | 16 +++++ examples/langs/lang.de.php | 16 +++++ examples/langs/lang.de_more.php | 13 ++++ examples/langs/lang.en.php | 17 +++++ lang.class.php | 107 ++++++++++++++++++++++++++++++++ 8 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 examples/example.php create mode 100644 examples/lang.de_extra.php create mode 100644 examples/lang.es.php create mode 100644 examples/langs/lang.de.php create mode 100644 examples/langs/lang.de_more.php create mode 100644 examples/langs/lang.en.php create mode 100644 lang.class.php diff --git a/README.md b/README.md index 06fbf66..b3af1ef 100644 --- a/README.md +++ b/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. \ No newline at end of file diff --git a/examples/example.php b/examples/example.php new file mode 100644 index 0000000..1fffd70 --- /dev/null +++ b/examples/example.php @@ -0,0 +1,17 @@ +setLangFolder('langs/'); +$lang->setLang('lang.es.php', 'es'); + +echo $lang->get('Home').'
'; +echo $lang->get('something'); + + +$lang->setLang('lang.de_extra.php', 'de'); +echo $lang->get('fail'); + +echo '
';
+print_r($lang->getAll());
+echo '
'; \ No newline at end of file diff --git a/examples/lang.de_extra.php b/examples/lang.de_extra.php new file mode 100644 index 0000000..fd726c1 --- /dev/null +++ b/examples/lang.de_extra.php @@ -0,0 +1,16 @@ +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 '
'.print_r($langstrings, true).'
'; + $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; + } + } + } +} \ No newline at end of file