简体   繁体   中英

How to Show Different Products Depending on Google Translate Widget Language Chosen

I am developing a website with shopping cart that will have different products per country. Customer wants to use Google Translate to change language.

We thought one website with different product categories for each country would be best. So someone in France chooses FR in Google Translate, and we have some code to show the FR product category.

But I cant find how to determine in my website code (PHP) what language is chosen by the Google Translate Widget. The URL stays the same, so I cannot query the URL.

How would one be able to determine at the server level (PHP) what language has been chosen by the Google Translate Widget?

I wonder if this is even possible as its probably using Javascript to change it, so would there be other ways to tap into the Google Translate Widget so I could use Javascript to show the correct product category?

This is the Google Widget Code

<script type="text/javascript">
function googleTranslateElementInit()
{
    new google.translate.TranslateElement(
    {
        pageLanguage: 'en',
        includedLanguages: 'en,cy',
        layout: google.translate.TranslateElement.InlineLayout.SIMPLE
    },
    'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

Thanks

This is one way, but not ideal due to horrible hashes in the URL...

Google can auto translate the page content if you add #googtrans(en|TARGET_LANG_CODE) to the end of the URL, eg www.mydomain.com#googtrans(en|fr)

So setup a multi-country site like standard, eg /fr, /de etc. (sub-domains could be used or anything in the URL to specify what country, eg ?country=fr).

Using links to each country section of the site, link to the country section with the hash on the end, eg www.mydomain.com/fr#googtrans(en|fr) or fr.mydomain.com#googtrans(en|fr) or www.mydomain.com?country=fr#googtrans(en|fr)

The country section in the URL, eg /fr allows you to show the correct store for that country (and anything else you want to display for that country, perhaps call different images as Google cant translate those), and then the hash allows Google to translate it to the correct language.

Its a horrible hash to use! Be good if we could not use the hash and just translate based on just /fr (probably can do this using the non-free API but this is just for the free widget).

Note: Its probably necessary to exclude the country sections eg /fr from being indexed in Google, because I would hazard a guess Google may see the original language content on these sub-sections before its JS translation kicks in, so we would not want that duplicate content. I do not believe Google indexes the widget translated pages, so just disallow these sections in the robots.txt file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM