Giter Club home page Giter Club logo

clang's Introduction

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

clang's People

Contributors

thedefinitionist avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.