CodeIgniter Forums
Multilanguage data in database in one column - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Multilanguage data in database in one column (/showthread.php?tid=55192)



Multilanguage data in database in one column - El Forum - 10-14-2012

[eluser]Unknown[/eluser]
Just created library for working with multi language string.

Inspiration come from qtranslate and how they store multilanguage data in database.

Please want to here from you why is this stupid and you could do this and thata.

Feel free to use it in your projects.

1. Strparse
- Find string in language coded string for given language
- Create one line of language coded string

2. Colparse(use Strparse)
- Create array from language coded string
- Create language coded string from array
- Update value in language coded string for given language

3. Translate(use Colparse and Strparse)
- Translate all fields from associative array
- Translate all fields from result array ( array of associative array )
- Parse all fields from associative array

Code:
// Class Strparse
<?php
    class Strparse
    {

        /**
        * Create one line of language coded string
        * @param str
        * @param lang
        */
        function create_lang_string($str, $lang)
        {
            return '<!--:'.$lang.'-->'.$str.'<!--:-->';
        }

        /**
        * Find string in language coded string for given language
        * @param str
        * @param lang
        */
        function find_lang_in_string($str, $lang)
        {
            $start = '<!--:'.$lang.'-->';
            $end = '<!--:-->';

            $str = " ".$str;
            $ini = strpos($str,$start);
            if ($ini == 0) return "";
            $ini += strlen($start);
            $len = strpos($str,$end,$ini) - $ini;
            return substr($str,$ini,$len);
        }
    }
?>

// Class Colparse
<?php
    class Colparse
    {
        private $languages;
        private $CI;

        public function __construct()
        {
            $this->CI = &get;_instance();
            $this->CI->load->library("strparse");

            // Get languages from config  
            foreach(config_item("languages") as $key=>$value):        
                $this->languages[] =  $key;
                endforeach;
        }

        /**
        *
        * @param str_to_update
        * @param cur_str
        * @param lang
        */
        function upate_colum($str_to_update, $cur_str, $lang)
        {
            // Create array from language coded string
            $titles = $this->col_to_array($str_to_update);

            // Update array with new string
            if($titles=="No matches") $titles = array();
            $titles[$lang] = $cur_str;

            // Return language coded string
            return $this->array_to_col($titles);
        }

        /**
        * Create array from language coded string
        * @param string
        */
        function col_to_array($string)
        {
            $titles = array();
            foreach($this->languages as $lang):

                // Find string in language coded string for given language
                $str = $this->CI->strparse->find_lang_in_string($string,$lang);
                if($str!=""){
                    $titles[$lang] = $str;
                }
                endforeach;

            // return if empty array
            if(count($titles)==0)return "No matches";

            // return titles as array
            return $titles;
        }

        /**
        * Create language coded string from array('me'=>value,'en'=value...)
        * @param arr
        */
        function asoc_array_to_col($arr)
        {
            $string = "";
            foreach($this->languages as $lang):

                // Create one line of language coded string
                if(isset($arr[$lang])) $string.=$this->CI->strparse->create_lang_string($arr[$lang],$lang);
                endforeach;

            // Return language coded string
            return $string;
        }

        /**
        * Create language coded string from regular array(value,value,value)
        * from post array and langauages are paralel
        * @param arr
        */
        function regular_array_to_col($arr)
        {
            $cnt = count($arr);
            $string = "";

            for($i=0;$i<$cnt;$i++):
                // Create one line of language coded string
                $string.=$this->CI->strparse->create_lang_string($arr[$i],$this->languages[$i]);
                endfor;

            // Return language coded string
            return $string;
        }
    }
?&gt;

// Class Translate
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Translate
    {

        public function __construct()
        {
            $this->CI = &get;_instance();
            $this->CI->load->library("strparse");
            $this->CI->load->library("colparse");
        }

        /**
        *
        * @param arr
        * @param lang
        */
        function asoc_array($arr, $lang)
        {
            foreach ($arr as $key=>$value):
                if(substr($value,0,4)=='&lt;!--'):
                    $arr[$key] = $this->CI->strparse->find_lang_in_string($arr[$key],$lang);
                    endif;
                endforeach;

            return $arr;
        }

        /**
        *
        * @param arr
        * @param lang
        */
        function res_array($arr, $lang)
        {
            foreach ($arr as $key=>$value):
                $arr[$key] = $this->asoc_array($value,$lang);
                endforeach;

            return $arr;
        }

        /**
        *
        * @param arr
        */
        function parse_asoc($post)
        {

            foreach ($post as $key=>$value):
                if(is_array($value))
                    $post[$key] = $this->CI->colparse->regular_array_to_col($value);

                endforeach;

            return $post;
        }

    }
?&gt;

// Config languages file look like this
&lt;?php

    $config['languages']    = array(
        'en' => 'English',
        'me' => 'Crnogorski',
        'ru' => 'Rусский',
        'de' => 'Deutsch'
    );