Different languages - small problem ??

10 posts by 2 authors in: Forums > CMS Builder
Last Post: July 14, 2015   (RSS)

By kovali - July 7, 2015

Hi there,

I'm building a website in 2 different languages, dutch and english.

For most of the content fields I can create a seperate record for both languages, like:

Projectdetails NL: <?php echo $record['projectdetails_nl'] ?>
Projectdetails UK: <?php echo $record['projectdetails_uk'] ?>

Next, I show the appropriate record for each language webpage


But I'm also using a record in CMS with dropdown menu, like:  Type: <?php echo $record['type'] ?>

Since the CMS is built in dutch language, the values in the dropdown menu are also listed in dutch, as:

eenvoudig
moeilijk
zeer moeilijk

Problem:  in the english language frontend of the website I end up with the dutch value using  <?php echo $record['type'] ?> 

Is there a way to show the translation of every possible value instead of the dutch (standard) values, so like:

easy
difficult
very difficult

Thx for helping me out pls !

By kovali - July 7, 2015

Thx Greg, I will give it a try... what if I would be using 4 languages ? What code should I use to explode the record into an array pls ?

By gregThomas - July 8, 2015 - edited: July 8, 2015

Hey Kovali,

You can extend your options to include as many languages as you want, here is how to do 3:

eenvoudig - easy - facilaj
moeilijk - difficult - malfacila
zeer moeilijk - very difficult - tre malfacila

Then on the front end of the site you could display the appropriate option like this:

//Explode the record type into an array, and add the first variable to dutchType, and the second to englishType
@list($dutchType, $englishType, $esperantoType) = explode(' - ', $record['type']);
if(/* -- code that's used to detect if dutch being used -- */){
  //Display dutch type  
  echo $dutchType;
if(/* -- code that's used to detect if esparanto being used -- */){
  //Display Espernanto type
    echo $esperantoType;
}else{
  //Display English type
  echo $englishType;
}

So the list function works by taking the items in an array, and adding each value in order to the variables you put in it. You can read more on it here:

http://php.net/manual/en/function.list.php

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By kovali - July 10, 2015

Hi Greg,

I've got it working for most parts of the CMS, but I also use some multi value checkboxes. Trouble is to join the values, for each language :

This code joins all the values, but shows all the variables in the array: <?php echo join(', ', $record['categorie:values']); ?> 

This code shows only the dutch variable, but doesn't join all possible values:    <div class="mix <?php @list($dutchCat, $frenchCat, $englishCat, $germanCat) = explode(' - ', $record['categorie']);  echo $dutchCat; ?>">

No good solution, I get an error:  <div class="mix <?php @list($dutchCat, $frenchCat, $englishCat, $germanCat) = explode(' - ', $record['categorie']); echo join(' ', '$dutchCat:values'); ?>">

Could you get me on the right track with this one please ? Thx !!

By gregThomas - July 10, 2015

Hey Kovali,

You need to cycle through each category option with a foreach loop, and then work out the language of each option. I've written some test code using a blog section I have in my test CMS:

  //Cycle through the blog posts...
  foreach($blogs as $blog){
    
    //Count how many blog category values we have.
    $len = count($blog['category:values']);

    //Cycle through the blog category values...
    foreach($blog['category:values'] as $key => $catVal){

      //Get the languages for each value.
      @list($englishType, $dutchType, $esperantoType) = explode(' - ', $catVal);
      
      if(/*Code to detect dutch*/){
        //Display dutch type  
        echo $dutchType;
      }elseif(/* code to detect esperanto*/){
        //Display Espernanto type
          echo $esperantoType;
      }else{
        //Display English type
        echo $englishType;
      }

      //If we're not dealing with the last element in the array, display a comma.
      if ($key != $len - 1) {
        echo ",";
      }
    }
    echo "<br>";
  }

I'm cycling through each blog category value, and then working out the language for each, then displaying the appropriate value. 

Let me know if you have any questions.

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By kovali - July 14, 2015

Sorry Greg, I get the idea but I'm afraid I don't quite understand all of the code you used.

I tried to transfer your code to my webpage, but don't get the desired result, yet:

<div id="Container" class="container">
<?php foreach ($projectenRecords as $record): ?>

<?php $len = count($record['categorie:values']); ?>

<?php foreach($record['categorie:values'] as $key => $catVal){

@list($dutchCat, $frenchCat, $englishCat, $germanCat) = explode(' - ', $catVal);

echo $dutchCat;

} ?>

<?php if ($key != $len - 1) {
echo ",";
} ?>


<div class="mix <?php @list($dutchCat, $frenchCat, $englishCat, $germanCat) = explode(' - ', $record['categorie']);
echo $dutchCat; ?>">

<?php foreach ($record['intro_foto'] as $index => $upload): ?>
<a href="/NL<?php echo $record['_link'] ?>"><img src="<?php echo htmlencode($upload['urlPath']) ?>" class="scalable" /></a>
<?php endforeach ?>


<div class="textwrap">
<h3><?php echo htmlencode($record['title']) ?></h3>
<p>Locatie: <?php echo htmlencode($record['locatie']) ?></p>
<p>Categorie: <?php @list($dutchCat, $frenchCat, $englishCat, $germanCat) = explode(' - ', $record['categorie']);
echo $dutchCat; ?></p>
<p>Status: <?php @list($dutchStat, $frenchStat, $englishStat, $germanStat) = explode(' - ', $record['status']);
echo $dutchStat; ?></p>
</div>
</div>
<?php endforeach ?>


<div class="gap"></div>
<div class="gap"></div>
</div>

By gregThomas - July 14, 2015

Hey Kovali,

I've created a basic integration below:

<div id="Container" class="container">
  <?php foreach ($projectenRecords as $record): ?>

    <?php
      $len = count($record['categorie:values']); 
      foreach($record['categorie:values'] as $key => $catVal){

        @list($dutchCat, $frenchCat, $englishCat, $germanCat) = explode(' - ', $catVal);

        echo $dutchCat;
        if ($key != $len - 1) {
          echo ",";
        }

      }
    ?>
  <?php endforeach ?>
</div>

Could you try replacing your current code with the code above and let me know if it works, and if it not could you tell me any errors you get? 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By kovali - July 14, 2015

No errors, but nothing is showing either...

By kovali - July 14, 2015

Found it !!   Thx Greg !