user1765862
user1765862

Reputation: 14145

CodeIgniter redirect user on the same page but on other language

I have layout (master) page where I have section divs, header, content, footer, ...

Inside my header div I have country flags. I'm trying to implement solution where user would from any location when clicked on country flag being redirect to the same page with country prefix.

I'm using location with http://jeromejaglale.com/doc/php/codeigniter_i18n and I have pages like

/en/Company/ and /fr/Company/

Now when user for example surfing /en/Company/History and click on french flag to be redirected to /fr/Company/History

Consider that those flags are under layout view so I cannot link those flag with language link from (company/history)view itself.

Should I grab url value and parse that url and replace language prefix with another language prefix or is there some better approach.

Upvotes: 0

Views: 1633

Answers (2)

Federico J.
Federico J.

Reputation: 15902

Well, what I'd do it'd be parse the url, and add the value, as you always have the language variable in the same place. To me, is the only way to do it.

Other problem, is how do you send your data to the view? I think you can show data from your controller to your layout when you call this->load->view(), find out how because it depends on how you send the data, maybe if you paste part of your controller code I can point you how to do it.

Anyway, you'll always have to write the url you want to go in your language flags, changing the url language part value according to the language you're using, via parsing.

Upvotes: 0

BobRock
BobRock

Reputation: 3467

You say that you use i18n codeigniter library.

You can put switch language logic inside controller (default or the one you want to switch lang) switch selected language value. This value can be retrieved using

$this->lang->lang();

This will return you either en or some other language you use

and from there is pretty easy, invert those value and put inside $data['switchLang'] = 'en'; or 'fr' for example, and further configure View page to use that value as

<?php echo anchor($this->lang->switch_uri($switchLang), 'Switch language'); ?>

Upvotes: 2

Related Questions