Multilingual website solution with multilingual routes |
[eluser]Salvatore Formisano[/eluser]
Hi everyone, during the last few weeks I've been working on a codeigniter site, and I wanted to have the chance to publish everything in both english and italian. I found a few solutions, but none of them delivered what I wanted. I therefore want to share what I've done so far, as I am aware there's a lot to improve. Also, I'm fairly new to both codeigniter and php/back-end programming, so I look forward to hear your hopinions. So, everything starts with a hook written by Phil Sturgeon: Code: <? This will set the CURRENT_LANGUAGE constant which will hold the language we want to display the site in. To have multilingual routes, I created a new directory called 'routes' under the application directory (application/routes). This directory holds (in my configuration) 2 files: routes_en.php and routes_it.php All I do in these files is defining a bunch of constants. Here's a chunk of routes_en.php Code: /* users */ and here's the same piece in routes_it.php Code: /* utenti */ The routes will obviously still need to be declared in application/config/routes.php as all I did so far is defining a few constants. this is a piece of my routes.php that makes use of the constants I published: Code: require_once(APPPATH.'/routes/routes_'.CURRENT_LANGUAGE.'.php'); as you can see I'm including the routes file with the declared constants in the language I need, and I do so by using the CURRENT_LANGUAGE constant. Now, how to call these routes from the view files? I made a helper called routes_helper.php : Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); finally, with this setup this is how a link looks in a view: Code: <?=anchor(get_route(ADMIN_SHOW_USERS_PATH),"<span>".$this->lang->line('users_list')."</span>")?> and if you need to pass any type of data trough the uri: Code: <?= anchor(
[eluser]Salvatore Formisano[/eluser]
So... the good part of this is that you get completely separated and translated routes for different languages, which is good both for the user (who gets meaningful urls in his language) and for seo (meaningful urls AND different urls for different content are always good) the bad part is that this complicates things a bit, as you need to mantain the routes files just as you would need to do for normal language files. Any kind of text content in my database schema has an italian and an english version. For instance, my site_contents table has: id,sc_key,title_it,title_en,content_it,content_en My categories table has: id,cat_name_en,cat_name_it,cat_permalink_en,cat_permalink_it because of this I can easily take advantage of the CURRENT_LANGUAGE constant and make sure even database content and database built urls are being retrieved in the appropriate language. A real case scenario: I want to display the blog posts contained in the "News and Politics" category My route for the blog category archive in routes.php would be Code: $route[BLOG_CATEGORY_ARCHIVE_PATH]="blog_controller/show_category_posts"; the BLOG_CATEGORY_ARCHIVE_PATH would be defined in routes_en.php as Code: define('BLOG_CATEGORY_ARCHIVE_PATH','en/blog/category_archive/:any'); and in routes_it.php as Code: define('BLOG_CATEGORY_ARCHIVE_PATH','it/blog/archivio_categoria/:any'); (urls are just for demo purpose, I'm sure anyone could come up with nicer ones ) SO, I used :any for this route because I want to use a permalink rather than an id. Let's take a look at the "News and Politics" category record in the categories table: Code: ====================================================================================== Now, in this situation the real url an English user would have is: http://www.sitename.com/en/blog/category...d-politics The route will point to the blog_controller and launch the show_category_posts method. From here on it really doesn't need any explanation: since we have the useful CURRENT_LANGUAGE constant, we can use this to let the controller look into the english columns and return the english content. So: 1] Get the category where 'cat_permalink_en' is equal to 'news-and-politics' 2] The record is found and has an id of 22, find all the blog_posts where cat_id is 22 (for clarity this could be the blog_posts structure) Code: ======================================================================================== 3] From here on it's the same, show all the _en contents, use the _en permalinks this is as much as I can write at 03:44 I am aware that the system is far from being perfect (or even good probably) - but this is exactly why I am posting here, I want to know what you people think of it, and if you have any practical ideas and proposals to build on it and make it quicker and easier to setup and mantain. p.s. This will probably give me away for the CI noob that I am, but.. in that pick_language hook I pasted before.. could I make use of the URI helper somehow? It would be really awesome to turn this: Code: // Lang set in URL via ?lang=something into something like this: Code: // Lang set in URL via ?lang=something of course I would be aiming at having it more dinamic, probably checking is the value of segment(1) exists as key of an array of available languages. Thanks to all and thanks to EllisLab for this wonderful framework!
[eluser]Salvatore Formisano[/eluser]
mmh, I'm guessing the idea isn't finding many fans?
[eluser]Ngulo[/eluser]
eeheh mannaggia... tu hai risolto? io sto cercando di fare la route delle url switchando i file di lingua ma non riesco... uff
[eluser]Salvatore Formisano[/eluser]
Hi, yes, I did find a working solution- if you want I can write what I did here, but in a couple of days (you know, it's Christmas ) And by the way, I don't think it's appropriate to discuss anything in a language other than English here. So, let me know if you're interested Cheers
[eluser]Ngulo[/eluser]
Hi, sorry, you're right..it's hard to me but it's better to talk in english language I'm really interested in your solution about routing and really could be great to get some suggestion from you about really thanks for helping me. i know it's Christmas so no problem have nice holidays best regards
[eluser]millo[/eluser]
Hi Salvatore, Strange that you didn't get so much response on this. I would have thought multilingual routes would have been and interesting topic since there doesn't seem to be much around addressing this. I'm currently looking around also. I've seen this http://ellislab.com/forums/viewthread/84593/P105/ but the URL doesn't change.
[eluser]umefarooq[/eluser]
check this thread hope will help http://ellislab.com/forums/viewthread/180320/ |
Welcome Guest, Not a member yet? Register Sign In |