Welcome Guest, Not a member yet? Register   Sign In
MultiLanguage Support
#1

[eluser]Geneous[/eluser]
Hello,
I have developed one website using codeigniter.But now i want it should support multiple languages.i.e. If a user type text in marathi language in a forum then text should displayed in marathi language only. :bug:

If anybody having solution please let me know.


Thanks in advance.
#2

[eluser]Phil Sturgeon[/eluser]
Language detection based on input text is a NIGHTMARE. I would not head down that route as there are so many better ways you could do this.

Take a look at the "Pick Language" hook I use for PyroCMS. It will check against $_GET for ?lang=es, then check against session and cookie, then check to see if the browser is suggesting a language, then as a fallback it looks at the language.php config for a default.

This hook basically gives you a constant CURRENT_LANGUAGE which you can use throughout your entire application, so you know which language they are trying to view and can store that code against their posts so it will only show up to people viewing the same language.

Another handy feature of this hook is that it will set the CodeIgniter language to whatever language they are trying to view (as long as it is in the supported_langs config item) so it basically gives you FULL multi-lang support for your app. :-)
#3

[eluser]MrCevic[/eluser]
I tried to implement your pick_language hook but something is not working. I tried to reverse-engineer the way you implemented in PyroCMS. This is what I've done:

- placed pick_language.php in "hooks"
- in "config/config.php" I set "$config['enable_hooks'] = TRUE" and just in case added this at the end:
Code:
function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'libraries/'. $class . EXT );
    }
}

- added this to "config/hooks.php"

Code:
$hook['pre_controller'][] = array(
    'function' => 'pick_language',
    'filename' => 'pick_language.php',
    'filepath' => 'hooks'
);

- create "config/language.php", with this code:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['supported_languages'] = array(
    'en'=> array('name' => 'English', 'folder' => 'english'),
    'es'=> array('name' => 'Español', 'folder' => 'spanish'),
    'fr'=> array('name' => 'Français', 'folder' => 'french'),
);

$config['default_language'] = 'en';
?>

- I create "helper/MY_url_helper.php" with this code:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

if ( ! function_exists('url_title'))
{
    function url_title($str, $separator = 'dash', $lowercase = FALSE)
    {
        if ($separator == 'dash')
        {
            $search        = '_';
            $replace    = '-';
        }
        else
        {
            $search        = '-';
            $replace    = '_';
        }

        $trans = array(
                        '&\#\d+?;'                => '',
                        '&\S+?;'                => '',
                        '\s+'                    => $replace,
                        '[^a-zАБВГҐДЕЄЁЖЗИІЫЇЙКЛМНОПРСТУФХЦЧШЩэЮЯЬЪабвгґдеєёжзиіыїйклмнопрстуфхцчшщюяьъ0-9\-\._]' => '',
                        $replace.'+'            => $replace,
                        $replace.'$'            => $replace,
                        '^'.$replace            => $replace,
                        '\.+$'                    => ''
                      );

        $str = strip_tags($str);

        foreach ($trans as $key => $val)
        {
            $str = preg_replace("#".$key."#i", $val, $str);
        }

        if ($lowercase === TRUE)
        {
            if( function_exists('mb_convert_case') )
            {
                $str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8");
            }
            
            else
            {
                $str = strtolower($str);
            }
        }
        
        return trim(stripslashes($str));
    }
}

function shorten_url($url = '')
{
    $CI =& get_instance();
    
    $CI->load->library('curl');

    if(!$url)
    {
        $url = site_url($CI->uri->uri_string());
    }
    
    // If no a protocol in URL, assume its a CI link
    elseif(!preg_match('!^\w+://! i', $url))
    {
        $url = site_url($url);
    }

    return $CI->curl->simple_get('http://tinyurl.com/api-create.php?url='.$url);
}
?>

- in "config/autoload.php" I set the following

Code:
$autoload['helper'] = array('language', 'url');
$autoload['config'] = array('language');

Then in the view, I use this snippet to allow the user to change the language:
Code:
<form action="<?php echo site_url(uri_string()); ?>" id="change_language" method="get">
        <select name="lng">
        &lt;?php foreach($this->config->item('supported_languages') as $key => $lang): ?&gt;
            <option value="&lt;?php echo $key; ?&gt;" &lt;?php echo CURRENT_LANGUAGE == $key ? 'selected="selected"' : ''; ?&gt;>
            &lt;?php echo $lang['name']; ?&gt;
            </option>
            &lt;?php endforeach; ?&gt;
        </select>
    &lt;/form&gt;

- and finally I add this Jquery code to the body:
Code:
$(document).ready(function(){

    $('form#change_language select').change(function(){
    $(this).parent('form').submit();
    });

});

- I also make sure to have a lang file for each supported language.

The dropdown seems to be working. When I use it I can see that whatever choice I make is reflected in the url (i.e. "/?lang=fr") but the app doesn't seem to be picking up the change. I suppose it's a session/cookie related problem but I don't seem to be able to solve it. Can you please help?

Quote:Update: I wasn't able to get any feedback on this issue, but I ended up finding a solution: the language selection hook works together with the allow query string hook AND (and this is where I found the problem) the .htaccess file, where you need to make sure to have this rewrite rule "RewriteRule ^(.*)$ index.php?/$1 [L]" with the question mark (this assuming that you use Friendly URLs and PHP5.




Theme © iAndrew 2016 - Forum software by © MyBB