Fix language detection for gettext
This commit is contained in:
@@ -17,6 +17,5 @@ $hook['post_controller_constructor'] = array(
|
|||||||
'filepath' => 'hooks'
|
'filepath' => 'hooks'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/* End of file hooks.php */
|
/* End of file hooks.php */
|
||||||
/* Location: ./application/config/hooks.php */
|
/* Location: ./application/config/hooks.php */
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
<?php defined('BASEPATH') || exit("No direct script access allowed");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mapping browser's primary language ID to supported language directory.
|
|
||||||
*/
|
|
||||||
$config['lang_available'] = array(
|
|
||||||
'en' => 'english',
|
|
||||||
'en-uk' => 'english',
|
|
||||||
'de' => 'german',
|
|
||||||
'de-at' => 'german'
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default language code. This language MUST be supported!
|
|
||||||
*/
|
|
||||||
$config['lang_default'] = 'en';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Selected language code (is set by the language detection).
|
|
||||||
*/
|
|
||||||
$config['lang_selected'] = 'en';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The name you want for the cookie.
|
|
||||||
*/
|
|
||||||
$config['lang_cookie_name'] = 'lang_select_language';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of seconds you want the language to be remembered.
|
|
||||||
*/
|
|
||||||
$config['lang_expiration'] = 63072000;
|
|
||||||
32
application/config/language.php
Normal file
32
application/config/language.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php defined('BASEPATH') || exit('No direct script access allowed');
|
||||||
|
|
||||||
|
/*
|
||||||
|
| -------------------------------------------------------------------
|
||||||
|
| Supported Languages
|
||||||
|
| -------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Contains all languages your site will store data in. Other
|
||||||
|
| languages can still be displayed via language files, that's totally
|
||||||
|
| different.
|
||||||
|
|
|
||||||
|
| Check for HTML equivalents for characters such as <20> with the URL
|
||||||
|
| below:
|
||||||
|
|
|
||||||
|
| http://htmlhelp.com/reference/html40/entities/latin1.html
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
$config['supported_languages'] = array(
|
||||||
|
'en'=> array('name' => 'English', 'locale' => 'en_US'),
|
||||||
|
'de'=> array('name' => 'Deutsch', 'locale' => 'de_DE')
|
||||||
|
);
|
||||||
|
|
||||||
|
/*
|
||||||
|
| -------------------------------------------------------------------
|
||||||
|
| Default Language
|
||||||
|
| -------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| If no language is detected, which one to use? Must be in the
|
||||||
|
| array above.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
$config['default_language'] = 'en';
|
||||||
@@ -18,8 +18,6 @@ class MY_Lang extends CI_Lang {
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->_gettext_domain = 'lang';
|
$this->_gettext_domain = 'lang';
|
||||||
log_message('debug', "Gettext Class Initialized");
|
log_message('debug', "Gettext Class Initialized");
|
||||||
|
|
||||||
$this->load_gettext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,30 +2,31 @@
|
|||||||
|
|
||||||
class Lang_detect {
|
class Lang_detect {
|
||||||
|
|
||||||
protected $CI;
|
private $CI;
|
||||||
|
|
||||||
// make config item available locally
|
private $supportedLanguages = array();
|
||||||
public $languages = array();
|
|
||||||
|
|
||||||
// the user's language (directory name)
|
private $language = '';
|
||||||
public $lang_dir = '';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->CI =& get_instance();
|
$this->CI =& get_instance();
|
||||||
$this->CI->load->config('lang_detect');
|
$this->CI->load->config('language');
|
||||||
$this->CI->load->helper('cookie');
|
$this->CI->load->helper('cookie');
|
||||||
|
|
||||||
|
log_message('debug', "Lang_detect Class Initialized");
|
||||||
|
|
||||||
// get list of supported languages
|
// get list of supported languages
|
||||||
$this->languages = $this->CI->config->item('lang_available');
|
$this->supportedLanguages = $this->CI->config->item('supported_languages');
|
||||||
if (empty($this->lang_dir)) {
|
|
||||||
// language directory not yet set: detect the user's language
|
if (empty($this->language)) {
|
||||||
$this->lang_dir = $this->detectLanguage();
|
// language not yet set: detect the user's language
|
||||||
|
$this->language = $this->detectLanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
log_message('debug', "Lang_detect Class Initialized");
|
$this->CI->lang->load_gettext($this->CI->config->item('language'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,74 +39,46 @@ class Lang_detect {
|
|||||||
* @return string Language directory name, e.g. 'english'
|
* @return string Language directory name, e.g. 'english'
|
||||||
*/
|
*/
|
||||||
public function detectLanguage() {
|
public function detectLanguage() {
|
||||||
$language = false;
|
for ($i = $this->CI->uri->total_segments(); $i > 0; $i--) {
|
||||||
|
$segment = $this->CI->uri->segment($i);
|
||||||
// obtain language code from URI segment if available
|
if (strlen($segment) == 2 && array_key_exists($segment, $this->supportedLanguages)) {
|
||||||
$langURI = $this->uriLanguageDetect();
|
$lang = $segment;
|
||||||
if ($langURI !== false) {
|
$this->CI->session->set_userdata('language', $lang);
|
||||||
// check the URI's language code
|
|
||||||
$language = $this->checkLanguage($langURI);
|
|
||||||
if ($language !== false) {
|
|
||||||
$lang = $langURI;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if a language cookie available get its sanitized info
|
if ($this->CI->session->userdata('language')) {
|
||||||
$langCookie = get_cookie($this->CI->config->item('lang_cookie_name'));
|
$lang = $this->CI->session->userdata('language');
|
||||||
if ($langCookie !== false) {
|
} else if (get_cookie('language') !== false) {
|
||||||
if (($language !== false) && ($langURI !== $langCookie)) {
|
$lang = get_cookie('language');
|
||||||
// URI has valid language but cookie has wrong language: update cookie
|
} else if ($this->CI->input->server('HTTP_ACCEPT_LANGUAGE')) {
|
||||||
$this->setLanguageCookie($langURI);
|
// explode languages into an array
|
||||||
}
|
$accept_langs = explode(',', $this->CI->input->server('HTTP_ACCEPT_LANGUAGE'));
|
||||||
if ($language === false) {
|
|
||||||
// invalid or no URI language code: check the cookie's language
|
|
||||||
$language = $this->checkLanguage($langCookie);
|
|
||||||
if ($language !== false) {
|
|
||||||
$lang = $langCookie;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($language === false) {
|
log_message('debug', 'Checking browser languages: ' . implode(', ', $accept_langs));
|
||||||
// no cookie/URI language code: check browser's languages
|
|
||||||
$accept_langs = $this->CI->input->server('HTTP_ACCEPT_LANGUAGE');
|
|
||||||
if ($accept_langs !== false) {
|
|
||||||
// explode languages into array
|
|
||||||
$accept_langs = strtolower($accept_langs);
|
|
||||||
$accept_langs = explode(",", $accept_langs);
|
|
||||||
|
|
||||||
// check all of them
|
// check them all, until a match is found
|
||||||
foreach ($accept_langs as $lang) {
|
foreach ($accept_langs as $lang) {
|
||||||
// remove all after ';'
|
$lang = strtolower(substr($lang, 0, 2));
|
||||||
$pos = strpos($lang, ';');
|
|
||||||
if ($pos !== false) {
|
if (in_array($lang, array_keys($this->supportedLanguages))) {
|
||||||
$lang = substr($lang, 0, $pos);
|
|
||||||
}
|
|
||||||
// get CI language directory
|
|
||||||
$language = $this->checkLanguage($lang);
|
|
||||||
// finish search if we support that language
|
|
||||||
if ($language !== false) {
|
|
||||||
// set cookie
|
|
||||||
$this->setLanguageCookie($lang);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if no valid language has been detected, use the default
|
||||||
|
if (empty($lang) || !in_array($lang, array_keys($this->supportedLanguages))) {
|
||||||
|
$lang = $this->CI->config->item('default_language');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($language === false) {
|
// save the selected language to avoid detecting it again
|
||||||
// no base language available or no browser language match: use default
|
$this->CI->session->set_userdata('language', $lang);
|
||||||
$lang = $this->CI->config->item('lang_default');
|
|
||||||
$language = $this->languages[$lang];
|
|
||||||
// set cookie
|
|
||||||
$this->setLanguageCookie($lang);
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the configuration for the CI_Language class
|
// set the language config
|
||||||
$this->CI->config->set_item('language', $language);
|
$this->CI->config->set_item('language', $this->supportedLanguages[$lang]['locale']);
|
||||||
// store the language code too
|
|
||||||
$this->CI->config->set_item('lang_selected', $lang);
|
return $lang;
|
||||||
return $language;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,50 +87,9 @@ class Lang_detect {
|
|||||||
* @param string $lang The language code, e.g. en
|
* @param string $lang The language code, e.g. en
|
||||||
*/
|
*/
|
||||||
private function setLanguageCookie($lang) {
|
private function setLanguageCookie($lang) {
|
||||||
set_cookie($this->CI->config->item('lang_cookie_name'), $lang,
|
set_cookie($this->CI->config->item('language_cookie_name'), $lang, $this->CI->config->item('language_expiration'));
|
||||||
$this->CI->config->item('lang_expiration'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches the language code from URI segment if available.
|
|
||||||
*
|
|
||||||
* @return mixed The language code, or FALSE if not found.
|
|
||||||
*/
|
|
||||||
private function uriLanguageDetect() {
|
|
||||||
// search the language code in the uri segments
|
|
||||||
for ($i = $this->CI->uri->total_segments(); $i > 0; $i--) {
|
|
||||||
$segment = $this->CI->uri->segment($i);
|
|
||||||
// the uri segment with the language code has the prefix 'l_'
|
|
||||||
if (strlen($segment) == 2 && array_key_exists($segment, $this->languages)) {
|
|
||||||
return $segment;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines the language directory.
|
|
||||||
*
|
|
||||||
* @param string $lang The language code, e.g. en_uk
|
|
||||||
* @return string The language directory, or FALSE if not found.
|
|
||||||
*/
|
|
||||||
private function checkLanguage(&$lang) {
|
|
||||||
if (!array_key_exists($lang, $this->languages)) {
|
|
||||||
if (strlen($lang) == 2) {
|
|
||||||
// we had already the base language: not found so give up
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
// try base language
|
|
||||||
$lang = substr($lang, 0, 2);
|
|
||||||
if (!array_key_exists($lang, $this->languages)) {
|
|
||||||
// calculated base language also not found: give up
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// return CI language directory
|
|
||||||
return $this->languages[$lang];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* End of file Lang_detect.php */
|
/* End of file Lang_detect.php */
|
||||||
|
|||||||
Reference in New Issue
Block a user