config codeigniter multiple language
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends CI_Controller { public function __construct() { parent::__construct(); check_lang(); $this->load->model('mail_model'); } public function index() { $data['mails'] = $this->mail_model->get_all_mail(); $data['language']=$this->session->userdata('language'); // echo '<pre>'; // print_r($data); // echo '</pre>'; $this->load->view('home_view2',$data); } } |
default_helper.php from folder helpers
Model
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Myxx_model extends CI_Model { public function get_all_mail() { return $this->db->get('tbl_xxx')->result_array(); } } |
view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> </head> <body> <h1>Welcome to CodeIgniter!</h1> <p><a href="?lang=th"> thai </a></p> <p><a href="?lang=en"> eng </a></p> <?php // echo lang('nav_home'); // echo '<hr>'; // //echo $name['name_'.$language]; // echo '<hr>'; //echo $mails; foreach ($mails as $value) { echo $value['content_'.$language]; echo "<br>"; } ?> </body> </html> |
example data
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE TABLE `tbl_mail` ( `mid` int(11) NOT NULL, `content_en` varchar(200) NOT NULL, `content_th` varchar(200) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Dumping data for table `tbl_mail` -- INSERT INTO `tbl_mail` (`mid`, `content_en`, `content_th`) VALUES (1, 'aaaaaaaaaaaaa', 'หหหหหหหหหหหหหหห'), (2, 'hhhhhhhhhhh', 'สสสสสสสสสสส'); |
autoload
1 |
$autoload['libraries'] = array('database', 'session'); |
1 |
$autoload['helper'] = array('html','url','default_helper','language'); |
default_helper from folder helpers
Applications->helpers (folder)
default_helper.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); if ( ! function_exists('check_lang')) { function check_lang() { $CI =& get_instance(); if($CI->input->get('lang') AND ($CI->input->get('lang')=='th' || $CI->input->get('lang')=='en' || $CI->input->get('lang')=='jp')){ $CI->session->set_userdata('language',$CI->input->get('lang')); } if(!$CI->session->userdata('language')){ $CI->session->set_userdata('language','th'); }else{ switch ($CI->session->userdata('language')) { case 'th': $CI->lang->load('pages', 'thai'); break; case 'en': $CI->lang->load('pages', 'english'); break; case 'jp': $CI->lang->load('pages', 'japanese'); break; } } } } |
Applications-> language -> english & thai
pages_lang.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $lang['lang_th'] = 'ภาษาไทย'; $lang['lang_en'] = 'English'; $lang['lang_jp'] = '日本語'; $lang['nav_home1'] = 'Home'; $lang['nav_news'] = 'News'; $lang['nav_course'] = 'Course'; $lang['nav_events'] = 'Events'; $lang['nav_our_history'] = 'Our History'; $lang['nav_about_us'] = 'About Us'; $lang['nav_personnel'] = 'Personnel'; $lang['nav_journal'] = 'Journal'; $lang['nav_other'] = 'Other'; $lang['nav_contact_us'] = 'Contact us'; $lang['nav_gallery'] = 'Gallery'; |
folder thai
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $lang['lang_th'] = 'ภาษาไทย'; $lang['lang_en'] = 'English'; $lang['lang_jp'] = '日本語'; $lang['nav_home'] = 'กกกก'; $lang['nav_news'] = 'News'; $lang['nav_course'] = 'Course'; $lang['nav_events'] = 'Events'; $lang['nav_our_history'] = 'Our History'; $lang['nav_about_us'] = 'About Us'; $lang['nav_personnel'] = 'Personnel'; $lang['nav_journal'] = 'Journal'; $lang['nav_other'] = 'Other'; $lang['nav_contact_us'] = 'Contact us'; $lang['nav_gallery'] = 'Gallery'; |
Spacial thanks N’Bank Lang center DRU for training me
Full code (dB in model)
https://github.com/devbanban/ci-multiple-lang