Forum Archive - Problem with 'HTTP_X_GT_LANG' variable

Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 1:40 am by lizardman
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?
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 8:43 am by Edvard
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!
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 12:28 pm by lizardman
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): 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?
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 12:43 pm by Edvard
Please add this code into the end of your index.php file. <?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!
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 1:38 pm by lizardman
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!
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 2:07 pm by Edvard
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!
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 23, 2014 11:25 pm by lizardman
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.
Re: Problem with 'HTTP_X_GT_LANG' variable
Jan 24, 2014 9:41 am by Edvard
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!

SIMILAR TOPICS

Joomshaper Pagebuilder buttons Link edit problemJan 11, 2019 10:04 amReplies: 1Post by: kumatest
Problem with pluginDec 30, 2018 11:59 pmReplies: 1Post by: Iyari
Problem of translation in my websiteDec 8, 2018 12:15 pmReplies: 1Post by: Ibrahim
Problem with qTranslate-x, please help!Aug 22, 2018 9:14 amReplies: 1Post by: sadammkhan
The problem with the translation of the social pluginDec 27, 2017 7:52 amReplies: 1Post by: UriyFil

Try GTranslate with a free 15 day trial