qTranslate-Team / qtranslate-x

Wordpress plugin: Adds user-friendly and database-friendly multilingual content management and translation support.

Home Page:http://qtranslatexteam.wordpress.com/about/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option for showing post in default language if translation not available

cr1xu5 opened this issue · comments

commented

Hi qTranslateTeam,

First, thank you for your great work! Since I found that this plugin is actively developed, I'll try to do a feature request.

I would be very pleased to see an option in the admin panel to also show posts in the default language when a translation is not available. Currently, (if I'm correct) this is only possible for the title of the post.

Something like this would be perfect:

Sorry, this entry is only available in Language

to:

Sorry, this entry is only available in Language:

post in default language here

Thanks!

Hi Zeus, please describe the details. Which admin page exactly do you mean, for example?

commented

Hi John, I'm sorry. I meant for an extra checkbox in the qTranslate Configuration page which could enable "Show complete post/page in default language when a translation is not available (even though the user chose another language)".

Currently it's only configurable when a translation is not available to show a link to the post in another language or to hide the post complety for the unavailable language. When I choose the first configuration, I find what I'm looking for is already implemented in the title of the post. So I was hoping this could perhaps be implemented for the body of the post too.

You mean configuration option in admin, which controls how it is shown on front-end? You do not mean to load default language to the editor in back-end instead of empty text, do you?

As showing at front end, sometimes there are a few languages available with translation, and links to all of them show up. Which language would you display then? A visitor of a site is not aware of the concept of 'default language' and may be puzzled why some particular language is shown over others available. Links treat all languages equally and offer user to choose by clicking one of them.

How would you deal with this?

commented

It is for the front-end and I understand it's not a very common use case. The idea behind this is that a large part of our userbase uses browsers in English (on the university), while they still speak our default language. We only support two languages, so I'd like to save them the time of loading another page. Even more, I've found that Google indexes the empty English pages, while I would still like the content to be found on Google, even when searched for in our default language.

We used to accomplish this by filtering qtrans_postsFilter. Perhaps you could help me with which function would be best to create a filter or a hook for. Also with regard to compatibility with future releases. Thank you!

It makes perfect sense on the sites with two languages. Why function postsFilter?

Search for $q_config['not_available'] to find a place to change. Actually I guess, it is pretty simple change, you would have to modify the end of function qtranxf_use_block, where the 'unavailable' message goes out, to include the text in the default language as well, if the new option is on. And we can add some 'politically correct' additional message about the display of the default language, if number of available languages is more than one.

Do you wish to implement it and to submit pull request?

commented

Ok great! I would wish I could, but unfortunately I don't have the time at the moment.

Yeah, the things are hectic for me too now. Let us keep it open, some day we will add that in.

In a bilingual project, I need to show posts in chosen language, or in alternate language if not available, with no additionnal messages.

Replacing $q_config['not_available'][$lang] with $lang_text on line 1306 at the end of the qtranxf_use_block function in qtranslate_core.php works well for this.

As I am relatively new to WordPress, I do not know how to permanently override this in my child theme's function.php - any help would be appreciated.

Hello and thank you for that great and useful plugin ! :)

Any news of that default language update ?

I could make use of it. ;)

I tried the PHP modification qtranslate_core.php, but can't get the desired result...

If you have another solution while waiting for the Plugin update i'm interested. ^^

Thank you.

I ended up doing something a bit different in qtranslate_core, since that last change did not work in some instances. Replace last line in same use_block function (french and english in this example) :

if ($lang == 'fr'){
return "<p>".preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $content['en'])."</p>";
}
else {
return "<p>".preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $lang_text)."</p>";
}

Hi Delorimier,

Thank your for that fast reply ! :)

I must replace that block ? :


if(!$show_available){
// check if content is available in default language, if not return first language found. (prevent empty result)
if($lang!=$q_config['default_language']){
$language = $q_config['default_language'];
//$lang_text = qtranxf_use($language, $text, $show_available);
$lang_text = $content[$language];
$lang_text = trim($lang_text);
if(!empty($lang_text)){
if ($q_config['show_displayed_language_prefix'])
return "(".$q_config['language_name'][$language].") ".$lang_text;
else
return $lang_text;

}

Sorry but it seems difficult for me to find which part to replace.

Here is the full block:


if (!function_exists('qtranxf_use_block')){
function qtranxf_use_block($lang, $blocks, $show_available=false, $show_empty=false) {
global $q_config;
$content = qtranxf_split_blocks($blocks);

// if content is available show the content in the requested language
if(!empty($content[$lang])) {
    return $content[$lang];
}elseif($show_empty){
    return '';
}

// content not available in requested language (bad!!) what now?

// find available languages
$available_languages = array();
foreach($content as $language => $lang_text) {
    //$lang_text = trim($lang_text);//do we need to trim? not really ... but better trim in qtranxf_split_blocks then
    //$content[$language]=$lang_text;
    if(!empty($lang_text)) $available_languages[] = $language;
}

//// if no languages available show full text
//if(sizeof($available_languages)==0) return $text;//not is is not empty, since we called qtranxf_get_language_blocks
//// if content is available show the content in the requested language
//if(!empty($content[$lang])) {//already done above now
//  return $content[$lang];
//}
//// content not available in requested language (bad!!) what now?

if(!$show_available){
    // check if content is available in default language, if not return first language found. (prevent empty result)
    if($lang!=$q_config['default_language']){
        $language = $q_config['default_language'];
        //$lang_text = qtranxf_use($language, $text, $show_available);
        $lang_text = $content[$language];
        $lang_text = trim($lang_text);
        if(!empty($lang_text)){
            if ($q_config['show_displayed_language_prefix'])
                return "(".$q_config['language_name'][$language].") ".$lang_text;
            else
                return $lang_text;
        }
    }
    foreach($content as $language => $lang_text) {
        $lang_text = trim($lang_text);
        if(empty($lang_text)) continue;
        if ($q_config['show_displayed_language_prefix'])
            return "(".$q_config['language_name'][$language].") ".$lang_text;
        else
            return $lang_text;
    }
}
// display selection for available languages
$available_languages = array_unique($available_languages);
$language_list = "";
if(preg_match('/%LANG:([^:]*):([^%]*)%/',$q_config['not_available'][$lang],$match)) {
    $normal_separator = $match[1];
    $end_separator = $match[2];
    // build available languages string backward
    $i = 0;
    foreach($available_languages as $language) {
        if($i==1) $language_list  = $end_separator.$language_list;
        if($i>1) $language_list  = $normal_separator.$language_list;
        $language_list = '<a href="'.qtranxf_convertURL('', $language, false, true).'">'.$q_config['language_name'][$language].'</a>'.$language_list;
        $i++;
    }
}
//qtranxf_dbg_echo('$language_list=',$language_list,true);
//if(isset($post)){
//  //qtranxf_dbg_echo('$post='.$post);
//}
return "<p>".preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $q_config['not_available'][$lang])."</p>";

}
}
function qtranxf_showAllSeparated($text) {
if(empty($text)) return $text;
global $q_config;
$result = '';
foreach(qtranxf_getSortedLanguages() as $language) {
$result .= $q_config['language_name'][$language].':'.PHP_EOL.qtranxf_use($language, $text).PHP_EOL.PHP_EOL;
}
return $result;

}

If i have multiple languages i must duplicate your block and edit the portion "if ($lang == 'fr'){".

The main language of the site is French and is translated in English, German, Portuguese 4 others.

Thank you for your help ! :)

Or maybe here ?

//qtranxf_dbg_echo('$language_list=',$language_list,true);
//if(isset($post)){
// //qtranxf_dbg_echo('$post='.$post);
//}
return "

".preg_replace('/%LANG:([^:]):([^%])%/', $language_list, $q_config['not_available'][$lang])."

";
}
}

Replace line 1388 in current version with code provided in my earlier response.

return "<p>".preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $q_config['not_available'][$lang])."</p>";

English content should then be shown when French content is not available, and vice-versa. Hope this works for you. Just change "en" and "fr" with you own language codes (en is main language on my site). Keep a copy, these changes will be overwritten following plugin updates. I see you have multiple languages, I guess you can make it work with more if else statements - you can use $content['en'] instead of $lang_text on that last line.

What needs to happen exactly if selected language content is not available? Show main language (french)? Is french content always available? In that case, this line only should work (replacing 1388 line):

return "<p>".preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $content['fr'])."</p>";

Or maybe:

return "<p>".preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $lang_text)."</p>";

Hello Delorimier,

Thank you for your help.

I wanted the plugin to redirect the visitor to French page if this page in the visitor langage is not available. This is for an event calendar which i want to update only in one langage.

This line seems to do the trick !

return "

".preg_replace('/%LANG:([^:]):([^%])%/', $language_list, $content['fr'])."

";

I'm testing around, will tell you if something goes wrong.

Thanks again Man ! :)

I finalized it together with the new option "Show content in an alternative language" in the latest on GitHub. Please, test it all with various number of available languages, single post view, search view and all other kind of applicable views. The pre-content message differs depending on the number of languages available and whether or not the alternative language is the default one. Altogether, there are four different cases. The messages may need to be polished a bit more, this is the most difficult part of this change.

commented

I like what you did with qtranxf_use_block! Thank you for making this truly happen. Works as expected with two languages. The additional messages are also really nice.

Perhaps it's an idea to change "For the sake of viewer convenience, the content is shown below in the alternative language." to "For your convenience, it is shown below." as to not distract too much from the content.

Also shouldn't we have to mention the language that is used in this case?: 'For the sake of viewer convenience, the content is shown below in an available alternative language.' Perhaps something like 'For the sake of viewer convenience, the content is shown below in the first available language, %s.' is a good idea?

first available language, %s.' is a good idea?

In the code, you may find commented lines with '%s'. When I tried it, the language name was repeated twice in two sentences following each other. It looked a bit crowded, so I deleted %s, but I am not really sure about the best way. Uncomment lines to see how it looks. Do you like it?

This issue can probably be closed now. We can continue discussion under closed issue, and may re-open it, if needed.

Thank you for the optimizations John. 👍

Thanks again for the work developing this plugin, it's very useful .However, and following the request of this issue, I would appreciate the option to show or hide the language warning: "For the sake of viewer convenience, the content is shown below in the alternative language", and "You may click the link to switch the active language". It can be fine to warn the user about the post not in a certain language, but in my case it is obvious, and I'd rather not showing any message at all, just the post itself. Thanks!

Hi, +1 for the suggestion of @xabispacebiker
It would be nice to be able to disable the warning message.

In the meantime I disabled the message in the frontend by adding this to the Theme's Stylesheet style.css

.qtranxs-available-languages-message {
    display: none;
}

@tarator THANK YOU!!!!!!!!!!!!!!!!!!!