Location: PHPKode > scripts > Language Definition > language-definition/LanguageDefinition.class.php
// ----------------------------------------------------------------------
// Copyright (C) 2009 by Muhammed Mamedov.
// http://www.turkmenweb.com/
// ----------------------------------------------------------------------
// This program is open source product; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Class Name: LanguageDefinition
// Filename:   LanguageDefinition.class.php
// Author: 	   Muhammed Mamedov <hide@address.com>
// Purpose:    Localize any website with ease
// ----------------------------------------------------------------------

Building a multilingual website? Want to change languages on the fly, in an Object Oriented way? This is the class I use to support UTF-8 websites. Gettext does a similar job, but it wants you to provide locale, and extension has to be on PHP. My class however, doesn't require any extensions to be enabled - it is a standalone application, and let's you support ANY language you want.

1) Create your language definition files (_lang.php) for each language
2) Initiate LanguageDefinition object, and pass the name of the language you want to load, and it will load definitions from appropriate 'language_lang.php' files
3) Access language terms using $lng->__('Home') or in convenient short form <?=__('Home')?>
4) Don't forget to set character_encoding to utf-8 in your pages, and language files
5) If a language term isn't defined, then that term name is printed on the page, making sure you always have text on the site when you need it. This also avoids language errors from stopping your page to display. You can always find non-working terms by activating the debug mode(showed below).

Sample Code:

Language Definition file
For language name="abc", you need to have abc_lang.php file
In the example below $language='ru', so you will need to have this 
file in '/path_to_language_defitinion_classes/ru_lang.php'

//Entries are saved in $lang array
$lang = array(

Main Code:

$language='ru'; //you can set this through a session or cookie or whatever
$lng = new LanguageDefinition('lng'); //you need to pass string with the name of the variable

// Optional; this will store terms that weren't defined in language files.
// This is useful if you need to see if you are using any terms(or misspelled) that 
// aren't defined in language file 
// To see debug info, simply: print_r($lng->getDebug()), at the end of your php/html page.


// Call from any PHP file in short way
// You can rename __() function in LanguageDefinition.class.php to any name you want 

// Call from any PHP directly by using object

//Change language on the fly

//Now definition for 'days' will load from 'AnyLanguageYouWant_lang.php' file


class LanguageDefinition
	const filestr='_lang.php'; //ending of language files
	private $baseloc; 	//Base location of language data to load. Absolute path, with trailing slash
	private $langName; 	//Language name. Language definitions are loaded based on this name.
	private $terms; 	//Holds all the terms for this language, loaded from the language file
	private static $LDInstanceVar; //Holds variable that holds this created class.
	private $debug=false;
	private $debugInfo; //Array to hold debug info	

	//If language to be loaded is ru, then the file
	//for language will be ru_lang.php
	//if you have initialized this class as $l= new LanguageDefinition('l')
	//in constructor you have to pass variable name
	public function __construct($classVarName){
	//Sets base location (absolute path) where language definition file are located
	public function setBase($loc){
	//Sets language, appropriate language file is going to be called.
	//i.e if you call setLang('ru'), 
	public function setLang($lname){
		$this->langName = $lname;


	public function getLang(){
		return $this->langName;
	//Return term for the corresponding language from the file
	//If definition for the term isn't found, return that term as a string
	public function __($term){
			return $this->terms[$term];
		else {
			//if we are in debug mode, then log terms that weren't found
				//add only if we don't have this term added already
			//if term wasn't found, return that term as a string.
			return $term;
	//processes language file, adds variables to our arary here
	private function processLangFile($file){
		 catch(Exception $e)
			  echo 'LanguageDefinition: ' .$e->getMessage();
		//now you have $lang array with all the variables, it comes from include file
			throw new Exception("LanguageDefinition: ");
	//Returns variable name to which this class was assigned during creation.
	public static function getClassVar(){
		return self::$LDInstanceVar;
	//Debug mode when $bool=true
	//In Debug mode, all terms that weren't found in language definition file,
	//but were used in the code are stored and can be retrieved by calling getDebugInfo();
	public function setDebug($bool)
		if(is_bool($bool)) {
			$this->debugInfo = array(); //initialize debugInfo to array
	//Return boolean - true if debug mode is on, false if debug mode is off
	public function getDebug(){
		return $this->debug;

	//Return debug info
	public function getDebugInfo(){
		return $this->debugInfo;

//Short form of calling terms, you can change this function name to any name you want
function __($term)
{	$classVar = LanguageDefinition::getClassVar();
	global $$classVar;
	return $$classVar->__($term);

Return current item: Language Definition