A very fast, very small translation component that you can quickly add to your website and start offering multi-language support. Taking advantage of MySql's powerful FULLTEXT index this is truly a highly efficient add-on.
The component is built in an Object Oriented style so it is very easy to integrate it in any project.
And it is really simple to use:
$t = new TC_Translation();
$t->t("This is the string I want to translate!");
All it takes is just two lines of code!
Because the translation component has a built in admin interface where you can search for strings in your websites, add new languages or translation we want to take advantage of the FULLTEXT feature that MySQL offers:
You have to edit your my.ini configuration file and under [mysqld] add these two lines:
[mysqld]
port=3306
ft_stopword_file = ''
ft_min_word_len = 3
The first variable ft_stopword_file tell the MySQL Server not to use any stopwords file. By default MySQL has a list of words that won't return any results via FULLTEXT search ( http://dev.mysql.com/doc/refman/5.5/en/fulltext-stopwords.html )
The next variable ft_min_word_len tells MySQL the length of the words you want to index. The recommended value for this variable is 3.
Inside the package you have a file translation.sql. Just import the table structure using phpMyAdmin or any other application.
Also, inside the package you have the class file called TC_Translation.php. If you open it you should see something like this:
//Database connection
const TR_HOST = 'localhost';
const TR_USER = 'root';
const TR_PASS = '';
const TR_DB = 'translation';
//Language options
const MIN_INDEX_LENGTH = 3;
Just include it like this, and you are all set to go:
<?php include "lib/TC_Translate.php"; ?>
The paths are different depending on where you choose to add the component to your website.
print $t->t('I want to translate this string');
Under the admin/ folder you have a administration interface that helps you translate the strings you wrapped around the t() method. You have a screenshot of its features. You can easily add CSS to make it look nice (admin/css/admin.css is already included).
Thank you for using this component!
public function __construct()
Creates a MySQL connection and stores it in a private variable ( the class constructor ).
/**
* boolean_search()
*
* @param string $str Unprepared string
* @return string Prepared string
*/
public function boolean_search($str)
/**
* boolean_match()
*
* @param string $str Unprepared string
* @return string Prepared string
*/
public function boolean_match($str)
Two methods that prepare the string for a FULLTEXT search in "BOOLEAN MODE"
/*
* setLanguage()
*
* @param string $lang Language code - sets the translation language
* @return boolean true - if language was succesfully set, false - otherwise
*/
public function setLanguage($lang = "")
You can easily switch through the languages you want the text to be translated in.
/**
* getLanguages()
*
* @param string $format Data format for languages
* @return mixed All the languages in the database
*/
public function getLanguages($format = "array")
Returns all languages in the database.
/**
* getAvailableTranslation()
*
* @param int $sid Unique string id
* @return array
*/
public function getAvailableTranslations($sid)
Get all available translations for a given string.
/**
* addLanguage()
*
* @param array $lang Language and language code
* @return boolean True is insertion was successful, False otherwhise
*/
public function addLanguage($lang = array())
Add another language to the database.
/**
* addTranslation()
*
* @param array $trans Translation details
* @return boolean
*/
public function addTranslation($trans = array())
Add translation for a given string.
/**
* search()
*
* @param string $str The keywords that were searched
* @return array The search results
*/
public function search($str)
Search for a given string.
/**
* t()
*
* @param string $str The string that needs to be translated
* @return string The translated string
*/
public function t($str = "")
Translate a given string.
public function _destruct()
Closes the MySQL connection ( the class destructor ).
Questions & Comments