How to show French or English ads based on the language of the site?

Hi,

I search here and on the web. I tried PHP code but it did not work. I think it's not taking php code in the box. So I tried Javascript(new for me) but I am sure that I am missing something simple for gurus.
Here is the code I used. I included a picture of the result. It's responding to my "switch" since it continue even if it's a French blog.
--------
<p id="after_ad" align="center">
<script>
var language = document.getElementsByTagName("html"[0].getAttribute("lang";
var lang = language .substring(0,2);

switch (lang) {
case 'fr':
<script type="text/javascript" src="http://mysite.com/ap/js.php#type=banner&align=center&zone=5"></script>;
break;
default:
<script type="text/javascript" src="http://mysite.com/ap/js.php#type=banner&align=center&zone=4"></script>;
}

</script>
</p>
-----------------------------
Thanks in advance for the help!
Steve

    Milan

    Hello @Steve McGrath,

    I hope you are having a good day and thanks for the asking.

    Could you please try out this code snippet rather than one you are using right now ?

    <script>
    var language = document.getElementsByTagName("html")[0].getAttribute("lang");
    var lang = language .substring(0,2);
    
    switch (lang) {
    	case 'fr':
    		jQuery.getScript( "http://mysite.com/ap/js.php#type=banner&align=center&zone=5", function( data, textStatus, jqxhr ) {
    		  // console.log( data ); // Data returned
    		  // console.log( textStatus ); // Success
    		  // console.log( jqxhr.status ); // 200
    		  // console.log( "Load was performed." );
    		});
    		break;
    	default:
    		jQuery.getScript( "http://mysite.com/ap/js.php#type=banner&align=center&zone=4", function( data, textStatus, jqxhr ) {
    		  // console.log( data ); // Data returned
    		  // console.log( textStatus ); // Success
    		  // console.log( jqxhr.status ); // 200
    		  // console.log( "Load was performed." );
    		});
    }
    
    </script>

    Let me know if it works for you or not. And if it don't work for you Would you please grant me support staff access so that I can check in depth regarding your issue ?. You can grant me support access via WPMU DEV > Support > Support Access > Grant Access.

    Cheers,
    Milan Savaliya.

    Milan

    Hello @Steve McGrath,

    I hope you are having a good day and sorry for being late here.

    I've tested on your site it is working as it should. Code I've given is working fine. What above code does is, first it detects what is in language attribute of html element. Then it filter out language and loads respective script.

    Right now I've remove some comments for your confirmation so that you can confirm that when you are on "test123" subsite it loads french version of script and when you are on main blog it loads english version of script. For your confirmation I've attached some screenshots please do review theme too.

    Cheers,
    Milan Savaliya.

    Milan

    Hello @Steve McGrath,

    Hope you are doing well.

    Sorry but it's not displaying any French ad banners with your code.

    Dear member french ad banners will be fetched by scripts you've used. Above code just loads respective scripts according to language used in your blog. If scripts you've used is not properly working then I am afraid my above code won't render ads perfectly.

    Dear @Steve McGrath code which i've given is working fine which can be confirmed by looking at javascript logs in browser console. It seems to me that scripts which you've used to render french ads are not loading french adds. There can be problem with scripts you've used in code which are

    http://mysite.com/ap/js.php#type=banner&align=center&zone=5

    and

    http://mysite.com/ap/js.php#type=banner&align=center&zone=4

    Please ensure that these scripts are properly returning content you want to display on your page. You can confirm what these scripts are loading by creating simple html file on your host and using this code as sample.

    <html>
    	<head>
    		<title>
    			Test
    		</title>
    	</head>
    	<body>
    		<div class="frecnad">
            <h1>French Ad Script Must Load French Ad banner. </h1>
    			<script src="http://mysite.com/ap/js.php#type=banner&align=center&zone=5"></script>
    		</div>
    
    		<div class="enad">
                     <h1>Eng Ad Script Must Load Eng Ad banner. </h1>
    			<script src="http://mysite.com/ap/js.php#type=banner&align=center&zone=4"></script>
    		</div>
    	</body>
    </html>

    Go to your host, create test html file as "test.html" and paste above code in it. Then open file in any browser and see what its rendering. If its working fine then I suggest you take snapshot of result and attach in your next post.

    Feel free to ping me back if you face any problem during testing.

    Cheers,
    Milan Savaliya.

    Steve McGrath

    Hi,

    I created /test123.html and my French banners are showing(pic) correctly thus the script is working fine.

    The "Before" Ad Code show English banners(4) at it's simplest form for it's usage . If I wanted French, I would put a 5 instead of near the end of the line.

    I used that same code on other sites but mostly English. But, I have 1coupleavie.com/azooc.com showing the French/English banners because the dating script I'm using is multilingual. If a user is French, they click on that language flag and it will fetch the translated French page from the database. I don't have to deal with a "switch" for that part of code to display banners.

    I don't want to create /fr folder just to be able to handle French banners. WP does a good job so far with multilingual sites.

    I'm sure it's something simple by using the right code. If you have other methods, I am open to suggestions.

    Later
    Steve

    Milan

    Hello @Steve McGrath,

    I hope you are having a good day.

    Thanks for providing me required information and following steps i've described. I really appreciate your efforts.

    Would you be able to send in your FTP and wp-admin credentials so I could take a look?

    You can send the info through our private contact form: https://premium.wpmudev.org/contact/

    Select "I have a different question" for your topic - this and the subject line ensure that it gets assigned to me

    Send in:

    Subject: "Attn: Milan Savaliya"
    -WordPress admin username
    -WordPress admin password
    -login url
    -FTP credentials (host/username/password)
    -link back to this thread for reference
    -any other relevant urls

    Once we have this information then I can debug the issue further

    Look forward to hearing back!

    Cheers,
    Milan Savaliya.

    Milan

    Hello @Steve McGrath,

    I hope you are having a good day and sorry for being late here.

    Thanks for sending requested information.

    There was problem with the way you were trying to load scripts, i've just replaced your previous code snippet with new modifications as below,

    <p id="afterad">
    	<script>
    		jQuery(document).ready(function($) {
    
    			var language = document.getElementsByTagName("html")[0].getAttribute("lang");
    			var lang = language .substring(0,2);
    
    			console.log( "Blog Language: " + lang );
    
    			var script   =    document.createElement('script');
    			// script.type  =    'text/javascript';
    
    			switch(lang){
    				case 'fr':
    					script.src   =    'http://misfan.com/ap/js.php#type=banner&align=center&zone=5';
    					break;
    				default:
    					script.src   =    'http://misfan.com/ap/js.php#type=banner&align=center&zone=4';
    					break;
    			}
    
    			$("#afterad").append( script );
    
    		});
    	</script>
    </p>

    I've placed above code snippet in Network Admin > Settings > Advertising > After Add textarea.

    What above code snippets does is that it creates script tag dynamically once document loads and then appends it to paragraph and finally rest of the work is governed by scripts which loaded by this way.

    Right now you don't need to do any changes or modifications on your end as I've completed those task on behalf of you. I've attached screenshots to confirm that its working as it should.

    I hope this helps you.

    Please let me know if there is anything else I can help you with.

    Enjoy WPMU DEV.

    Cheers,
    Milan Savaliya.