Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
Hi,

How can I use the multilang plugin to make simple translation separations inside PHP code such as:

<cms:if k_lang='en'><?php $title="M-ODU powered by Emerge "; ?></cms:if>
<cms:if k_lang='pt'><?php $title="M-ODU promovido por Emerge"; ?></cms:if>

This doesn't seem to work, it always gets me the 'pt' result for some reason!
Does this even make sense? Can I use translations inside <php> code?

Thanks!
Hi,

The code snippet you posted does not fully show how exactly you are using PHP to output the translated strings.
Please post a more comprehensive one that illustrates the mentioned point for us to suggest a solution.

In the meanwhile, perhaps you might find the following threads on using native PHP with Couch code to be helpful? -
viewtopic.php?f=2&t=10763#p27290
viewtopic.php?f=2&t=12977
Awww cool <cms:php> tag might be the solution!

A more precise example can be a snippet of a PHP contact form code I'm using on the same website. It prints feedback if certain fields are filled by the user. For example, this one prints at $errTel if the field is empty or wrongly filled:

Code: Select all
if (empty($_POST['telephone'])) {
      $errTel = 'Please enter your telephone number';
   } elseif (!preg_match('/^[0-9\+]{5,15}$/', $_POST['telephone'])) {
      $errTel = 'Please enter a valid telephone number';
}


How do I translate this fields? If it's english it prints:
$errTel = 'Please enter your telephone number';

and if it's portuguese:
$errTel = 'Por favor insira o seu contacto de telefone';
I guess this shall help:

Code: Select all
<cms:php>
    global $errTel;
    if (empty($_POST['telephone'])) {
          <cms:if k_lang='en'>
              $errTel = 'Please enter your telephone number';
          <cms:else_if k_lang='pt'>
              $errTel = 'Por favor insira o seu contacto de telefone';
          </cms:if>
       } elseif (!preg_match('/^[0-9\+]{5,15}$/', $_POST['telephone'])) {
          <cms:if k_lang='en'>
              $errTel = 'Please enter a valid telephone number';
          <cms:else_if k_lang='pt'>
              $errTel = 'Por favor, coloque um numero de telefone válido';
          </cms:if>
    }
</cms:php>


And wherever you want to display the $errTel variable you can do:
Code: Select all
<cms:php>
    global $errTel;
    echo $errTel;
</cms:php>


For example, if you are using bootstrap alert in which you want to display the output you can do:
Code: Select all
<div class="alert alert-danger" role="alert">
  <cms:php>global $errTel; echo $errTel;</cms:php>
</div>


Regards,
GXCPL (CTR)
Image
where innovation meets technology
4 posts Page 1 of 1