CodeIgniter Forums

Full Version: language by url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I have a multilanguage app. I translate the web writting in routes.php

$route['(\w{2})/(.*)'] = '$2';

$route['(\w{2})'] = $route['default_controller'];


and in config.php

$config['languages'] = array("es" => "espanol", "en" => "english");
$config['redirect_urls']=false;

$config['redirect_urls'] = true;

$autoload['helper'] = array('language','url');
$autoload['language'] = array("app");


and extending lang.php

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Lang extends CI_Lang
{
function __construct()
{
parent::__construct();
$config =& get_config();
if(isset($config['languages']) && isset($config['redirect_urls']))
{
global $URI, $CFG, $IN;
//var_dump($URI);
$lang = (isset($URI->segments['1']))?$URI->segments['1']:'';
$uri=$URI->segments;
if(strlen($lang) == 2 && array_key_exists($lang,$config['languages']) == true)
{
$config['language']=$config['languages'][$lang];
}
elseif($config['redirect_urls'] == true || strlen($lang) == 2 && array_key_exists($lang,$config['languages']) == false)
{
$url=$config['base_url'];
$url.=(empty($config['index_page']))?'':$config['index_page'].'/';
$url.=array_search($config['language'],$config['languages']).'/';
if(strlen($lang)==2)
{
array_shift($uri);
$url.=implode('/',$uri);
}else
{
$url.=implode('/',$uri);
}
header("location: $url");
}
}
}
}


In language, I have the diferents languaje, english, spanish... app_lang.php

$lang['msj_welcome'] = "Welcome to Codeigniter";

$lang['msj_welcome'] = "Bienvenido a Codeigniter";

It works.

But my problem is that I don't know how I can keep the language when I do a redirect for example in a login

function login(){


if (($this->session->userdata('ses_usuario'))){

redirect('home', 'location');

}

My problem is when I submit, the url is always redirected to english url. So,
en/login go to en/home, but es/login go to en/home too, and I want to go to es/home.

Any hints.



El Forum

[eluser]solid9[/eluser]
Have you tried Google Translate API ?
https://developers.google.com/translate/

It will save you tons of time.

El Forum

[eluser]solid9[/eluser]
Have you tried putting your language into SESSION?

Just like the way you put a username when you login and logout in your site.

El Forum

[eluser]Unknown[/eluser]
Thanks, but I would like to have the language in my url,

domain/es/home
domain/en/home

El Forum

[eluser]boltsabre[/eluser]
On a side note about Google Translate and SEO... if you use it, you should block ALL translated pages from the google search engine bot, you DO NOT want auto-translated pages indexed, you'll suffer in the rankings.

Why? It's to stop people from:
1. translating their entire site into every available language, which would be a huge drain on googles resources if, for example, there where 1+ billion websites (and constantly growing) translating into 200+ different languages!
2. Google knows its auto translator is just that, crappy to say the least. It only wants to index high quality pages (regardless of the language).

So while it's fine to use it to provide a service to your customer base, make sure those pages are not indexed by google :-)