Reputation: 3118
I am customizing moodle in a new project. I want to add my limited strings to my special language file, in order to call it in any place. So I need to add a new file to language packs, e.g. bahar.php
Upvotes: 1
Views: 1379
Reputation: 2452
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.
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.
Upvotes: 0
Reputation: 10221
For a local plugin the folder is
/local/yourpluginname/lang/en/local_yourpluginname.php
Then the file should contain
defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'Your plugin name';
$string['mycustomstring'] = 'A custom string';
Then get the string using
echo get_string('mycustomstring', 'local_yourpluginname');
By default you will need to have an en
folder with English strings.
But then you can also add additional languages using the iso code. For example to add Persian/Farsi the code is fa
. So create an additional folder:
/local/yourpluginname/lang/fa/local_yourpluginname.php
Then in the file add:
defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'نام افزونه شما';
$string['mycustomstring'] = 'رشته های سفارشی';
You will also need to install the Farsi language pack. Go to site admin -> language packs or simply go direct to yourmoodlesite.com/admin/tool/langimport/index.php
and select Farsi.
If you want to translate the existing Moodle strings, then it might be better to translate them via AMOS. Then everyone will benefit from the translations. https://docs.moodle.org/dev/Contributing_a_translation
Upvotes: 1