tags:

views:

66

answers:

5

Let's get to the point,

when at this site

mysite.com/en/index.html

or

mysite.com/en/model1/index.html

and clicking on an <a href="swap current url to /FR/...">FR</a> (or country flag img) What should I do to load this other page?

mysite.com/fr/index.html

or

mysite.com/fr/model1/index.html

I know urlParser JQUery plugin for getting the path, it's just I'm a n00b and I can't code at all (just make beauty html+css). :)

Thanks pals.

A: 

You could use absolute URLs in links like

href="/fr/index.html"

or

href="/fr/model1/index.html"

or get the language compnonent of the current url and replace it with the new one

var current_language = jQuery.url.segment(1);

var str = document.location;

var new_url  = str.replace('/' + current_language + '/', "/fr/");

Something like that

Yasen Zhelev
Ok but then i have to put manually the current lang
Rod
Do you use the same code for each language or you have a separate copies for each language. Do you use pure HTML,CSS,JS or you use some scripting language like PHP? Provide more info what you want to achieve please. "Dror" solutions looks good to me.
Yasen Zhelev
No PHP, just static HTML pages
Rod
Use document.location.href.replace('/en/' '/fr/');on english pages and document.location.href.replace('/fr/' '/en/'); on French then
Yasen Zhelev
Ok, but what happens if there's a third or fourth language in the future?
Rod
A: 

I'm not sure if there should be an relation between your current and your target URL, judging from your text, sorry, but in generally that would be how you applay a click listener to all a elements (specify with class / id) and let it open a specific url:

$(document).ready(function(){
  $('a').click(function() {
    window.location = "http://mysite.com/fr/index.html";
  });
});
Hannes
That's neat but just like a regular a> with href. The point is to get the current path wherever you are, get the first part (language folder) and swap it to other one. If you are at the english home and click on the FR link you are driven to the french home or if you are inside the french ABOUT page and click EN taken to the english ABOUT (just changing that part of the path .com/EN/...)
Rod
aaaaaaaaaaah, now i get what you want :D Well, sorry then, but in the end, just do `window.location = window.location.replace('/en/','/fr/');` of course it would make more sense to use an variable then fixed '/en/' but there are many approaches for that, you can check the current url for occurence of /fr/ or /en/ and act accourdingly, set a cookie, set it somwhere in the clicked element and access that via jQuery ect.
Hannes
A: 

Do you need to replace the /en/ in the path to /fr/?
if so use this:

$(document).ready(function(){
  $('a').click(function() {
    document.location.href =document.location.href.replace('/en/' '/fr/');
  });
});
Dror
That's impressive. But just works in one way. I guess I need an array with all the possible langs or parse the URL and change it then.
Rod
A: 

If there are more than one language add language="en" or language="fr" or whatever you want to each link tag and put this code

$(document).ready(function(){ $('a').click(function() { var language = $(this).attr('language') document.location.href =document.location.href.replace('/en/' '/'+language+'/'); }); });

This way you will define the language to which the link will point in the HTML

Yasen Zhelev
Great! awesome. Thanks so much.
Rod
+1  A: 

Assuming the directory structure is the same for each language...

something as simple as:

<script>
    //set a global var
    var current = '/en/';
    function changeLanguage(to){
        var from = document.location.href;
        var url = from.replace('/' + current + '/', to);
        document.location = url;
    }
</script>
<img src="frenchflag.jpg" onclick="changeLanguage('/fr/')" />
<img src="swissflag.jpg" onclick="changeLanguage('/sw/')" />
<img src="britishflag.jpg" onclick="changeLanguage('/en/')" />

then on each page you just paste and change the current variable and remove the image for that language. With just a little more work you could automate that part too if you happen to have a lot of pages.

AndyD273
That's really nice but what if I want to use the same snippet for all the site (inside an included header)?
Rod
Then do something to detect which page you are on. Something like `var current = jQuery.url.segment(1);` and chose which flag not do display using code. `<div id='flags'></div> <script>var flags = ''; if(current != '/en/'){ flags = flags + '<img... />'; } document.getElementByID('flags').innerHTML = flags; </script>`
AndyD273
I should note that I've never used jQuery.url.segment, just took the example from the other post. `var url = '/' + document.location.href.split("/")[3] + '/';` will return something similar to what url.segment is apparently supposed to do. (3 may not be the correct position for 'en', you'll have to play with it)
AndyD273