FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
Info
FORMfields & FORMgen :: Languages

Dynamically Translating the Language of Your Form

(2289 views. Last Updated: 2007-10-26 7:48 PM)

Here are some examples of how to dynamically translate the language of your form.

The following examples will use the translations at FORMfields/src/translations.csv. It is necessary for you to fill in any columns that will be needed by your program. For example, if you are looking to translate the FORMfields error message of "cannot be blank" to Spanish, then you will have to open translations.csv in an spreadsheet editor like MS Excel and enter "no pueden estar en blanco" in the Spanish column. Futhermore, if the translation contains special characters like "cannot be blank" in Chinese, which is "空白列", then you will need to enter the NCR (Numeric Character Reference), "空白列" (This is because the functions that read translations.csv are not yet UTF-8 compliant). Also, you can add any number of other languages to the file just by adding another column heading. For example, you could add Thai just by adding a column that has "Thai" in the first row. Note: if a column doesn't have a translation for a given word, it will automatically output the English. Note: translations ARE case sensitive.

Example 1: Globally Setting the Current Language
You can globally set the current language so that all your forms are translated from the native language, which is defaulted to English as FORMfields is written in English. Here is an example of how to set the current language to Spanish. This is done by editing FORMfields/src/FfGlobals.php and replacing:
$GLOBALS["FF_GLOBALS_CURRENT_LANGUAGE"] = "English";
with:
//$GLOBALS["FF_GLOBALS_CURRENT_LANGUAGE"] = "English";
$GLOBALS["FF_GLOBALS_CURRENT_LANGUAGE"] = "Spanish";
Then if you edited the translations.csv file as mentioned above by addeding "no pueden estar en blanco" for the Spanish translation of "cannot be blank", all FORMfields forms will print "no pueden estar en blanco" instead of "cannot be blank."

Example 2: Form-Wise Setting of the Current Language
You can set the current language on a per form basis. Here is an example of how to set the current language to Spanish. Edit your form and add:
$lh = &FfLH::s();
$lh->setLanguage("Spanish");
after the require_once() statement at the top of your form that includes FORMfields.php. Then if you edited the translations.csv file as mentioned above by adding "no pueden estar en blanco" for the Spanish translation of "cannot be blank", this form will now print "no pueden estar en blanco" instead of "cannot be blank."

Example 3: Dynamically Translating Any Text
You can use FORMfields to dynamically translate any text. If you used either of the methods in Example 1 or 2 above to set the current language to Spanish, then you can dynamically translate "cannot be blank" into "no pueden estar en blanco" by using something like:
echo FfLH::t("cannot be blank");
Moreover, you could use any other translation in the translations.csv file or you could add another row with a completely new translation. Please recall that if you translation.csv file didn't have a translation for say "I HAVE RED HAIR" then:
echo FfLH::t("I HAVE RED HAIR");
would simply output "I HAVE RED HAIR".
Info
Support | Earn Money
Copyright © 2005-2020 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.