Welcome Guest, Not a member yet? Register   Sign In
Multilingual application
#1

Hi,
I am trying to find out how to make the web multilingual.
Can you tell me please, what is the best practice?

I have routes like this:
Code:
$route['((en|cz)/)?test'] = 'test/index';
Is there a way to write it better?

In MY_controller I do this:
PHP Code:
public $language 'sk'// should not be protected??? But I can not call it in template if it is protected
protected $languages = ['en''cz'];

public function 
__construct()
{
    
parent::__construct();

    if( 
in_array$s1 $this->uri->segment), $this->languages ) ) $this->language $s1;


Is it the right way to use?

Thanks.
Reply
#2

(This post was last modified: 09-14-2015, 02:48 PM by rtorralba.)

(09-14-2015, 12:06 PM)Camo Wrote: Hi,
I am trying to find out how to make the web multilingual.
Can you tell me please, what is the best practice?

I have routes like this:


Code:
$route['((en|cz)/)?test'] = 'test/index';
Is there a way to write it better?

In MY_controller I do this:


PHP Code:
public $language 'sk'// should not be protected??? But I can not call it in template if it is protected
protected $languages = ['en''cz'];

public function 
__construct()
{
    
parent::__construct();

    if( 
in_array$s1 $this->uri->segment), $this->languages ) ) $this->language $s1;


Is it the right way to use?

Thanks.

Hi,

You can use the following regular expresion for routes:

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

And to can pass variables to views you can use a protected variable to add all variables that you will use at your views:

MY_Controller:
PHP Code:
class MY_Controller extends CI_Controller
{
  protected view_vars = array();
  public function __construct()
  {
     $segment = $this->uri->segment(1);
     $this->view_vars['language'] = isset($segment) && strlen($segment) == $segment 'en';
  }


Example controller:

PHP Code:
class Example extends MY_Controller
{
  public function index()
  {
    $this->view_data['example_data'];
    $this->load->view('view'$this->view_data);
  }

You can use the ci language class http://www.codeigniter.com/user_guide/li...guage.html
Greetings.
Reply
#3

(This post was last modified: 09-14-2015, 04:23 PM by Camo.)

Thanks $view_data in MY_controller is a good idea.
But this route $route['^(\w{2})/(.*)$'] = '$2'; wont work if I want to call e.g. home/index/7 ??? Am I right?
And what about that slash? Here it is non optional part of that regexp. Is that ok?
Reply
#4

(This post was last modified: 09-14-2015, 04:07 PM by rtorralba.)

(09-14-2015, 03:14 PM)Camo Wrote: Thanks $view_data in MY_controller is a good idea.
But this route $route['^(\w{2})/(.*)$'] = '$2'; wont work if I want to call ie. home/index/7 ??? Am I right?
And what about that slash? Here it is non optional part of that regexp. Is that ok?

I think would be good that you have a default language and if always use it and redirect / (segment(1) empty) to /en (default language)
Greetings.
Reply
#5

Default language should be invisible. That's ok it works fine. Have a nice day.
Reply
#6

(09-14-2015, 04:26 PM)Camo Wrote: Default language should be invisible. That's ok it works fine. Have a nice day.

Rate it please.
Greetings.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB