Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Multi-Language, Multi-Version
#1

[eluser]Vasile Magaru[/eluser]
Hi, I want to share with you a little add-on for CI.
I spend time searching for a way to create a multiple language site with a mobile and desktop interface, but I didn't find any add-on that can do this like I wanted. So here it is, with the explanations below. The files are attached to this post.
I named the add-on eklypse.

CodeIgniter's Eklypse Add-on

Installation
1. Extract the content of the archive;
2. Copy the content extracted to your CI folder;
3. Replace every files that you are prompted to do it;
4. Edit the system/application/config/config.php file after your needs;
* I recommend you to set an encryption key, since will use CI's sessions;
5. Now, open the system/application/config/autoload.php file and for the libraries, set the session library to be auto-loaded. And for the helpers, set the language helper, url helper and the core helper to be auto-loaded. The core helper is a part of the eklypse add-on. Since you'll use multiple languages and versions, you will need this loaded at the start to provide information to the pages.
6. Open system/application/config/eklypse.php file, that is the configuration file for this add-on. Here you'll have some options that you need to configure after your needs;
a) $config['eklypse_multivers'] = TRUE; - set this to TRUE if you want to use multiple versions;
b) $config['eklypse_multilang'] = TRUE; - set this to TRUE if you want to use multiple languages;
c) $config['eklypse_arrayoutput'] = '<p>{content}</p>'; - will be explained bellow;
d) $config['eklypse_version'] = 'desktop'; - the default version of the site;
e) $config['eklypse_language'] = 'english'; - the default language of the site;
f) Routes will be explained bellow;
g) $config['eklypse_iphone_browsers'] = 'iphone'; - the version that an iphone will see;
h) $config['eklypse_mobile_browsers'] = 'mobile'; - the version that regular mobiles will see;
i) $config['eklypse_smartphone_browsers'] = 'smartphone'; - the version that a smartphone will see;
j) $config['eklypse_robot_browsers'] = 'desktop'; - the version that any robots will see;
7. In your application directory, the views folder, create a folder for every site version you want to use;
8. In your application directory, the language folder, create a folder for every language you want to use;
* This steps are necessarily if you want the application to function properly for multiple versions and languages. If you set in the system/application/config/eklypse.php file not to use any of the multiple options you must not do the steps 7 and 8;

Routes
This add-on, creates 2 controllers, one that will set the language and one that will set the version by the user's choice.
So, in the system/application/config/routes.php file, you must set some routes.
This is the default options, if you want to tweak them, feel free to do it.
Code:
$route['version-(desktop|iphone|mobile|smartphone)'] = 'version';
$route['language-(english|german|italian|romanian)'] = 'language';
The first rule take an url like http://yoursite.com/version-mobile and set the mobile version for the user.
In the system/application/config/eklypse.php file, you must set the routes options, like in the system/application/config/routes.php file.
Code:
$config['eklypse_langroute'] = '/language-{content}';
$config['eklypse_versroute'] = '/version-{content}';
For example, if you have:
$route['version-(mobile|desktop)'] = 'version'; in the routes.php
here you must write:
$config['eklypse_versroute'] = '/version-{content}';

Another example:
$route['site/version/set-(mobile|desktop)'] = 'version'; in the routes.php
set this in the eklypse.php
$config['eklypse_versroute'] = '/site/version/set-{content}';

And the last one:
$route['switch/version-(mobile/desktop)-device/user'] = 'version'; in the routes.php
set this in the eklypse.php
$config['eklypse_versroute'] = '/switch/version-{content}-device/user';

Do the same thing for the language route;
The {content} reference will be replaced with the value passed by the user, ex. mobile, english, desktop, italian, etc...
The language and version controllers, will find these values bassed on your routes model.

Helper's functions
1. eklypse_multivers(); - takes no parameter, returns an array with all the versions used;
2. eklypse_multilang(); - takes no parameter, returns an array with all the languages used;
3. eklypse_version(); - returns a string with the site version used or detected for the user;
4. eklypse_language(); - returns a string with the site language used or detected for the user;
* The last 2 are the main functions that makes the system work;

The eklypse_print function
Code:
eklypse_print($array_name = '', $array_var = array(), $eklypse_default = TRUE, $output_def = '{content} ');
The eklypse_print function from the engine helper is a powerfull function that you can use everywhere in your project, but is especially designed for the language and version arrays.
The $array_name is used with 2 values, language or version. For any other values nothing will happen.
#2

