Have you ever wanted to add the new phrases into the .xml language files without having to add them manually into the .xml language files? If so, then this following trick is definitely for you. Yes, files word means that there are more than one .xml language files will be involved regarding that new phrases. The next question is then, how can I set the correct phrases for the correct language? Or, how can I set the correct phrases based on the current selected language?
This following trick will show you how you can set the new phrases based on the selected language in the web applications that generated by PHPMaker. You will learn about the flexibility of PHPMaker to set new phrases on the fly, so that we don't need to insert our new phrases into the english.xml or the other .xml language files (if your web application runs in the multi language). You will learn also how to get the selected language code using a global variable that provided by PHPMaker.
In addition, this following trick will also useful if you write your own extension and you have to define the new phrases from the extension side. You can set those new phrases anywhere in your code inside the extension, without having to write them in the Server Events side of PHPMaker.
[hidepost]
Simply use the following code in Page_Load server event:
global $Language; // don't forget to use a global Language object variable
global $gsLanguage; // a global variable to hold the selected language code
if ($gsLanguage == "id") { // if the selected language is Indonesian
$Language->setPhrase("MaximumRecordsPerPage", "Maksimum record per halaman.");
} else { // else if the selected language is English
$Language->setPhrase("MaximumRecordsPerPage", "Maximum records per page.");
}
// Now let's try to echo the new phrase ...
echo "The new phrase is: " . $Language->Phrase("MaximumRecordsPerPage");
As you can see from that code above, we are using $gsLanguage as a global variable to hold the selected language code, afterwards, let's check the selected language code, and then execute the proper code based on the selected language code. Also, we are using $Language as a global Language object variable, so that we can use setPhrase and Language that belong to the $Language object.
Also, as I mentioned earlier in the third paragraph above, even the example code above is used in Server Events, you can write the code also from the extension side, so that your extension users do not need to write any code to set the new phrases from the Server Events.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.