Skip to content

thielicious/cLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cLang

Small API for managing multilingual websites.


INTRODUCTION

cLang ('c' abbr. for 'change') is a small API which you can use for cheap multilingual websites. It's using cookies and optimal for OnePages. No additional frameworks necessary, PHP 7.0.0+ required.


SETUP INFORMATION

Use your CLI and enter the following to clone:
git clone https://github.com/thielicious/cLang.git


USAGE (Example)

(inc.php) Create an object:
Parameters: cookie name, default language and alternative language

require "cLang.class.php";
$clang = new cLang("language-cookie", "de", "en");

(index.php) Create a menu for subsites in both languages:

<?php include "inc.php"; ?>
<ul id=menu>
	<li><a href=index.php><?= $clang->check("NEUIGKEITEN","NEWS") ?></a></li> | 
	<li><a href=?contact><?= $clang->check("KONTAKT", "CONTACT") ?></a></li>
</ul>

(index.php) Create a small simple UX to make visitors able to click and change language of the website:

<ul id=language>
	<li><a <?= $clang->active("de", "class=active-lang", true) ?> href=lang.php?lang=de>DE</a></li>
	<li><a <?= $clang->active("en", "class=active-lang") ?> href=lang.php?lang=en>EN</a></li>
</lu>

(index.php) Write some text in both languages inside of the content on every page:

<section id=content>
	<?php
		if (!count($_GET)) {
			echo "<h2>".$clang->check("Willkommen!","Welcome")."</h2>";
		} elseif (isset($_GET["contact"])) {
			echo "<h2>".$clang->check("Kontakt","Contact")."</h2>";
		}
	?>
</section>

(lang.php) Now use the following PRG pattern to handle a $_GET request when someone chooses a language:

include "inc.php";
if (isset($_GET["lang"])) {
	if ($_GET["lang"] == $clang->get("default") || $clang->get("language")) {
    		$clang->switch($_GET["lang"]);
	}
}
header("Location: ".$_SERVER["HTTP_REFERER"]);

METHODS

cLang::__construct(string $name, string $default, string $lang)

  • Choose a cookie name, a default language and another one that is supposed to be selected.

cLang::switch(string $lang)

  • This will switch to the selected language by re/placing its cookie.

cLang::reset()

  • Remove the cookie and the default language will be used upon page refresh.

cLang::get(string $param)

  • Return cookie name or language by using one of the following strings
    "name", "default", "language"

cLang::check(string $default, string $lang)

  • Return the content in the language that's currently set, otherwise it will pick default.

cLang::active(string $lang, string $style, $default = null)

  • Apply a CSS style to the active UX.
    Example: $clang->active("de", "class=active-lang", true)
    Use true as 3rd parameter if you would like to set the language as default.


🆕 A Demo has been added.



If you encounter any bugs, feel free to open up an issue, thank you.

thielicious.github.io

Releases

No releases published

Packages

No packages published

Languages