I will explain how to do it for a generic PHP project, which should work for Moodle too.
Firstly create a separate folder named lang in your project. This folder can be used to put your language packs and unicode handler libraries.
For unicode handling , save the following file as portable-utf8.php in the lang folder.
portable-utf8.php
Your language pack files can be named as bahar.php, tamil.php etc. and saved in the same location.
A sample file should look like this; with the keyword string followed by the translation.
<?php
function get_translate($value){
$translations=array(
"yourstring1"=>"விஷேட அம்சங்கள்",
"yourstring2"=>"பின் செல்ல",
"yourstring3"=>"தொலைபேசி எண்",
"yourstring4"=>"தேர்ந்தெடுக்கப்பட்ட பண்ணை",
"yourstring5"=>"கிராமம்",
);
return utf8_html_encode($translations[$value]);
}
?>
Then you need to change the strings in the files you need with this:
<?php echo get_translate("your_string");?>
Please note that this string should match with the string in your language pack file.
The language of your choice is passed from page to page through the GET method. So you will need to pass it when when linking. Optionally, if you want to do dynamic translation on a button click, you can just use this jQuery code.
$("#yourid").on('click', function(e) {
var split = location.search.replace('?', '').split('=');
if(split[1]=='yourlang1'){
window.location.href="yourdir/yourfile1.php?lang=yourlang1";
}
else if (split[1]=='yourlang2'){
window.location.href="yourdir/yourfile2.php?lang=yourlang2";
}
else if (split[1]=='yourlang'){
window.location.href=""yourdir/yourfile3.php?lang=yourlang3";
}
else{
window.location.href="yourdir/yourfile4.php";
}
});
Finally call your language pack on the top of the PHP file where the translation needs to be done; like this:
<?php
$lng='';
// setting up language
require 'yourdir/lang/portable-utf8.php';
if(isset($_GET['lang'])){
$lng=$_GET['lang'];
if($_GET['lang']=="yourlang1"){
require 'yourdir/lang/yourlang1.php';
}
elseif ($_GET['lang']=="yourlang2")
{
require 'yourdir/lang/yourlang2.php';
}
else{
require 'yourdir/lang/yourlang3.php';
}
}else{
$lng='yourlangdefault';
require 'lang/yourlangdefualt.php';
}
?>
this might look lengthy at first, but when you have hundreds, or maybe thousands of strings to add, this is the easiest way. All you need to do is add the PHP code to the top of your page, replace the string as given above with a single line of code and add the string to your language pack file if it does not exist.
If you need any further explaination, please let me know.