Joomla 1.011 and higher - Global meta data should be language-dependent

Locked
User avatar
eelziere
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Mon Aug 28, 2006 10:04 pm

Joomla 1.011 and higher - Global meta data should be language-dependent

Post by eelziere » Tue Oct 17, 2006 9:02 am

Hi All,

I am using Joomla 1.0.11. Most of my websites are handling several languages for their frontends (english, french, ...)
The problem is that in the "Meta Data" tab of the backend global configuration, you can specify description and keywords
only for a single language. Too bad... :( On the other hand, if you want to have specific meta data for a given content
item, this can be handled by the JoomFish component: you just need to translate the #__content.metadesc and the
#__content.metakey columns. The rest of this discussion will only deal with global meta data.

I think global meta data should be language-dependent: it is essential if you want your multi-languages based sites to be
properly referenced by search engines (G__gle and others).

I found a "quite" clean way to implement this feature. Unfortunatly, the following Joomla core files are impacted.
I do not pretend this IS the BEST implementation, but at least, it has been integrated into real sites, fully tested and
it is working fine! ;) Comments are warmly welcomed!

1. File configuration.php

a) You need to add a new variable called $mosConfig_MetaLang, which will contain the codes (separated with "|" characters)
   of the languages for which you want to support different meta data.

Example:

Code: Select all

<?php
$mosConfig_MetaLang = 'english|french';
?>
b) Then you need to store into the $mosConfig_MetaDesc and $mosConfig_MetaKeys variables as many values (separated with "|"
   characters and in the same order) as the number of language codes stored into the new $mosConfig_MetaLang variable.
   This step is optional, as the impacted version of the backend global configuration files will allow you to do so (see sections
   2. and below.)

Example:

Code: Select all

<?php
$mosConfig_MetaDesc = 'english description = bla bla bla|french description = bla bla bla';
$mosConfig_MetaKeys = 'english keywords = bla, bla, bla|french keywords = bla, bla, bla';
$mosConfig_MetaLang = 'english|french';
?>
2. File includes/joomla.php

In this file, we need to pass the meta language iso code as an additional argument to all the functions dealing with header
meta tags generation.

a) The addMetaTag() function is modified as follow:

Code: Select all

<?php
    function addMetaTag( $name, $content, $prepend='', $append='', $lang='' ) {  // HERE
        $name = trim( htmlspecialchars( $name ) );
        $content = trim( htmlspecialchars( $content ) );
        $prepend = trim( $prepend );
        $append = trim( $append );
        $lang = trim( $lang );  // HERE
        $this->_head['meta'][] = array( $name, $content, $prepend, $append, $lang );  // HERE
    }
?>
b) The appendMetaTag() function is modified as follow:

Code: Select all

<?php
    function appendMetaTag( $name, $content, $lang='' ) {  // HERE
        $name = trim( htmlspecialchars( $name ) );
        $n = count( $this->_head['meta'] );
        for ($i = 0; $i < $n; $i++) {
            if ($this->_head['meta'][$i][0] == $name) {
                $content = trim( htmlspecialchars( $content ) );
                if ( $content ) {
                    if ( !$this->_head['meta'][$i][1] ) {
                        $this->_head['meta'][$i][1] = $content ;
                    } else {
                        $this->_head['meta'][$i][1] = $content .', '. $this->_head['meta'][$i][1];
                    }
                }

                $lang = trim( $lang );  // HERE
                $this->_head['meta'][$i][4] = $lang ;  // HERE

                return;
            }
        }
        $this->addMetaTag( $name, $content, '', '', $lang );  // HERE
    }
?>
c) The prependMetaTag() function is modified as follow:

Code: Select all

