Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED Dynamic (fake) pages and $language
#3
Finally I got this sorted out ! Smile
I post the solution here in case anyone is strugling with something similar.

Problem:
Display the correct language for pages created by a plugin through the "data-index", "indexid" and "menuitems" filters.

First, make sure you have seperate translation files for front- and backend.
  • Backend: en_US.php, fr_FR.php, de_DE.php, ...
  • Frontend: en.php, fr.php, de.php, ...
In the top of "my_plugin.php":
PHP Code:
add_filter('menuitems''my_menuitems');
add_filter('indexid''my_indexid');
add_filter('data_index''my_data_index'); 

In the "my_menuitems"-filter function:
PHP Code:
function my_menuitems($menu) {
    global 
$language;

    if (isset(
$_COOKIE['language'])) {
        
i18n_merge('GSDownloads'$language);
    } else {
        
i18n_merge('GSDownloads''en');  // set default language
    
}

    if ((isset(
$_GET['id'])) && ($_GET['id'] === 'my_plugin')) {
        
$menu .= '<li class="index current active"><a href="' $SITEURL 'index.php?id=my_plugin" title="' i18n_r('my_plugin/MENU') . '">' i18n_r('my_plugin/MENU') . '</a></li>';
    } else {
        
$menu .= '<li class="index"><a href="' $SITEURL 'index.php?id=my_plugin" title="' i18n_r('my_plugin/MENU') . '">' i18n_r('my_plugin/MENU') . '</a></li>';
    }
    return 
$menu;


In the "my_indexid"-filter funtion:
PHP Code:
    global $my_plugin_id;  // store id to use later in the "data-index" function

    
if ($id === 'my_plugin') {
        
$my_plugin_id$id;
    } else {
        
$my_plugin_id'';
    }
    return 
$id;


In the "data-index"-filter function:
PHP Code:
function GSDDataIndex($data_index) {
    
    global 
$my_plugin_id$language;
    
    if (
$my_plugin_id === 'my_plugin') {

        
i18n_init();
        if (isset(
$_GET[I18N_SET_LANGUAGE_PARAM])) {
            
$language $_GET[I18N_SET_LANGUAGE_PARAM];
            
i18n_merge('my_plugin'$language);
        } else {
            if (isset(
$_COOKIE[I18N_LANGUAGE_COOKIE])) {
                
$language $_COOKIE[I18N_LANGUAGE_COOKIE];
                
i18n_merge('my_plugin'$language);
            } else {
                
i18n_merge('my_plugin''en');
            }
        }

        
$data_index = new SimpleXmlElement('<?xml version="1.0" encoding="UTF-8"?>' "\n" '<item></item>');
        
$data_index->addChild('title'i18n_r('my_plugin/TITLE'));
        
$data_index->addChild('menu'i18n_r('my_plugin/MENU'));
        
$data_index->addChild('pubDate''');
        
$data_index->addChild('meta''');
        
$data_index->addChild('metad''');
        
$data_index->addChild('url''my_plugin');
        
$data_index->addChild('content'i18n_r('my_plugin/CONTENT'));
        
$data_index->addChild('parent''');
        
$data_index->addChild('template''');
        
$data_index->addChild('private''');    
    }

    return 
$data_index;


VoilĂ , that's all folks ! Big Grin

It took me a while to figure this out, I hope it will be of some use to someone.

Cheers,
Fripsy.
Reply


Messages In This Thread
Dynamic (fake) pages and $language - by fripsy - 2014-11-27, 20:09:12
RE: Dynamic (fake) pages and $language - by fripsy - 2014-11-28, 00:28:42



Users browsing this thread: 1 Guest(s)