Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi Language
#1
Hello,
I'm Daniele.
I'm building a simple semi-static website that should be multilingual.
I have seen a couple of posts about multilanguage support in GetSimple, but nobody has sorted out. I've seen many great ideas and I found this one http://get-simple.info/forum/viewtopic.php?id=510 perfect to accomplish my goal. So I used the customField plugin and I modified some of the admin pages of GS in order to get the multilanguage support.

ATTENTION! I'm a web designer not a PHP developer: my code could look crappy.

Instruction:
Download and Install CustomField plugin. Create a file /data/other/customfields.xml which should be in the following format:
Code:
<channel>
  <item>
    <desc>language</desc>
    <label>Language </label>
    <type>dropdown</type>
    <option>it</option>
    <option>de</option>
    <option>fr</option>
    <option>es</option>
  </item>
</channel>

Modify plugin/customfields.php. You need to add this line:
Code:
function createTagInputs(){    
     global $tags;
     global $data_edit;
     $uri           = @$_GET['id'];
     $language      = @$_GET['language']; //Add this line
     $path = GSDATAPAGESPATH;

and replace with these others:
Code:
case "dropdown":    
     echo "<td>";
     echo "<b>".$tags[$key]['label'].":</b><br />";
     echo "<select id=\"post-".strtolower($key)."\" name=\"post-".strtolower($key)."\" class='".$class."'>";
     echo "<option value='".$data_edit->$key."'>".$data_edit->$key."</option>";
     foreach ($tags[$key]['options'] as $option) {
       echo "<option value='".$option."'>".$option."</option>";
     }
     echo "</select>";
     echo "</td>";
     break;
with:
Code:
case "dropdown":     echo "<td>";
     echo "<b>".$tags[$key]['label'].":</b><br />";
     echo "<select id=\"post-".strtolower($key)."\" name=\"post-".strtolower($key)."\" class='".$class."'>";
     if ($data_edit->$key != '' ) {
         $thelanguage = $data_edit->$key;
     } else {
         $thelanguage = $language;
     }
     foreach ($tags[$key]['options'] as $option) {
         echo "<option ";
         if (strtolower($thelanguage) == strtolower($option)) { echo " SELECTED "; }
              echo "value='".$option."'>".$option."</option>";
     }
     echo "</select>";
     echo "</td>";
     break;
Change admin/pages.php adding this line:
Code:
$id           = @$_GET['id'];
$ptype           = @$_GET['type'];
$language      = @$_GET['language']; //Add this line

Now you have to change:
Code:
$table .= '<td>'. @$dash .'<a title="'.$i18n['EDITPAGE_TITLE'].': '. cl($page['title']) .'" href="edit.php?id='. $page['url'] .'" >'. cl($page['title']) .'</a><span class="showstatus toggle" >'. $homepage . $page['menuStatus'] . $page['private'] .'</span></td>'
with

Code:
$table .= '<td>'. @$dash .'<a title="'.$i18n['EDITPAGE_TITLE'].': '. cl($page['title']) .'" href="edit.php?id='. $page['url'] .'&language=' . $language . '" >'. cl($page['title']) .'</a><span class="showstatus toggle" >'. $homepage . $page['menuStatus'] . $page['private'] .'</span></td>';

and
Code:
if (count($filenames) != 0) {      foreach ($filenames as $file) {
          if (isFile($file, $path, 'xml')) {
               $data = getXML($path .$file);
               $status = $data->menuStatus;
               //$pagesArray[$count]['title'] = $data->title;
               $pagesArray[$count]['title'] = html_entity_decode($data->title, ENT_QUOTES, 'UTF-8');
               $pagesArray[$count]['parent'] = $data->parent;
               $pagesArray[$count]['menuStatus'] = $data->menuStatus;
               $pagesArray[$count]['private'] = $data->private;
               if ($data->parent != '') {
                    $parentdata = getXML($path . $data->parent .'.xml');
                    $parentTitle = $parentdata->title;
                    $pagesArray[$count]['sort'] = $parentTitle .' '. $data->title;
               } else {
                    $pagesArray[$count]['sort'] = $data->title;
               }
               $pagesArray[$count]['url'] = $data->url;
               $pagesArray[$count]['date'] = $data->pubDate;
               $parentTitle = '';
               $count++;
          }
     }
}

to

Code:
if (count($filenames) != 0) {
     foreach ($filenames as $file) {
          if (isFile($file, $path, 'xml')) {
               $data = getXML($path .$file);
               //$status = $data->menuStatus;
               //$pagesArray[$count]['title'] = $data->title;
               if (strtolower($data->language) == strtolower($language) OR $language == '') {
                    $pagesArray[$count]['title'] = html_entity_decode($data->title, ENT_QUOTES, 'UTF-8');
                    $pagesArray[$count]['parent'] = $data->parent;
                    $pagesArray[$count]['menuStatus'] = $data->menuStatus;
                    $pagesArray[$count]['private'] = $data->private;
                    if ($data->parent != '') {
                         $parentdata = getXML($path . $data->parent .'.xml');
                         $parentTitle = $parentdata->title;
                         $pagesArray[$count]['sort'] = $parentTitle .' '. $data->title;
                    } else {
                         $pagesArray[$count]['sort'] = $data->title;
                    }
                    $pagesArray[$count]['url'] = $data->url;
                    $pagesArray[$count]['date'] = $data->pubDate;
                    $parentTitle = '';
                    $count++;
               }
          }
     }
}
admin/edit.php. Simply add this line
Code:
// Get passed variables
$uri           = @$_GET['uri'];
$id           = @$_GET['id'];
$ptype           = @$_GET['type'];
$language      = @$_GET['language']; //Add this line
$path           = GSDATAPAGESPATH;
and replace
Code:
if ($tmpData->parent == '')
               {
                    $parents_list .= '<option '.@$sel.' value="'.$goodname.'" >'.$goodname.'</option>';
               }
with
Code:
if ($tmpData->parent == '' AND strtolower($tmpData->language) == strtolower($language))
               {
                    $parents_list .= '<option '.@$sel.' value="'.$goodname.'" >'.$goodname.'</option>';
               }
admin/template/sidebar-pages.php: Add the following lines on top of the page:
Code:
<?php
     $language      = @$_GET['language'];
     if (!isset($language))
          $thelanguage = '';
     else
          $thelanguage = '?language='.$language;
?>
then replace
Code:
<li><a href="pages.php" accesskey="p" <?php check_menu('pages');  ?>><?php echo $i18n['SIDE_VIEW_PAGES']; ?></a></li>
<li><a href="edit.php" accesskey="c" <?php if((@$_GET['id'] == "") && (get_filename_id()==='edit'))  { echo 'class="current"'; } ?>><?php echo $i18n['SIDE_CREATE_NEW']; ?></a></li>
with
Code:
<li><a href="pages.php<?php echo $thelanguage; ?>" accesskey="p" <?php check_menu('pages');  ?>><?php echo $i18n['SIDE_VIEW_PAGES'] . ' ' . strtoupper($language); ?></a></li>
     <li><a href="edit.php<?php echo $thelanguage; ?>" accesskey="c" <?php if((@$_GET['id'] == "") && (get_filename_id()==='edit'))  { echo 'class="current"'; } ?>><?php echo $i18n['SIDE_CREATE_NEW'] . ' ' . strtoupper($language); ?></a></li>
Last but not least admin/template/include-nav.php. Replace and add
Code:
<li><a class="pages" href="pages.php" accesskey="p" ><?php echo $i18n['TAB_PAGES'];?></a></li>
with
Code:
<li><a class="pages" href="pages.php?language=it" accesskey="p" ><?php echo $i18n['TAB_PAGES'].' IT';?></a></li>
     <li><a class="pages" href="pages.php?language=de" accesskey="p" ><?php echo $i18n['TAB_PAGES'].' DE';?></a></li>
     <li><a class="pages" href="pages.php?language=fr" accesskey="p" ><?php echo $i18n['TAB_PAGES'].' FR';?></a></li>
     <li><a class="pages" href="pages.php?language=es" accesskey="p" ><?php echo $i18n['TAB_PAGES'].' ES';?></a></li>

Ok and that's the admin. Now add this new function to your template in order to get the menu with the right language:

Code:
function menu_data_reloaded($id = null,$xml=false) {
        $menu_extract = '';
        global $PRETTYURLS;
        global $SITEURL;
    global $tags;
    global $lang;
    
    if ($lang == '') $lang = 'it';
        
        $path = GSDATAPAGESPATH;
        $dir_handle = @opendir($path) or die("Unable to open $path");
        $filenames = array();
        while ($filename = readdir($dir_handle)) {
            $filenames[] = $filename;
        }
        
        $count="0";
        $pagesArray = array();
        if (count($filenames) != 0) {
            foreach ($filenames as $file) {
                if ($file == "." || $file == ".." || is_dir(GSDATAPAGESPATH . $file) || $file == ".htaccess"  ) {
                    // not a page data file
                } else {
            $data = getXML(GSDATAPAGESPATH . $file);
            if (strtolower($data->language) == strtolower($lang)) {
            if ($data->private != 'Y') {
                $pagesArray[$count]['menuStatus'] = $data->menuStatus;
                $pagesArray[$count]['menuOrder'] = $data->menuOrder;
                $pagesArray[$count]['menu'] = $data->menu;
                $pagesArray[$count]['parent'] = $data->parent;
                $pagesArray[$count]['title'] = $data->title;
                $pagesArray[$count]['url'] = $data->url;
                $pagesArray[$count]['private'] = $data->private;
                $pagesArray[$count]['pubDate'] = $data->pubDate;
                $pagesArray[$count]['language'] = $data->language;
                $count++;
            }
            }
                }
            }
        }
        
        $pagesSorted = subval_sort($pagesArray,'menuOrder');
        if (count($pagesSorted) != 0) {
            $count = 0;
            if (!$xml){
            foreach ($pagesSorted as $page) {
                    $text = (string)$page['menu'];
                    $pri = (string)$page['menuOrder'];
                    $parent = (string)$page['parent'];
                    $title = (string)$page['title'];
                    $slug = (string)$page['url'];
                    $menuStatus = (string)$page['menuStatus'];
                    $private = (string)$page['private'];
            $pubDate = (string)$page['pubDate'];
            $language = (string)$page['language'];
                    
                    $url = find_url($slug,$parent);
                    
                    $specific = array("slug"=>$slug,"url"=>$url,"parent_slug"=>$parent,"title"=>$title,"menu_priority"=>$pri,"menu_text"=>$text,"menu_status"=>$menuStatus,"private"=>$private,"pub_date"=>$pubDate,"language"=>$language);
                    
                    if ($id == $slug) {
                        return $specific;
                        exit;
                    } else {
                        $menu_extract[] = $specific;
                    }
                
            }
            return $menu_extract;
            } else {
            $xml = '<?xml version="1.0" encoding="UTF-8"?><channel>';    
            foreach ($pagesSorted as $page) {
                    $text = $page['menu'];
                    $pri = $page['menuOrder'];
                    $parent = $page['parent'];
                    $title = $page['title'];
                    $slug = $page['url'];
                    $pubDate = $page['pubDate'];
                    $menuStatus = $page['menuStatus'];
                    $private = $page['private'];
            $language = $page['language'];
                      
                    $url = find_url($slug,$parent);
                    
                    $xml.="<item>";
                    $xml.="<slug><![CDATA[".$slug."]]></slug>";
                    $xml.="<pubDate><![CDATA[".$pubDate."]]></pubDate>";
                    $xml.="<url><![CDATA[".$url."]]></url>";
                    $xml.="<parent><![CDATA[".$parent."]]></parent>";
                    $xml.="<title><![CDATA[".$title."]]></title>";
                    $xml.="<menuOrder><![CDATA[".$pri."]]></menuOrder>";
                    $xml.="<menu><![CDATA[".$text."]]></menu>";
                    $xml.="<menuStatus><![CDATA[".$menuStatus."]]></menuStatus>";
                    $xml.="<private><![CDATA[".$private."]]></private>";
            $xml.="<language><![CDATA[".$language."]]></language>";
                    $xml.="</item>";
                    
            }
            $xml.="</channel>";
            return $xml;
            }
        }
        
        closedir($dir_handle);
    }

To test all your work simply insert these lines in your template:

Code:
<?php global $lang; $lang = 'de'; print_r(menu_data_reloaded()); ?>

Of course, you need to create the pages!



If you don't want to modify all these files, I attached them to the post.

Thank You, I love GS
Reply
#2
Well made solution!

I’m not too happy about all the code changes directly in the core, though. Wondering whether it would work as a plugin…
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#3
Hi, unfortunately, I do not work.
The administration panel works as expected, but I can not call commands in the template.
Can someone help me, and I care about.

Sorry, my English is not good.
Reply
#4
Hello!

This approach to multilingual content seems very promising. Thanks Daniele!
I can't code a single line of php, but I followed all steps and the backend looks good. Smile
However, I can't see the results on the site. My menu still shows all pages. I think I'm missing something.

Environment:
- Locally, XAMPP
- GS Version 2.02_RC1
- GS Installed on a subfolder

Any ideas?

Thanks in advance!
Reply
#5
I have also tried, but it doesn't show anything in my template.
Has someone an idea how and were i have to put
<?php global $lang; $lang = 'de'; print_r(menu_data_reloaded()); ?>
???
Reply
#6
I replace core function menu_data with code from menu_data_reloaded and add code that set lang, then go to fiunction that print menu items and add code to print that item if $lang == menu item lang

Also globals should be disable so try to store language in session

Karol.
Reply
#7
Could anyone build and share working distributive of GS with multi-language support. I've tried to make it myself but failed. Please, help, it's very necessary thing for me.
Reply
#8
karol Wrote:I replace core function menu_data with code from menu_data_reloaded and add code that set lang, then go to fiunction that print menu items and add code to print that item if $lang == menu item lang

Also globals should be disable so try to store language in session

Karol,
would you describe your corrections in detail, please?
freedom rulezzz! dependence suxxx...
Reply
#9
I have developed a new plugin that does not require any changes to the GetSimple core: http://get-simple.info/extend/plugin/i18n/69/.

Visit http://mvlcek.bplaced.net for more details.
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply




Users browsing this thread: 1 Guest(s)