<?php
    function prependMetaTag( $name, $content, $lang='' ) {  // HERE
        $name = trim( htmlspecialchars( $name ) );
        $n = count( $this->_head['meta'] );
        for ($i = 0; $i < $n; $i++) {
            if ($this->_head['meta'][$i][0] == $name) {
                $content = trim( htmlspecialchars( $content ) );
                $this->_head['meta'][$i][1] = $content . $this->_head['meta'][$i][1];

                $lang = trim( $lang );  // HERE
                $this->_head['meta'][$i][4] = $lang ;  // HERE

                return;
            }
        }
        $this->addMetaTag( $name, $content, '', '', $lang );  // HERE
    }
?>
d) The getHead() function is modified as follow:

Code: Select all

<?php
    function getHead() {
        $head = array();
        $head[] = '<title>' . $this->_head['title'] . '</title>';
        foreach ($this->_head['meta'] as $meta) {
            if ($meta[2]) {
                $head[] = $meta[2];
            }
            $head[] = '<meta name="' . $meta[0] . '" content="' . $meta[1] . '"' . (($meta[4] == '')? '' : ' lang="' . $meta[4] . 

'"') . ' />';  // HERE
            if ($meta[3]) {
                $head[] = $meta[3];
            }
        }
        foreach ($this->_head['custom'] as $html) {
            $head[] = $html;
        }
        return implode( "\n", $head ) . "\n";
    }
%>
3. File includes/frontend.php

The mosShowHead() function is modified as follow:

Code: Select all

<?php
function mosShowHead() {
    global $database, $option, $my, $mainframe, $_VERSION, $task, $id;
    global $mosConfig_MetaLang, $mosConfig_MetaDesc, $mosConfig_MetaKeys, $mosConfig_lang, $mosConfig_live_site, $mosConfig_sef, 

$mosConfig_absolute_path, $mosConfig_sitename, $mosConfig_favicon;  // HERE

    if ($my->id || $mainframe->get( 'joomlaJavascript' )) {
        ?>
        <script language="JavaScript" src="<?php echo $mosConfig_live_site;?>/includes/js/joomla.javascript.js" 

type="text/javascript"></script>
        <?php
    }

    $meta = explode("|", $mosConfig_MetaLang);  // HERE
    $lang = array_search($mosConfig_lang, $meta);  // HERE
    if ($lang == -1) $lang = 0;  // HERE

    $meta = explode("|", $mosConfig_MetaDesc);  // HERE
    $desc = ($lang < count($meta))? $meta[$lang] : "";  // HERE

    $meta = explode("|", $mosConfig_MetaKeys);  // HERE
    $keys = ($lang < count($meta))? $meta[$lang] : "";  // HERE

    $query = "SELECT iso"
    . "\n FROM #__languages"
    . "\n WHERE code = '" . $mosConfig_lang . "'";  // HERE
    $database->setQuery( $query );  // HERE
    $database->loadObject( $row );  // HERE
    $iso = $row->iso;  // HERE
    
    $mainframe->appendMetaTag( 'description', $desc, $iso );  // HERE
    $mainframe->appendMetaTag( 'keywords', $keys, $iso );  // HERE
    $mainframe->addMetaTag( 'Generator', $_VERSION->PRODUCT . ' - ' . $_VERSION->COPYRIGHT);
    $mainframe->addMetaTag( 'robots', 'index, follow', '', '', $iso );  // HERE

    // REMAINING CODE...
}
?>
4. File administrator/components/com_config/config.class.php

a) A new class variable called $config_MetaLang must be added:

Code: Select all

<?php
// Meta Settings
    /** @var string */                       // HERE
    var $config_MetaLang        = null;  // HERE
?>
b) The heading comment for the bind() method is containing a typo mistake:

Code: Select all

<?php
    /*
     *    @return null|string null if operation was satisfactory, otherwise returns an error
     */
?>
c) The heading comment for the getVarText() method should be fixed as follow:

Code: Select all

<?php
    /*
     * Builds the configuration file lines for all variables
     */
?>
5. File administrator/components/com_config/admin.config.php

The saveconfig() function is modified as follow:

Code: Select all

