28
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FuelPHPで日英などの多言語切り替えに対応する

Posted at

FuelPHPでブラウザの言語設定によって表示文字言語を切り替えてみました。
※ URLに/en/ /ja/等のパラメータは入りません。

fuel/app/bootstrap.php
// Initialize the framework with the config file.
Fuel::init('config.php');

$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($languages[0]) && preg_match('/^en/i', $languages[0])) {
	Config::set('language', 'en');
} else  {
	Config::set('language', 'ja');
}

サイト内で必要な文言は言語毎(以下は日本語と英語)にファイルに記述。

fuel/app/lang/ja/main.php
return array(
	'lbl_hello' => 'こんにちは!',
	'lbl_bye' => 'またね!'
);
fuel/app/lang/en/main.php
return array(
	'lbl_hello' => 'Hello!',
	'lbl_bye' => 'Bye!'
);

Controllerで言語ファイルを読み込んで

        public function before()
        {
                \Lang::load('main');

Viewで表示します。

echo __('lbl_hello');

確認は、ブラウザの言語設定を切り替えて確認します。

他に適切な方法が有ればコメントお願いします。

28
28
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
28
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?