Support for dynamically language setting

This commit is contained in:
Sebastian Korotkiewicz 2014-10-26 16:59:35 +01:00
parent 24323dd63f
commit b69c6650f0
4 changed files with 201 additions and 3 deletions

View File

@ -101,7 +101,7 @@ $config['charset'] = 'UTF-8';
| setting this variable to TRUE (boolean). See the user guide for details.
|
*/
$config['enable_hooks'] = FALSE;
$config['enable_hooks'] = TRUE;
/*

View File

@ -10,7 +10,11 @@
|
*/
$hook['pre_controller'][] = array(
'function' => 'pick_language',
'filename' => 'pick_language.php',
'filepath' => 'hooks'
);
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */

View File

@ -0,0 +1,118 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Language
*
* Which language to use
* Translate Stikked to your own language, see htdocs/application/language files
* Currently: english, german, swissgerman, spanish, norwegian, portuguese, turkish, french, japanese, polish, russian, chinese-simplified, chinese-traditional
*
*/
$config['supported_languages'] = array(
'en' => array(
'name' => 'English',
'folder' => 'english',
'direction' => 'ltr',
'codes' => array('en', 'english', 'en_US'),
'ckeditor' => NULL
),
'de' => array(
'name' => 'Deutsch',
'folder' => 'german',
'direction' => 'ltr',
'codes' => array('de', 'german', 'de_DE'),
'ckeditor' => NULL
),
'sw' => array(
'name' => 'Schweizerdeutsch',
'folder' => 'swissgerman',
'direction' => 'ltr',
'codes' => array('sw', 'swissgerman', 'sw_SW'),
'ckeditor' => NULL
),
'es' => array(
'name' => 'Espa&ntilde;ol',
'folder' => 'spanish',
'direction' => 'ltr',
'codes' => array('esp', 'spanish', 'es_ES'),
'ckeditor' => NULL
),
'no' => array(
'name' => 'norsk',
'folder' => 'norwegian',
'direction' => 'ltr',
'codes' => array('no', 'norwegian', 'no_NO'),
'ckeditor' => NULL
),
'pt' => array(
'name' => 'Portugu&ecirc;s de Portugal',
'folder' => 'portuguese',
'direction' => 'ltr',
'codes' => array('ptb', 'portuguese-portugal', 'pt_PT'),
'ckeditor' => 'pt-pt'
),
'tr' => array(
'name' => 'Türkçe',
'folder' => 'turkish',
'direction' => 'ltr',
'codes' => array('tr', 'turkish', 'tr_TR'),
'ckeditor' => NULL
),
'fr' => array(
'name' => 'Français',
'folder' => 'french',
'direction' => 'ltr',
'codes' => array('fra', 'french', 'fr_FR'),
'ckeditor' => NULL
),
'jp' => array(
'name' => '日本語',
'folder' => 'japanese',
'direction' => 'ltr',
'codes' => array('jp', 'japanese', 'jp_JP'),
'ckeditor' => NULL
),
'pl' => array(
'name' => 'Polski',
'folder' => 'polish',
'direction' => 'ltr',
'codes' => array('plk', 'polish', 'pl_PL'),
'ckeditor' => NULL
),
'ru' => array(
'name' => 'Русский',
'folder' => 'russian',
'direction' => 'ltr',
'codes' => array('rus', 'russian', 'ru_RU'),
'ckeditor' => NULL
),
'cn' => array(
'name' => '繁體中文',
'folder' => 'chinese-simplified',
'direction' => 'ltr',
'codes' => array('cht', 'chinese-simplified', 'zh_CN'),
'ckeditor' => NULL
),
'zh' => array(
'name' => '繁體中文',
'folder' => 'chinese-traditional',
'direction' => 'ltr',
'codes' => array('cht', 'chinese-traditional', 'zh_TW'),
'ckeditor' => NULL
),
);
/*
* Default Language
*
* If no language is specified, which one to use?
* Currently: english (en) | german (de) | swissgerman (sw)
* spanish (es) | norwegian (no) | portuguese (pt)
* turkish (tr) | french (fr) | japanese (jp)
* polish (pl) | russian (ru)
* chinese-simplified (cn) | chinese-traditional (zh)
*
*/
$config['default_language'] = 'en';

View File

@ -0,0 +1,76 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed.');
/**
* Determines the language to use.
*
* This is called from the Codeigniter hook system.
* The hook is defined in application/config/hooks.php
*/
function pick_language() {
require APPPATH . '/config/language.php';
// Re-populate $_GET
parse_str($_SERVER['QUERY_STRING'], $_GET);
// If we've been redirected from HTTP to HTTPS on admin, ?session= will be set to maintain language
if ($_SERVER['SERVER_PORT'] == 443 and !empty($_GET['session'])) {
session_start($_GET['session']);
} else {
session_start();
}
// Lang set in URL via ?lang=something
if (!empty($_GET['lang'])) {
// Turn en-gb into en
$lang = strtolower(substr($_GET['lang'], 0, 2));
}
// Lang has already been set and is stored in a session
elseif (!empty($_SESSION['lang_code'])) {
$lang = $_SESSION['lang_code'];
}
// Lang has is picked by a user.
elseif (!empty($_COOKIE['lang_code'])) {
$lang = strtolower($_COOKIE['lang_code']);
}
// Still no Lang. Lets try some browser detection then
elseif (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// explode languages into array
$accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$supported_langs = array_keys($config['supported_languages']);
// Check them all, until we find a match
foreach ($accept_langs as $accept_lang) {
if (strpos($accept_lang, '-') === 2) {
// Turn pt-br into br
$lang = strtolower(substr($accept_lang, 3, 2));
// Check its in the array. If so, break the loop, we have one!
if (in_array($lang, $supported_langs)) {
break;
}
}
// Turn en-gb into en
$lang = strtolower(substr($accept_lang, 0, 2));
// Check its in the array. If so, break the loop, we have one!
if (in_array($lang, $supported_langs)) {
break;
}
}
}
// If no language has been worked out - or it is not supported - use the default
if (empty($lang) OR !array_key_exists($lang, $config['supported_languages'])) {
$lang = $config['default_language'];
}
// Whatever we decided the lang was, save it for next time to avoid working it out again
$_SESSION['lang_code'] = $lang;
// Load CI config class
$CI_config = & load_class('Config');
// Set the language config. Selects the folder name from its key of 'en'
$CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);
// Sets a constant to use throughout ALL of CI.
define('AUTO_LANGUAGE', $lang);
}