1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
<?php defined('BASEPATH') or exit('No direct script access allowed');
class MY_Lang extends CI_Lang
{
// current language, obtained from first segment of URI
public $current = '';
// translations of strings to avoid the problem that once a language is loaded it's
// not possible to access other language translations
// id -> dest lang -> word in other lang
protected $translations_from_id = array(
'news' => array(
'en' => 'news',
'es' => 'noticias'
),
'article' => array(
'en' => 'article',
'es' => 'noticia'
)
);
// inversion of the previous array. It's created dinamically to minimize user errors
// origin lang -> word in other language -> id
protected $translations_to_id = array();
public function __construct()
{
parent::__construct();
// create inversion of $translations_from_id
$this->_invert_translations();
$URI =& load_class('URI', 'core');
$CONFIG =& load_class('Config', 'core');
// get URI segment that should define the language
$lang_uri = $URI->segment(1);
// default language, only for home page
if ($lang_uri === null) {
$lang_uri = $CONFIG->item('language_abbr');
}
// save language if it's valid (has been defined in config file)
if (in_array($lang_uri, $CONFIG->item('languages_abbr'))) {
$this->current = $lang_uri;
$CONFIG->set_item('language', $CONFIG->item('lang_uri_abbr')[$lang_uri]);
} elseif (! is_cli()) {
show_404();
}
}
/**
* Returns site_url() of provided URI adding to its beginning language and
* first segment translation
*
* @uses site_url helper (CI_Config alias)
*
* @param string|string[] $uri URI string or an array of segments
* @param string $lang language to which translate URI first segment
* @param string $protocol
* @return string
*/
public function site_url_lang($uri = '', $lang = '', $protocol = null)
{
// if lang not provided use current
$lang = $lang ? $lang : $this->current;
$uri = ltrim($uri, '/');
if ($uri) {
// separate first segment from the rest of the URI
preg_match("/^([^\/]*)(\/.*)?$/", $uri, $m);
$first_segment = $m[1];
$rest = isset($m[2]) ? $m[2] : '';
// Translate first segment to current language
$translated_first_segment = $this->translate_from_id($first_segment, $lang);
if($translated_first_segment) {
$first_segment = $translated_first_segment;
}
// add possibly modified first segment to rest of URI
$uri = $first_segment.$rest;
}
// add language to beginning of URI
$uri = $lang.'/'.$uri;
return site_url($uri, $protocol);
}
/**
* Returns redirect() of provided URI using site_url_lang to get the right URI
*
* @param string|string[] $uri URI string or an array of segments
* @param string $lang language to which translate URI first segment
* @param string $method Redirect method (‘auto’, ‘location’ or ‘refresh’)
* @param string $code HTTP Response code (usually 302 or 303)
* @return string
*/
public function redirect_lang($uri = '', $lang = '', $method = 'auto', $code = NULL)
{
redirect(site_url_lang($uri, $lang), $method, $code);
}
/**
* Translates string from $orig_lang to $dest_lang
*
* Returns translated string or false if translation not found
*
* @param string $line string to translate
* @param string $lang
* @return mixed string or false
*/
public function translate($line, $orig_lang, $dest_lang)
{
// get the id from the translations_to_id array
if (isset($this->translations_to_id[$orig_lang][$line])) {
$id = $this->translations_to_id[$orig_lang][$line];
} else {
return false;
}
// return translated value from translations_from_id
return isset($this->translations_from_id[$id][$dest_lang]) ?
$this->translations_from_id[$id][$dest_lang] :
false;
}
/**
* Translates string from given lang to identifier
*
* Returns translated string or false if translation not found
*
* @param string $line string to translate
* @param string $lang
* @return mixed string or false
*/
public function translate_to_id($line, $lang)
{
return isset($this->translations_to_id[$lang][$line]) ?
$this->translations_to_id[$lang][$line] :
false;
}
/**
* Translates string from identifier to given lang
*
* Returns translated string or false if translation not found
*
* @param string $id string id to translate
* @param string $lang
* @return mixed string or false
*/
public function translate_from_id($id, $lang)
{
return isset($this->translations_from_id[$id][$lang]) ?
$this->translations_from_id[$id][$lang] :
false;
}
/**
* Translates string
*
* Returns translated string or false if translation not found
*
* @param string $line string to translate
* @param string $lang
* @param array $translations translations array (lang->origin->destination)
* @return mixed string or false
*/
private function _multi_lang_translate($line, $lang, $translations)
{
return isset($translations[$lang][$line]) ?
$translations[$lang][$line] :
false;
}
/**
* Creates inversion of $translations_from_id
*/
private function _invert_translations()
{
foreach ($this->translations_from_id as $id => $translation) {
foreach ($translation as $lang => $word) {
$this->translations_to_id[$lang][$word] = $id;
}
}
}
}