[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:
<?
function pick_language() {
require_once(APPPATH.'/config/language.php');
session_start();
// 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;
}
// Lang has already been set and is stored in a session
elseif( !empty($_SESSION['lang_code']))
{
$lang = $_SESSION['lang_code'];
}
// Lang has is picked by a user.
// Set it to a session variable so we are only checking one place most of the time
elseif( !empty($_COOKIE['lang_code']) )
{
$lang = $_SESSION['lang_code'] = $_COOKIE['lang_code'];
}
// Still no Lang. Lets try some browser detection then
else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
{
// explode languages into array
$accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));
// Check them all, until we find a match
foreach ($accept_langs as $lang)
{
// Turn en-gb into en
$lang = substr($lang, 0, 2);
// Check its in the array. If so, break the loop, we have one!
if(in_array($lang, array_keys($config['supported_languages'])))
{
break;
}
}
}
// If no language has been worked out - or it is not supported - use the default
if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
{
$lang = $config['default_language'];
}
// Whatever we decided the lang was, save it for next time to avoid working it out again
$_SESSION['lang_code'] = $lang;
// Load CI config class
$CI_config =& load_class('Config');
// Set the language config. Selects the folder name from its key of 'en'
$CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);
// Sets a constant to use throughout ALL of CI.
define('CURRENT_LANGUAGE', $lang);
}
?>
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 */
define('ADMIN_SHOW_USERS_PATH','en/admin/users');
define('ADMIN_NEW_USER_PATH','en/admin/users/new');
define('ADMIN_CREATE_USER_PATH','en/admin/users/create');
define('ADMIN_EDIT_USER_PATH','en/admin/users/edit/:num');
define('ADMIN_UPDATE_USER_PATH','en/admin/users/update');
define('ADMIN_DELETE_USER_PATH','en/admin/users/delete/:num');
and here's the same piece in routes_it.php
Code:
/* utenti */
define('ADMIN_SHOW_USERS_PATH','it/amministrazione/utenti');
define('ADMIN_NEW_USER_PATH','it/amministrazione/utenti/aggiungi');
define('ADMIN_CREATE_USER_PATH','it/amministrazione/utenti/salva');
define('ADMIN_EDIT_USER_PATH','it/amministrazione/utenti/modifica/:num');
define('ADMIN_UPDATE_USER_PATH','it/amministrazione/utenti/salva_modifiche');
define('ADMIN_DELETE_USER_PATH','it/amministrazione/utenti/elimina/:num');
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');
/* users */
$route[ADMIN_SHOW_USERS_PATH]="admin/users_controller/show_users";
$route[ADMIN_NEW_USER_PATH]="admin/users_controller/new_user";
$route[ADMIN_CREATE_USER_PATH]="admin/users_controller/create_user";
$route[ADMIN_EDIT_USER_PATH]="admin/users_controller/edit_user";
$route[ADMIN_UPDATE_USER_PATH]="admin/users_controller/update_user";
$route[ADMIN_DELETE_USER_PATH]="admin/users_controller/delete_user";
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');
if ( ! function_exists('get_route'))
{
function get_route($route,$parameter=NULL)
{
$string = "walawalabingbang";
$any = ":any";
$num = ":num";
if(strpos($route,$any)!==false){
if($parameter==NULL) return "You need to specify a parameter for this route";
$route = str_replace($any,$parameter,$route);
}
if(strpos($route,$num)!==false){
if($parameter==NULL) return "You need to specify a parameter for this route";
$route = str_replace($num,$parameter,$route);
}
return $route;
}
}
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(
get_route(ADMIN_DELETE_USER_PATH,$user->id),
$this->lang->line('app_delete'),
array("class"=>"ico ico-delete delete delete_user")
); ?>