Problem with 'HTTP_X_GT_LANG' variable

Popular automatic website translation tool

Problem with 'HTTP_X_GT_LANG' variable

Postby lizardman » Thu Jan 23, 2014 5:40 am

Hi,

I've written a bit of code in order to have GTranslate translate only certain pages for certain languages. I'm using the $_SERVER['HTTP_X_GT_LANG'] variable to detect currently selected language code. I don't know how to achieve this result any other way.

What is the problem? The detection of the currently selected language code works, but not all the time. For example, for two specific pages the 'HTTP_X_GT_LANG' variable is empty. I have no idea why. It works for all other pages.

Also, sometimes it gives the wrong language code. I may be on a pt or fr page, but the 'HTTP_X_GT_LANG' variable contains the bg code. This is quite baffling.

You can see for yourself. I've echoed the full URL as the name of a class. If you go to this page (view source and then search in it for the word 'testing'), you will see right next to the 'testing' class another class with the full URL. It works perfectly. But if you go to this page (again view source, but search for 'notranslate'), you will see right after the 'notranslate' class the class with the URL. This time the 'HTTP_X_GT_LANG' variable is empty. It's the same code, but different result for the second page.

I should mention that if I am on the English version of the page that is not working, and then go to the Bulgarian version through the widget, it works ('HTTP_X_GT_LANG' contains bg). But if I go to the page from another Bulgarian page, the 'HTTP_X_GT_LANG' variable is empty.

Why do you think it's not working for the second page? What could the problem be?
lizardman
 
Posts: 26
Joined: Sat Sep 28, 2013 12:39 am

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby Edvard » Thu Jan 23, 2014 12:43 pm

Hi,

In your original language there will not be HTTP_X_GT_LANG language parameter at all, so you need to check if it is set or not. I think the problem is in your code. Please check if it is set and in that case it should be non empty. If you see that it is set and it is empty let me know.

It would be helpful to post your code here.

Thanks!
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby lizardman » Thu Jan 23, 2014 4:28 pm

Hi,

Yes, in the original language the variable is empty. I'm talking only about the translations.

I don't know how to check if HTTP_X_GT_LANG is set. From what I understood, there are no settings to configure for this, so I just echo it to see if it contains the language code.

Here's the code of the function I use (note that it's just for testing whether it will work - that's why I echo the full current URL):

Code: Select all
function block_auto_translation() {
   $current_lang_code = $_SERVER['HTTP_X_GT_LANG'];
   $trimmed = trim($current_lang_code, '%');
   $current_url = "http://" . $trimmed . '.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   $translated_pages = array(
   
//Bulgarian

"http://bg.thevenusproject.com/about/aims-and-proposals",
"http://bg.thevenusproject.com/about/resource-based-economy",
"http://bg.thevenusproject.com/about/resume",
"http://bg.thevenusproject.com/about/interview",
"http://bg.thevenusproject.com/about/roxanne-meadows",
"http://bg.thevenusproject.com/about/research-center",
"http://bg.thevenusproject.com/technology/cities-in-the-sea",
"http://bg.thevenusproject.com/technology/city-systems",
"http://bg.thevenusproject.com/technology/construction",
"http://bg.thevenusproject.com/technology/energy",
"http://bg.thevenusproject.com/extras/faq",
"http://bg.thevenusproject.com/technology/housing"
   );
   $partially_translated_pages = array(
   "some pages",
   );
   if(in_array($current_url, $translated_pages)) {
   // if in the list of translated pages, do not insert "no translate" tag
   echo "<span class=\"testing\"><span class=\"$current_url\">";
   }
   elseif(in_array($current_url, $partially_translated_pages)) {
   // if in the list of partially translated pages, do not insert "no translate" tag
   echo "<span class=\"testing\"><span class=\"$current_url\">";
   }
   else {
      //block auto translation for all untranslated pages
      echo "<span class=\"notranslate\"><span class=\"$current_url\">";
   }
}


It works for all other pages except http://bg.thevenusproject.com/about/res ... ed-economy and http://bg.thevenusproject.com/technolog ... in-the-sea (for them the HTTP_X_GT_LANG variable is empty, as can be seen from the page source; it is not empty only when I'm on the original language version of those pages and switch to the Bulgarian version through the widget).

Why could it be that for those two pages the variable is empty?
lizardman
 
Posts: 26
Joined: Sat Sep 28, 2013 12:39 am

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby Edvard » Thu Jan 23, 2014 4:43 pm

Please add this code into the end of your index.php file.

Code: Select all
<?php

...

if(isset($_SERVER['HTTP_X_GT_LANG']))
    echo '<!-- current language is: ' . $_SERVER['HTTP_X_GT_LANG'] . '-->';
else
    echo '<!-- language is not set -->';



Then open the pages you are interested in and check the source code.

Let me know, if you see a strange behavior.

Thanks!
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby lizardman » Thu Jan 23, 2014 5:38 pm

Hi,

I added your code to the end of the index.php. Now the language is not set only for this page (across all languages).

Furthermore, sometimes it gives the wrong language code.

Thanks for looking into this!
lizardman
 
Posts: 26
Joined: Sat Sep 28, 2013 12:39 am

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby Edvard » Thu Jan 23, 2014 6:07 pm

Hi,

Please move the code from the end of your index.php to the beginning before your site script is fired and check again.

Make sure that you do not have server side caching.

Thanks!
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby lizardman » Fri Jan 24, 2014 3:25 am

Hi,

Thanks for the suggestions.

Indeed, the problem is due to cache. When I clear Joomla's cache, everything works as it should. Investigating a bit further, the specific cache files that are responsible are the page cache files.

Obviously, one wants to keep page caching in order to improve performance. Can you tell me, is it possible to somehow have the HTTP_X_GT_LANG variable always current? Maybe there are different ways to do it, but I don't know how GTranslate exactly works so I can't think of any right now.

Thanks for your help. :) This means a lot to me.
lizardman
 
Posts: 26
Joined: Sat Sep 28, 2013 12:39 am

Re: Problem with 'HTTP_X_GT_LANG' variable

Postby Edvard » Fri Jan 24, 2014 1:41 pm

Hi,

You need to make sure that the part of your script which should provide dynamic content isn't cached. Not sure what you exactly are trying to do, but as I know you may set to not cache module positions. You can try to use Jumi and write your custom script inside module and specify to not cache it.

Thanks!
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia


  • Related Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests

2GLux