Language switching : Process variable and "redirect" to current_url() |
[eluser]bonatoc[/eluser]
Hello, I'm fairly new to MVC, but I begin to grasp the concept. But as others, I'm coming from procedural habits, so it's still a bit difficult for me to abstract. Anyway, your help is greatly appreciated. Pardon my french if I use any noobie lingo. I guess my problem resembles a lot to changing a site's skin on the fly from whatever URL you're under. If you already know the trick you can stop reading and start replying :-) I am developing a localized e-commerce app. I have chosen not to rely on CI localization system, because my customer is already used to a custom string management system. I need to have an always accessible language switcher in my topnav, which looks like : ENG | ESP | FRA | DEU ... The main problem I have to face is that changing the language should not be interpreted by CI as pointing to a Changelang Class, or a changelang.php Controller. If I am currently under : http://localhost/CI/index.php/admin my language switcher links are : Code: echo current_url()."/changelang/".$language_variable_being_fetched; http://localhost/CI/index.php/admin/changelang/en-utf-8 http://localhost/CI/index.php/admin/changelang/es-utf-8 http://localhost/CI/index.php/admin/changelang/fr-utf-8 ... Since I need the $changelang variable to be parsed and processed from any Class I'm under, I'm wondering how to do this without having to copy-paste a function in each class, and redeclaring loadings of views many (many) times. Changing the language should work when calling it from any section of the site : http://localhost/CI/index.php/admin http://localhost/CI/index.php/products http://localhost/CI/index.php/products/productdetail/59 I want to retrieve the $changelang variable to update a cookie, and including a static file containing all the strings (the system is the same used by PHPMyAdmin when you change language). I think I will have no problem retrieving the $changelang value using uri_segments, but... How do I process the variable in a function (load the proper language file) and then redirect (or reroute?) the user to the exact same page he was prior of the click ? Is it by creating a function __changelang (double underscore) ? You guessed it, I don't want CI to expect a file to point to, called changelang.php. Once the $changelang var is processed, it should simply include the "en-utf-8.php" file, and then evaluate as usual, executing whatever function is called in the URL (again, the page the user was under when he clicked/switched language). Thanks for your help.
[eluser]bonatoc[/eluser]
OK, so I'm somewhat less a newbie than 2 hours before. After doing some RTFM_ing, I ended up with this, in config/routes.php Code: $route['(:any)/changelang/(:any)'] = '$1'; But since I struggle with Regex's, could someone tell me if this will work no matter where "/changelang/" is located in the URL ? What I mean is : Is this expression valid for ALL the following URLs ? http://mysite.com/admin/changelang/de-utf-8 http://mysite.com/products/detail/59/cha...g/de-utf-8 http://mysite.com/products/changelang/de...detail/59/ Also, is there a simple way to clean current_url() or SELF to avoid double declarations ? Like : http://mysite.com/admin/changelang/de-ut...g/de-utf-8 I guess I could pass the URI segments to my View and do some substr from SELF . Thanks ! |
Welcome Guest, Not a member yet? Register Sign In |