<?php
function saveconfig( $task ) {
    // REMAINING CODE...

    // handling of special characters
    $row->config_sitename            = htmlspecialchars( $row->config_sitename, ENT_QUOTES );    

    $row->config_MetaDesc            = htmlspecialchars( $row->config_MetaDesc, ENT_QUOTES );    
    $row->config_MetaKeys            = htmlspecialchars( $row->config_MetaKeys, ENT_QUOTES );    
    $row->config_MetaLang            = htmlspecialchars( $row->config_MetaLang, ENT_QUOTES );  // HERE    

    // REMAINING CODE...
}
?>
6. File administrator/components/com_config/admin.config.html.php

The showconfig() function is modified as follow:

Code: Select all

<?php
    function showconfig( &$row, &$lists, $option) {
        global $mosConfig_absolute_path, $mosConfig_live_site, $mosConfig_session_type, $database, $mainframe;  // HERE

        // REMAINING CODE...

            <?php
            $tabs->endTab();
            $tabs->startTab("Metadata","metadata-page");
            ?>
            <input type="hidden" name="config_MetaLang" value="<?php echo $row->config_MetaLang; ?>" />  <!-- HERE -->
            <input type="hidden" name="config_MetaDesc" value="<?php echo $row->config_MetaDesc; ?>" />  <!-- HERE -->
            <input type="hidden" name="config_MetaKeys" value="<?php echo $row->config_MetaKeys; ?>" />  <!-- HERE -->
            <script type="text/javascript">  <!-- HERE -->
            <!--
                function UpdateMetaHiddenInput(strMetaHiddenInputName, intIndex, strValue) {
                    var objMetaHiddenInput = adminForm[strMetaHiddenInputName];
                    var arrMetaHiddenInput = objMetaHiddenInput.value.split('|');
                    
                    arrMetaHiddenInput[intIndex] = strValue;
                    objMetaHiddenInput.value = arrMetaHiddenInput.join('|');
                }
            //-->
            </script>
            <table class="adminform">
            <?php  // HERE
                $metaLang = explode("|", $row->config_MetaLang);
                $metaDesc = explode("|", $row->config_MetaDesc);
                $metaKeys = explode("|", $row->config_MetaKeys);
                
                for($l = 0; $l < count($metaLang); $l++) {
                    $query = "SELECT name, iso"
                    . "\n FROM #__languages"
                    . "\n WHERE code = '" . $metaLang[$l] . "'";
                    $database->setQuery( $query );
                    $database->loadObject( $langRow );
            ?>
            <tr>  <!-- HERE -->
                <td colspan="2"><b><?php echo $langRow->name . " (" . $langRow->iso . ")"; ?>:</b></td>
            </tr>
            <tr>
                <td width="185" valign="top">Global Site Meta Description:</td>
                <td><textarea class="text_area" cols="50" rows="3" style="width:500px; height:50px" onchange="javascript: 

UpdateMetaHiddenInput('config_MetaDesc', <?php echo $l; ?>, this.innerText);"><?php echo ($l < count($metaDesc))? $metaDesc[$l] : ""; 

?></textarea></td>  <!-- HERE -->
            </tr>
            <tr>
                <td valign="top">Global Site Meta Keywords:</td>
                <td><textarea class="text_area" cols="50" rows="3" style="width:500px; height:50px" onchange="javascript: 

UpdateMetaHiddenInput('config_MetaKeys', <?php echo $l; ?>, this.innerText);"><?php echo ($l < count($metaKeys))? $metaKeys[$l] : ""; 

?></textarea></td>  <!-- HERE -->
            </tr>
            <?php  // HERE
                }
            ?>

        // REMAINING CODE...
    }
?>
That's all folks! It's a piece of cake! :D

I hope this discussion will help the development team to integrate this, again, VERY useful feature.

Cheers,
Eric Elzière.
Last edited by eelziere on Tue Oct 17, 2006 3:07 pm, edited 1 time in total.

Locked

Return to “Wishlist Archives - Archived”