Welcome Guest, Not a member yet? Register   Sign In
[Snippet] Switch language with URL
#1

(This post was last modified: 03-01-2018, 03:49 AM by V I R U S.)

Some people around net are digging about how to make their website using multiple languages and allow users to switch this from within URL without modifying routes, adding new routes or using some prefixes/parameters in your controllers.

Some examples on what we are trying to get:

http://example.com/content - content with default language
http://example.com/en/content - content with english language
http://example.com/de/content - content with german language
http://example.com/ru/content - content with russian language

The simple trick is in modifying query url and checking if first element is withing langeages list. Then just to remove this one, to get modified url parameter.

First, you will need to alter your "config.php" file and add a list of all available languages.
Code:
$config['language']    = 'english';
$config['languages']    = [
  'english' => 'en',
  'german' => 'de',
  'russian' => 'ru'
];

Now we will have to create new file "MY_Router.php" in "/application/core" folder, with following content:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Router extends CI_Router {

    protected function _parse_routes()
    {
    // Language detection over URL
    if($this->uri->segments[1] == $this->config->config['language']) {
      unset($this->uri->segments[1]);
    }    
    if(array_search($this->uri->segments[1], $this->config->config['languages'])) {
      $this->config->config['language'] = array_search($this->uri->segments[1], $this->config->config['languages']);
      unset($this->uri->segments[1]);
    }
    
    // Return default function
    return parent::_parse_routes();
    }  
}

Thats it. Now for example default "welcome" route will be accessible thru "/", "/en", "/de", "/ru" url, you will just have to add some text Smile

There is also some other magic, how you can use your Database to get all text translated Smile

Enjoy!
Reply
#2

It's a good solution! Thank you for that!
Reply
#3

Use the codeigniter language library with this class extension: URI Language Identifier. I also use this controller for switching the language

class LangSwitch extends CI_Controller {

public function __construct() {
parent::__construct();
}
public function switchLanguage($language = "") {

$this->load->library('user_agent');
$referrer = $this->agent->referrer();

$l = substr($referrer, strlen(base_url()));

if(isset($referrer)){
preg_match('/\/(.+)$/i',$l,$match);
$redirect_url;
if (empty($match)) {
redirect(base_url().$language ,'refresh');
}
else{
$redirect_url = base_url().$language.$match[1];
}
redirect($redirect,'refresh');
}else{
redirect(base_url(),'refresh');
}
}
}
Reply
#4

site_url() and base_url() function
not work....
Reply
#5

For site_url() and base_url() you need to load the url_helper
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

Hi , thanks for sharing.
Can you please help me to to this auto localization in codeigniter 4
Thanks
Reply
#7

Hi Guys,

I am new to here and know I am not skilled in coding like some of you are but I do come from a digital marketing background. Wouldn't pulling all this information to translate the languages slow the site down? I know Chrome automatically has this feature for certain languages where you can choose the language you want to view the site in. if its a must for your site to automatically show the correct language depending on search location than I understand doing this but if not why do this
Reply
#8

Most of the new web browser allow the translation of the web page to a different language,
but some of them get it wrong.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB