Welcome Guest, Not a member yet? Register   Sign In
Multilingual website solution with multilingual routes
#2

[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 Big Grin )

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:
======================================================================================
id | cat_name_en       | cat_name_it        | cat_permalink_en  | cat_permalink_it  
======================================================================================
22 | News and Politics | Notizie e Politica | news-and-politics | notizie-e-politica
--------------------------------------------------------------------------------------

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:
========================================================================================
id | cat_id | post_title_en | post_title_it    | post_permalink_en | post_permalink_it
========================================================================================
6  | 22     | A simple post | Un semplice post | a-simple-post     | un-semplice-post
----------------------------------------------------------------------------------------

(RECORD CONTINUES..)
========================================================================================
post_content_en              | post_content_it        |
========================================================================================
this morning the president.. | questa mattina il pr.. |

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 Big Grin


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
  if(!empty($_GET['lang']))
  {
    // Turn en-gb into en
    $lang = substr($_GET['lang'], 0, 2);
    $_SESSION['lang_code'] = $lang;
  }

into something like this:

Code:
// Lang set in URL via ?lang=something
  if($this->uri->segment(1)=='en' || $this->uri->segment(1)=='it')
  {
    // Turn en-gb into en
    $lang = $this->uri->segment(1);
    $_SESSION['lang_code'] = $lang;
  }

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!


Messages In This Thread
Multilingual website solution with multilingual routes - by El Forum - 08-08-2010, 08:13 PM
Multilingual website solution with multilingual routes - by El Forum - 08-08-2010, 08:48 PM
Multilingual website solution with multilingual routes - by El Forum - 08-09-2010, 05:18 AM
Multilingual website solution with multilingual routes - by El Forum - 12-24-2010, 11:10 AM
Multilingual website solution with multilingual routes - by El Forum - 12-24-2010, 12:00 PM
Multilingual website solution with multilingual routes - by El Forum - 12-24-2010, 03:49 PM
Multilingual website solution with multilingual routes - by El Forum - 02-06-2011, 03:39 PM
Multilingual website solution with multilingual routes - by El Forum - 02-07-2011, 05:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB