language translation using array

12 posts by 3 authors in: Forums > CMS Builder
Last Post: November 4, 2013   (RSS)

By ht1080z - October 21, 2013 - edited: October 21, 2013

Hello,

I created a multi section with keyphrase, title_en, title_gr, title_hu fields and stored some strings for translation data, example:

keyphrase: login
title_en: Login
title_gr: Είσοδος
title_hu: Bejelentkezés

My selected language is stored in $_SESSION['language'] = "gr";
and i use to get my desired language from tables for example: $blog['title_'.$_SESSION['language']];

Is there any easy way to create a php translation function that search for translation in my array giving the key phrase and return with the translation of the used language?

<?php echo translation('login'); ?> and return with "Είσοδος"?

Please advise,
Karls

By ross - October 24, 2013

Hi Karls

Thanks for posting! I feel like I understand what you are looking for here and have an idea. If it doesn't take too much work to change, in your multi section have the fields like this:

keyphrase
en
gr
hu

Then, you can use the <?php echo translation('login'); ?> function. The code in your translation function will look something like this:

<?php

function translation($keyphrase) {

    $language = $_SESSION['language'];

    $translation = mysql_select("TABLE NAME", "keyphrase = '$keyphrase'");

    return $translation[$language]; 
}

?>

You will need to put the name of your keyphrase table where I have "TABLE NAME".You'll also need to be using CMS Builder version 2.08 or higher to use the mysql_select function.  

Does this make sense? Do you want to give it a shot and let me know how you make out? Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By ht1080z - October 24, 2013 - edited: October 25, 2013

Hello Ross,

Thank you for your help and your time on this.

I changed some things i the script you provide so i can make it work in my case:

function translation($keyphrase) {
    $language = $_SESSION['language'];
    $translation = mysql_select("language", "keystr = '$keyphrase'"); $trans = @$translation[0];
    return $trans['title_'.$language];
}

Its working exactly like i imagined!

I have one more question; my translation table in the database is for no more than 500 records.

What is faster for the server when calculate php and mysql:
- load the whole translation table at the head of every page and find the keyphrases than  return the translation in the memory or
- select every time from the translation table with mysql select (like in this solution)?

Thank you again,
Karls

By Dave - October 30, 2013

Hi Karlz, 

What is faster for the server when calculate php and mysql:
- load the whole translation table at the head of every page and find the keyphrases than  return the translation in the memory or
- select every time from the translation table with mysql select (like in this solution)?

You'd need to test it to know for sure but I'd try loading them all into memory.

Also, be sure to escape any input to mysql: 

    $translation = mysql_select("language", "keystr = '" .mysql_real_escape_string($keyphrase). "'");

Hope that helps! 

Dave Edis - Senior Developer
interactivetools.com

By Dave - October 31, 2013

HI Karlz, 

Can you give me some guidelines to modify this function to make it work from loaded array?

I assume first i need to load the translation table to array and then find with foreach...

You'll have to experiment to figure out exact code, but you here's a code snippet to load $records the first time a function is called and have it available after that without reloading.

// load $records the first time this function is called
static $records;
if (!isset($records)) { $records = mysql_select('your_table_name'); }  // 

The PHP keyword "static" basically means "remember the value of this variable the next time this function is called".  PHP Docs: http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static

So basically the first time the function is called it will load $records and then it will be available after that without needing to be reloaded.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By ht1080z - October 31, 2013

Hello Dave,

thank you for your help, that was exactly what i needed i think.

I came up with this and its working.

function translate($keyphrase) {
    static $transRecords;
    if(!isset($transRecords)) { $transRecords = mysql_select('language'); }
    $language = $_SESSION['language'];
    
    for($i=0; $i<sizeof($transRecords); $i++)
    {
        $row = @$transRecords[$i];
        if($row['keystr'] === $keyphrase )
            return $row['title_'.$language];       
    }
}


Please review the above script if you have more time on this.

Thank you again,
Karls

By Dave - November 1, 2013

Hi Karlz, 

Looks good!  If there's not too many language records and it's fast enough on the server I'd just leave it as is.  

If you want to make it faster you could try create an associative array of keystring values to records so you didn't need to loop over them every time.  PHP's associative array lookup code is likely a lot faster.  We actually have a utility function that does that called array_groupBy().  So your code might be something like this (untested): 

function translate($keyphrase) {
    static $transRecords;
    if(!isset($transRecords)) {
      $transRecords = mysql_select('language');
      $transRecordsByKeystr = array_groupBy($transRecords, 'keystr');
    }

    return @$transRecordsByKeystr[$keyphrase][ 'title_' . @$_SESSION['language'] ];
}


Hope that helps, let me know if that works.  

Dave Edis - Senior Developer
interactivetools.com

By ht1080z - November 3, 2013

Hi Dave,

Thank you for your reply.

I tried the suggested array_groupBy() solution to achieve better performance but it working only for the first call of the function, on every other calls in the same page, nothing returns.

Karls

By Dave - November 3, 2013

Hi Karlz,

Sorry, we'll need to make that new variable static too.  Try this:

static $transRecords;
static $transRecordsByKeystr;

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com