[eluser]Vasile Magaru[/eluser]
The $array_var is an array that can be used optionaly. Be carefull, if you use $array_name with one of the 2 values and the $array_var, the $array_name will not take any effect on the result, just in case you pass an empty $array_var. So, use $array_name or $array_var. The $array_var was designed to be used for personal use of the function and not for the default use.
The $eklypse_default can be set to TRUE of FALSE. If TRUE, then the $output_def will always be equal with the one defined in the configuration file. So if you want to use the function for personal use, set $eklypse_default to FALSE and configure the next parameter, $output_def.
You can also set $eklypse_default to false and $array_name to one of the 2 values to create a more personal output of the versions or languages of the site.
The function will always return a string or an empty string.
For more tweaking, use the fourth variable, $output_def. The default output that is also configurable, will just put every content from the array in a new paragraph. If you configure this, then maybe you'll not get the desired result for the default language or version output.
So, fell free to use $output_def, but dont forget to set the $eklypse_default to FALSE.

More examples:

Code:
&lt;?php echo eklypse_print('version', NULL, FALSE, '[<a href="version-{content}">{content}</a>]&nbsp;'); ?&gt;

will return:

Code:
[<a href="version-desktop">desktop</a>]&nbsp;[<a href="version-iphone">iphone</a>]&nbsp;[<a href="version-mobile">mobile</a>]&nbsp;[<a href="version-smartphone">smartphone</a>]&nbsp;



Code:
&lt;?php echo eklypse_print('version', NULL, FALSE); ?&gt;

will return:

Code:
desktop iphone mobile smartphone



Code:
&lt;?php echo eklypse_print('', array('male', 'alien', 'female'), FALSE, '<p>{content}</p>'); ?&gt;

will return:

Code:
<p>male</p><p>alien</p><p>female</p>

Probably you have seen the {content} reference. This will be replaced with every value of the array. So in the $output_def, put {content} wherever you want the value from the array.

The end
All you got to do now, is to use the eklypse_language(); and eklypse_version(); in your controller.
Code:
&lt;?php

class Main extends Controller {

    function Main()
    {
        parent::Controller();    
    }
    
    function index()
    {        
        $this->lang->load('general', eklypse_language());    
        $this->load->view(eklypse_version().'/main_view');
    }
    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

You can find a working demo here: http://eklypse.mobitag.ro

That's it, for any feedback, please contact me or use this thread.
#3

[eluser]gyo[/eluser]
I like this invisible seamless integration, but I have one question though: how can you let the search engines to crawl all the languages, if the URIs all look the same?

Thanks!
#4

[eluser]Vasile Magaru[/eluser]
I don't know how robots work, just the basic things, so I hope I'll not say something stupid.
If crawlers are searching pages using different ip's for different regions, we can tweak the code a little more and redirect a romanian crawler to the romanian pages.
So maybe geo locations can help.
Also a sitemap may be usefull?!?
And custom meta information for every language.
Sorry for my lack of knowlegde in this, but if you find my ideas good to go in practice, please let us know.
#5

[eluser]Buso[/eluser]
Usually SEO guys prefer URI languages..

Personally, I think I'm done with that. Our sites shouldn't be built for robots, robots should be improved.
The more unnecessary info in the url, the uglier it looks.
I'm ok with meaningful urls though, like /ci-multi-language, because with that the user can tell what the link is about before clicking on it.
#6

[eluser]gyo[/eluser]
I'm not really a SEO guy, but I understand that search engines optimization is (almost) necessary.
I agree that robots should be improved, but you can wait for them to improve and ignore SEO, or work with what you have.

With that said, if the same URI can be in different languages, it's impossible for the search-engines to link to a specific language. The same is true if you want to link to a specific resource in a different language.

Personally I'm a fan of sub-domains for languages, like http://en.somedomain.com.
Still a handsome url, isn't it? Wink
#7

[eluser]Vasile Magaru[/eluser]
Thanks to Buso's suggestion, I changed the license for the add-on so that you can use, edit or copy at will.
The files are attached to the first post.




Theme © iAndrew 2016 - Forum software by © MyBB