A Template for Multilingual UDT's (User Defined Tags)

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm

A Template for Multilingual UDT's (User Defined Tags)

Post by Russ »

This is now in the Forge
http://dev.cmsmadesimple.org/projects/multilingualudt

This is the sister post to the "A Template for Multilingual Tags/Plugins" one.
http://forum.cmsmadesimple.org/index.php/topic,37799.0.html

This is a proposed template for multilingual UDT's or User Defined Tags.
Just create a UDT called:
'sfi_multilingual_udt_template'

and paste the code below into it.

Code: Select all

# sfi_multilingual_udt_template
# shoesforindustry.net
# Russ Baldwin
# This is a proposed skeleton template for a multilingual UDT.
# Version 0.1
# 
# Use on your page with:
# {sfi_multilingual_udt_template lang='en_US'} 
# setting the display language as appropriate
#
# If you do not add the 'lang' parameter:
# {sfi_multilingual_udt_template}
# Then the UDT will use the language set in Admin:
# 'Site Admin / Global Settings / Default language'
# If no language is set there, then the Tag uses the default (en_US)
#
# If the language specified is not in the UDT, then the UDT uses the default (en_US)
#
#
# Get the display language parameter, if any, else use the database default (case select statement below)
if (isset ($params['lang'])) {
//  If there is a language parameter, but it is not in the UDT list of languages, then use the default. If this is not set then use a default
$setLanguage = $params['lang'];
}
else { 
	//  If no language parameter is specified then try and use display / frontendlang from database with 
	global $gCms;
	$db = &$gCms->GetDb();
	$query ="SELECT ".cms_db_prefix()."siteprefs.sitepref_value FROM ".cms_db_prefix()."siteprefs WHERE sitepref_name = 'frontendlang'";
	$frontendLang= $db->GetOne($query);
		if (isset($frontendLang)) {
                        //OK lang is set in the database so use this for display language
			$setLanguage =$frontendLang;
			} else {
                        //NO lang is set in the database so use this a default (en_US)
			$frontendLang = "en_US";
			$setLanguage =$frontendLang;
			}
		
}
// End check for language parameters

// You can turn on a debug on which shows the set language - uncomment the line below
//$UDTDebug = "<h3>DEBUG</h3><p>Display language = ". $setLanguage. "</p>";

//Display strings for UDT (main) alter these as required by your program/language
switch($setLanguage)
// Add new case statements as desired for more languages
{
case "de_DE": //All strings used in UDT for text displayed on a site in German / Deutsch
$returnString="Dies ist die zurückgegebene Zeichenfolge aus dem sfi_multilingual_udt_template";
break;

case "es_ES": //All strings used in UDT for text displayed on a site in Spanish / Español
$returnString="Esta es la cadena de regresar de la sfi_multilingual_udt_template";
break;

case "fr_FR": //All strings used in UDT for text displayed on a site in French / Français
$returnString="Il s'agit de la chaîne retournée par la sfi_multilingual_udt_template";
break;

case "nl_NL": //All strings used in UDT for text displayed on a site in Dutch /Nederlandse
$returnString="Dit is de geretourneerde string uit de sfi_multilingual_udt_template";
break;

default: //(en-US) All strings used in UDT for text displayed as default
$setLanguage="en_US";
$returnString="This is the returned string from the sfi_multilingual_udt_template";
break;
}

// Get your parameters for the UDT... if any...

//Your  main UDT code probably goes here... this is just returning a string in specified language
$returnString='<p class="myClass">'.$returnString.'</p>';

//And return DEBUG infromation if set
if ((isset ($UDTDebug))) {
$returnString.=$UDTDebug;
}
#End of your code

#Return localised UDT information to your page
echo $returnString;
Use on your page with:
{sfi_multilingual_udt_template lang='en_US'}
setting the display language as appropriate

If you do not add the 'lang' parameter e.g.:
{sfi_multilingual_udt_template}
Then the UDT will use the language set in Admin:
'Site Admin / Global Settings / Default language'
If no language is set there, then the Tag uses the default (en_US)
If the language specified is not in the UDT, then the UDT uses the default (en_US)

I've not tried this with the MLE version of CMSMS, I will if I get time, but you should be OK if you specify a language.
(I'm not sure if MLE relies on the same database fields.)


There are extensive comments in the code.
Let me know if I can improve things!
Also let me know what you think and could you / would you use it?

Also I'd like to apologise in advance about the tag translations, I used Google Translation for it so it could be absolute rubbish! Sorry.

Russ
Last edited by Russ on Mon Oct 19, 2009 10:41 am, edited 1 time in total.
Post Reply

Return to “Developers Discussion”