LoginSignup
5
9

More than 5 years have passed since last update.

CodeIgniterで言語指定を動的に変更する方法

Last updated at Posted at 2015-04-08

CodeIgniterでは、/application/config/config.phpで指定された言語に基づいて、$this->lang->line('hoge');の値が出力されますが、以下のように記述することで、動的に言語指定を変更することができます。

$this->lang->is_loaded = array();
$this->lang->language = array();
$this->lang->load('言語ファイル名','言語');

※上記言語ファイル名には「hoge_lang.php」の「hoge」を指定します。

/application/controllers/hoge.php
function hoge() {
	// ここからはconfig.phpで指定された言語になる
	$result['default'] = $this->lang->line('hoge');
	// ここまではconfig.phpで指定された言語になる

	$this->lang->is_loaded = array();
	$this->lang->language = array();
	$this->lang->load('hogehoge','japanese');

	// ここからは日本語になる
	$result['jp'] = $this->lang->line('hoge');
	// ここまでは日本語になる


	$this->lang->is_loaded = array();
	$this->lang->language = array();
	$this->lang->load('hogehoge','english');

	// ここからは英語になる
	$result['en'] = $this->lang->line('hoge');
	// ここまでは英語になる

	$this->lang->is_loaded = array();
	$this->lang->language = array();
	$this->lang->load('hogehoge');

	// ここからはconfig.phpで指定された言語になる
	$result['default2'] = $this->lang->line('hoge');

	// ビューを表示する
	$this->load->view('hogeview',$result);
}
5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9