Force a module to support multiple languages

Below some tactics on how to force a module to support multiple languages.
The examples in this article will show you how it can be achieved for the FEU/SelfRegstration modules but you can certainly use the same tactics with other modules (ex: the picture comments in the Gallery module).

First make sure you can detect the language of the page (outside of the scope of this article)
In this article, the language is inside $lang_parent and is either "nl" or "fr"...

Although this article was created for a 2 lang website, it's easy to change the regex used to 3 or more languages.

FEU text input example:


The following will be shown to the visitors of the website:


Because we used the hash tag to separate the languages, we can now easily create a regex to show only part of the prompt!

Inside the "Change Settings Template":
Place this at the top:

{if $lang_parent == 'nl'}
  {$replacegroup='$1'}
{else}
  {$replacegroup='$2'}
{/if}

Replace:

{$control->prompt}

With:

{if $control->prompt|strstr:'#'}
  {$control->prompt|regex_replace:"/(.*)#(.*)/":"{$replacegroup}"}
{else}
  {$control->prompt}
{/if}

After making these changes, the appropriate text will be show to the website visitors:
For nl:

For fr:


FEU dropdown example:



The following will be shown to the visitors of the website:


The hash tags at the start and the end of the options will allow us to use regex on the generated HTML tag (which contains all the options in one long string).

Inside the "Change Settings Template":
Place this at the top:

{if $lang_parent == 'nl'}
  {$replacegroup='$1'}
{else}
  {$replacegroup='$2'}
{/if}

Replace:

{$control->control}

With:

{if $control->control|strstr:'cms_dropdown' && $control->control|strstr:'#'}
  {$control->control|regex_replace:"/#(.*?)#(.*?)#/":"{$replacegroup}"}
{else}
  {$control->control}
{/if}

After making these changes, the appropriate text will be show to the website visitors:
For nl:

For fr:

Handling Errors:

We need to change the language strings that contain "%s" and where the prompt (ex: "Voornaam#Prénom") will be shown.
Why? Because we need to be able to regex it :)

Example using the SelfRegstiration module:
Create the module_custom/SelfRegstration/lang folder (more info)
Copy any language files you want to use to that folder.

For the language strings you want to change, add colons around the %s:

  $lang['error_requiredfield'] = 'Veld %s moet ingevuld zijn';
Becomes:
  $lang['error_requiredfield'] = 'Veld |%s| moet ingevuld zijn';

The following will be shown to the visitors of the website:
For nl:

For fr:


Inside the "Registration Template":
Place this at the top:

{if $lang_parent == 'nl'}
  {$error_replacegroup='$1"$2"$4'}
{else}
  {$error_replacegroup='$1"$3"$4'}
{/if}

Now replace:
<p><font color="red">{$message}</font></p>

With:

{if $message|strstr:'|'}
  {$new_error=$message|regex_replace:"/(.*)\|(.*)#(.*)\|(.*)/":"{$error_replacegroup}"}
{else}
  {$new_error=$message}
{/if}
<p class="error">{$new_error}</p>

After making these changes, the appropriate text will be show to the website visitors:
For nl:

For fr:

Done

I know this isn't the easy road but at least it's a road :)

Greetings,
Manuel

ALL-INKL.COM - Webhosting Server Hosting Domain Provider

Comments