ServWise.com Posted May 13, 2009 Share Posted May 13, 2009 I noticed that WHMCS is lacking in the ability to add product titles and descriptions in multiple languages and although there is a mod solution already published http://forum.whmcs.com/showthread.php?t=15045&highlight=products+multiple+languages it is not the most elegant or flexible solution so I decided to create something better. This solution uses Smarties own plug-in functionality to allows you to easily add multilingual content to any field in the admin. It works by surrounding the content in simple xml tags e.g. <English>English content</English> <French>French content</French> First thing you need to do is create a plug-in file in the following folder /whmcs/includes/smarty/plugins/function.getlang.php and add the following code to the file <?php /* * Smarty plugin * ————————————————————- * File: function.getlang.php * Type: function * Name: XML Language Data * Purpose: Returns only the language data surrounded by language tags * ————————————————————- */ function smarty_function_getlang($params, &$smarty){ $xmldata ='<?xml version="1.0" encoding="ISO-8859-1" ?><root>'.$params['data'].'</root>'; $lang = $params['lang']; if(simplexml_load_string($xmldata)===FALSE) { return $params['data']; } else { $langxmlobj = new SimpleXMLElement($xmldata); if ($langxmlobj->$lang){ return $langxmlobj->$lang; } else { return $params['data']; } } } ?> Next for any smarty variable that outputs data entered into the admin E.g. the $product.name or $product.description tag in your orderforms you can replace it with the tag below (Note the script will output the full value if no tags are found) {getlang data=$product.name lang=$language} OR {getlang data=$product.description lang=$language} It is then just a simple job or adding the content in the admin with the correct names based on your sites language. <English>English content</English><French>French content</French If anyone finds any bugs then please post them here 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 13, 2009 Author Share Posted May 13, 2009 <English>English content</English><French>French content</French should be <English>English content</English><French>French content</French> 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 13, 2009 Share Posted May 13, 2009 It's VERY VERY VERY NICE. Just one question, what is happen if you have only two translations (like example) and someone change language to Portuguese, for example, and you don't have a <portuguese> tag. Is it possible to set a 'default' language for when you don't have one translated? This REALLY will solve many of my problems in all fields that don't support translations and I really suggest add this to WHMCS. One example is the security question, I need to setup questions in different languages, not only my own language. 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 13, 2009 Author Share Posted May 13, 2009 At the moment if it does not find a tag it will simply output the whole data but it would be easy to add this functionality to the code. Here is a version which you can set a default language to be used. (Default language needs to be set in $deflang variable) <?php /* * Smarty plugin * ————————————————————- * File: function.getlang.php * Type: function * Name: XML Language Data * Purpose: Returns only the language data surrounded by language tags * ————————————————————- */ function smarty_function_getlang($params, &$smarty){ $deflang ="English"; $xmldata ='<?xml version="1.0" encoding="ISO-8859-1" ?><root>'.$params['data'].'</root>'; $lang = $params['lang']; if(simplexml_load_string($xmldata)===FALSE) { return $params['data']; } else { $langxmlobj = new SimpleXMLElement($xmldata); if ($langxmlobj->$lang){ return $langxmlobj->$lang; } elseif ($langxmlobj->$deflang){ return $langxmlobj->$deflang; } else { return $params['data']; } } } ?> 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 13, 2009 Share Posted May 13, 2009 This is a really fast solution to solve problems in fields that need translations. Matt, this can be added in version 4.0.1 by default... Just look it. 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 13, 2009 Share Posted May 13, 2009 Is it possible to use it in knowledgebase, categories, downloads, articles, announcements, etc.? 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 13, 2009 Author Share Posted May 13, 2009 Is it possible to use it in knowledgebase, categories, downloads, articles, announcements, etc.? I don't see why not although I have not tested it. It really depends on if they output the knowledge base page as one variable. Also where you have html code in the content you would probably need to wrap it in <![CDATA[ elements. e.g. <English><![CDATA[ html content ]]></English> 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 13, 2009 Author Share Posted May 13, 2009 Oh dear, I just tried it in an announcement and unfortunately the WYSIWYG editor strips the XML tags, this may be a configurable option for the editor but it's not going to work out of the box. Ill see if I can work out what is happening. 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 13, 2009 Author Share Posted May 13, 2009 Nope it does not look like it will work at all with the WYSIWYG editor, adding the language tags to the tinymce config to allow them works but because tinymce also adds lots of other surrounding tags like <p> etc it will be invalid XML and so wont work. There does not seem to be a way to turn the WYSIWYG editor off either. real shame as it would have been a good intermediate solutions until WHMCS adds proper multilingual capability to the system. At it's simplest WHMCS could just add additional editors to the admin (1 for each language) and combine all the output data into an XML file which is saved to the db as a single article and use my script to pull the right language data from the XML at display time. This has a great advantage in that nothing needs to change in the database (No need for separate DB entries for each language) and is totally backward compatible with the current system. The same can be said for any other db text that needs to be multilingual. 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 13, 2009 Share Posted May 13, 2009 I would like to know what do you think about it... 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 13, 2009 Author Share Posted May 13, 2009 Actually it surprises me that WHMCS don't already use XML for storing items that need separate language data, it's a perfect solution that negates the need to have multiple records for each item and is highly flexible and fast.. I just checked the email templates db table (The only thing that is properly multi-language in WHMCS admin) and each language email has a separate record. 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 14, 2009 Share Posted May 14, 2009 What do you thing in request as feature? Any whmcs staff is reading this post? Can comment about it? 0 Quote Link to comment Share on other sites More sharing options...
ljesh Posted May 14, 2009 Share Posted May 14, 2009 (edited) It works. ServWise, if you have a moneybookers account, PLEASE send me a PM with your e-mail address and allow me to send you a small donation for spending your time making this wonderful mod as a small appreciation for making my life easier. WHMCS is probably the MOST used billing system in the webhosting industry. I never understood why they don't allow EVERYTHING to be translatable in other languages. Thank you, I mean alot. One question... Does this works for knowledge base categories and articles? If yes, please someone write instructions as detailed as possible, please. Edited May 14, 2009 by ljesh 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 14, 2009 Author Share Posted May 14, 2009 It works.ServWise, if you have a moneybookers account, PLEASE send me a PM with your e-mail address and allow me to send you a small donation for spending your time making this wonderful mod as a small appreciation for making my life easier. WHMCS is probably the MOST used billing system in the webhosting industry. I never understood why they don't allow EVERYTHING to be translatable in other languages. Thank you, I mean alot. money[AT]servwise.com, no obligation though.. One question... Does this works for knowledge base categories and articles? If yes, please someone write instructions as detailed as possible, please. Unfortunately no not at the moment as the WYSIWYG editor messes up the XML tags, I'm looking at a way to get round it but we'll see. 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 14, 2009 Author Share Posted May 14, 2009 Here is an updated script and some instructions for allowing multi-language content in WYSIWYG editor fields. Please note this is somewhat of a hack and you need to be careful exactly how you structure the page to make sure no stray html code gets trapped between tags (p tags surrounding the tags are okay and in fact have to be there for the sudo tag conversion to work ) In the WYSIWYG editor you can place the sudo codes surrounding your content like so [##English#] This is english [#English##] [##Italian#] This is Italian [#Italian##] The html should come out something like this <p>[##English#]</p> <p>This is english</p> <p>[#English##]</p> <p>[##Italian#]</p> <p>This is Italian</p> <p>[#Italian##]</p> Please make sure that no additional p tags (Apart from the ones above which are catered for) fall outside of the surrounding tags or likely the conversion will not work and the whole string will be outputted instead. The modified script to is below, enjoy <?php /* * Smarty plugin * ————————————————————- * File: function.getlang.php * Type: function * Name: XML Language Data * Purpose: Returns only the language data surrounded by language tags * ————————————————————- */ function smarty_function_getlang($params, &$smarty){ // Set a default language $deflang ="English"; // Convert sudocode from wysiwyg editor $data = str_replace("<p>[##","<",$params['data']); $data = str_replace("<p>[#","]]></",$data); $data = str_replace("##]</p>",">",$data); $data = str_replace("#]</p>","><![CDATA[",$data); $data = str_replace("##][##","><",$data); // process data $xmldata ='<?xml version="1.0" encoding="ISO-8859-1" ?><root>'.$data.'</root>'; $lang = $params['lang']; if(simplexml_load_string($xmldata)===FALSE) { return $params['data']; } else { $langxmlobj = new SimpleXMLElement($xmldata); if ($langxmlobj->$lang){ return $langxmlobj->$lang; } elseif ($langxmlobj->$deflang){ return $langxmlobj->$deflang; } else { return $params['data']; } } } ?> 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 14, 2009 Author Share Posted May 14, 2009 To be honest I'm not at all happy with the WYSIWYG code hack above, There are too many issues, announcements have RSS which AFAICS cant be edited in a template so always outputs this full html including sudo tags (Not good). Ideally this would be far far better if WHMCS implemented it directly into the system with multiple WYSIWYG editors (Like they have for email templates) and as described in other posts above. Guess we will wait and see. 0 Quote Link to comment Share on other sites More sharing options...
ljesh Posted May 14, 2009 Share Posted May 14, 2009 I will stay using the "version" you published in the first post. I would love EVERYTHING to be translatable in many different languages (we do business in 3 languages, equally important to us and our clients) but at least our WHMCS looks much nicer with product description in our order form. Can anyone help me translate the group name? I cannot do that one. 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 14, 2009 Author Share Posted May 14, 2009 For Ljesh, Just tried myself to translate the category descriptions but for some reason WHMCS html encode the output so I have to make a small modification to the script. Latest version decodes the html entities to allow the xml to be valid. <?php /* * Smarty plugin * ————————————————————- * File: function.getlang.php * Type: function * Name: XML Language Data * Purpose: Returns only the language data surrounded by language tags * ————————————————————- */ function smarty_function_getlang($params, &$smarty){ // Set a default language $deflang ="English"; // Decode encoded html $data = html_entity_decode($params['data']); // Convert sudocode from wysiwyg editor $data = str_replace("<p>[##","<",$data); $data = str_replace("<p>[#","]]></",$data); $data = str_replace("##]</p>",">",$data); $data = str_replace("#]</p>","><![CDATA[",$data); $data = str_replace("##][##","><",$data); // process data $xmldata ='<?xml version="1.0" encoding="ISO-8859-1" ?><root>'.$data.'</root>'; $lang = $params['lang']; if(simplexml_load_string($xmldata)===FALSE) { return $params['data']; } else { $langxmlobj = new SimpleXMLElement($xmldata); if ($langxmlobj->$lang){ return $langxmlobj->$lang; } elseif ($langxmlobj->$deflang){ return $langxmlobj->$deflang; } else { return $params['data']; } } } ?> 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 14, 2009 Share Posted May 14, 2009 Every new post I see script better and still asking if someone from WHMCS staff are reading this post. This can be added as default in whmcs. 0 Quote Link to comment Share on other sites More sharing options...
ljesh Posted May 14, 2009 Share Posted May 14, 2009 Thanks again ServWise But, I am planning to remove all the changes and restore to before I installed this mod. I am finding rather difficult to trace all of the changes I need to make in my template Maybe if you have 1-2 terms it's ok, but filling all in this way is probably not going to work because 1. it's very hard to locate and change all of the terms that do not use language file 2. you are eventually going to loose all of your work because of some of the next versions. I wish WHMCS team integrated this by default and make WHMCS a real multi-language platform. 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 15, 2009 Share Posted May 15, 2009 Me too. I wish WHMCS team integrated this by default and make WHMCS a real multi-language platform. But you can use another addon (checksum) to help you in WHMCS upgrades. It's what I use for. 0 Quote Link to comment Share on other sites More sharing options...
abednegoo Posted May 18, 2009 Share Posted May 18, 2009 wonderful mod, this solved all my issues with the old tweak! 0 Quote Link to comment Share on other sites More sharing options...
abednegoo Posted May 18, 2009 Share Posted May 18, 2009 $data = str_replace("[##","<",$data); $data = str_replace("[#","]]></",$data); $data = str_replace("<p>[##","<",$data); $data = str_replace("<p>[#","]]></",$data); $data = str_replace("##]",">",$data); $data = str_replace("##]</p>",">",$data); $data = str_replace("#]","><![CDATA[",$data); $data = str_replace("#]</p>","><![CDATA[",$data); $data = str_replace("##][##","><",$data); fix for non wywiwyg strings. You forgot the regular [##, [#, #] and ##] tags... 0 Quote Link to comment Share on other sites More sharing options...
jozeph Posted May 18, 2009 Share Posted May 18, 2009 Thanks for fix. 0 Quote Link to comment Share on other sites More sharing options...
ServWise.com Posted May 18, 2009 Author Share Posted May 18, 2009 $data = str_replace("[##","<",$data); $data = str_replace("[#","]]></",$data); $data = str_replace("<p>[##","<",$data); $data = str_replace("<p>[#","]]></",$data); $data = str_replace("##]",">",$data); $data = str_replace("##]</p>",">",$data); $data = str_replace("#]","><![CDATA[",$data); $data = str_replace("#]</p>","><![CDATA[",$data); $data = str_replace("##][##","><",$data); fix for non wywiwyg strings. You forgot the regular [##, [#, #] and ##] tags... For non wysiwyg strings you should just use the standard <english> and </english>, The codes above were meant to be a work around for the wysiwyg editor which wont allow <> other than when in basic html usage. However your fix allows them to be used in both I guess so is valid in any case. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.