Last active
June 22, 2016 18:38
-
-
Save ipimpat/21e2641147a1d8edae9a to your computer and use it in GitHub Desktop.
Simple PyroCMS module (event) that sets the language based on first uri segment, so that content stored under e.g. /da will make PyroCMS (CI) run as "danish". Create a folder in any "module" directory named i18n and add these files, and install module using the admin panel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') or exit('No direct script access allowed'); | |
/** | |
* i18n details file | |
* | |
* @author Kim Henriksen <kh@ipimp.at> | |
* @package PyroCMS | |
* @subpackage i18n Module | |
* @category module class | |
*/ | |
class Module_i18n extends Module | |
{ | |
public $version = '1.0.0'; | |
public function info() | |
{ | |
return array( | |
'author' => 'Kim Henriksen', | |
'name' => array( | |
'en' => 'i18n' | |
), | |
'description' => array( | |
'en' => 'Kim Henriksen i18n Module.' | |
), | |
'frontend' => FALSE, | |
'backend' => FALSE, | |
); | |
} | |
public function install() | |
{ | |
return TRUE; | |
} | |
public function uninstall() | |
{ | |
return TRUE; | |
} | |
public function upgrade($old_version) | |
{ | |
// Your Upgrade Logic | |
return TRUE; | |
} | |
public function help() | |
{ | |
// Return a string containing help info | |
// You could include a file and return it here. | |
return "Some Help Stuff"; | |
} | |
} | |
/* End of file details.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
/** | |
* i18n Events Class | |
* | |
* @package PyroCMS | |
* @subpackage i18n Module | |
* @category events | |
* @author Kim Henriksen <kh@ipimp.at> | |
*/ | |
class Events_i18n | |
{ | |
/** | |
* CodeIgniter | |
* @var type | |
*/ | |
protected $CI; | |
/** | |
* Language config | |
* @var array | |
*/ | |
protected $config; | |
/** | |
* Event constructor | |
* registers this event | |
*/ | |
public function __construct() | |
{ | |
// We don't wan't to mess around with /admin panel | |
if (preg_match('@\/admin(\/.+)?$@', $_SERVER['REQUEST_URI'])) | |
{ | |
return; | |
} | |
$this->CI = & get_instance(); | |
// Load helper | |
$this->CI->load->helper('i18n/i18n'); | |
// register the public_controller event when this file is autoloaded | |
Events::register('public_controller', array($this, 'run')); | |
require APPPATH . '/config/language.php'; | |
$this->config = $config; | |
} | |
/** | |
* Event run | |
*/ | |
public function run() | |
{ | |
// Get language code segment | |
$code = $this->_get_language_code_segment(); | |
// If the current CI language (CURRENT_LANGUAGE) is not equal | |
// to the the first URI segment we are under | |
// override session language code and refresh the page. | |
if ($this->_is_valid_language_code($code) AND CURRENT_LANGUAGE !== $code) | |
{ | |
change_language($code); | |
} | |
} | |
/** | |
* Check if the code is a valid language code, and is supported | |
* @param string $code | |
* @return boolean | |
*/ | |
protected function _is_valid_language_code($code) | |
{ | |
if (strlen($code) === 2 AND array_key_exists($code, $this->config['supported_languages'])) | |
{ | |
return TRUE; | |
} | |
return FALSE; | |
} | |
/** | |
* Returns language code based on first uri segment | |
* if no valid language could be found in first segment (or it doesn't exist) | |
* the default language code is returned | |
* @return string | |
*/ | |
protected function _get_language_code_segment() | |
{ | |
// Get the first uri segment | |
// If it's exactly to chars long, we treat it as a language segment | |
// (if the first uri segment doesn't exist, the default language is used | |
if (strlen($code = $this->CI->uri->segment(1, $this->config['default_language'])) === 2) | |
{ | |
return $code; | |
} | |
// If the first segment is not of 2 chars lenght, we imagine that | |
// the first segment is the default language string | |
else | |
{ | |
return $this->config['default_language']; | |
} | |
} | |
} | |
/* End of file events.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('change_language')) | |
{ | |
/** | |
* Overwrites the session lang code and reloads the (current) url | |
* also preserves query strings if any | |
* @param string $code | |
* @param string $url optional url instead of current url | |
*/ | |
function change_language($code, $url = NULL) | |
{ | |
$CI = & get_instance(); | |
// Overwrite the language session code | |
// which is created by the pick_language hook | |
$_SESSION['lang_code'] = $code; | |
if (empty($url)) | |
{ | |
$base = $CI->config->site_url($CI->uri->uri_string()); | |
$url = $_SERVER['QUERY_STRING'] ? $base . '?' . $_SERVER['QUERY_STRING'] : $base; | |
} | |
// Refresh the page | |
redirect($url, 'location'); | |
} | |
} |
We have an english frontpage, we just slugged it "en/"
and then we added every english page as a child of that page
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi kimse1!
I have a question.
How the pages in the admin side of PyroCMS should be organized in order to have multi-language dynamic content. I like your example at http://viptel.dk/ but do not understand how the pages should be organized.
Unfortunately PyroCMS doesn't allows slashes in the slugs to have something like .../en/....Or I am on the wrong path?
Any help will be appreciated.
Thanks in advance!
.