Switching contents language in CodeIgniter |
[eluser]KeyStroke[/eluser]
Hi, I'm going to use CI's language class to make a site that supports English and Arabic. I have two questions about that though: 1) How do you usually separate both versions of the site? for example: is it like ar.yourdomain.com/home (ar = Arabic), or yourdomain.com/ar/home? or maybe another way? 2) How do you swap languages within the application? Do you set a global language variable in each PHP file or a bunch of IF statements throughout the program or how exactly? You help is much appreciated ![]()
[eluser]gon[/eluser]
Hi, Quote:1) How do you usually separate both versions of the site? I use the second option. Quote:2) How do you swap languages within the application? Do you set a global language variable in each PHP file or a bunch of IF statements throughout the program or how exactly? One of the first things you should do in every controller is get the language from the first url segment. You should try to do this automatically. For example extending all controllers from another one that do this in the constructor. And keep the language in a global var, ie $language. You could then use that language var in the SQL queries, or elsewhere, but there should be no IFs because all the code should be the same. Just using a different value on $language var. For the actual translation, you can use the language library that CI provides, or just use keys in the views and doing replacements after rendering the view. For example: <title>__TITLE__</title> And after getting the view, execute a function that seeks for __KEY__ in the HTML, gets the KEY name, and looks for the translation. The translation values can be taken from an array: $i18n['TITLE']['en'] = "english title"; $i18n['TITLE']['ar'] = "arabic title"; Actually the CI language class works just this way. regards
[eluser]KeyStroke[/eluser]
Thanks a lot gon! I have two more questions if you don't mind: 1) In language files, should English and Arabic array keys be different from each other (e.g: $lang['welcome_en'] and $lang['welcome_ar'])? or are they supposed to use the same key name since they're in different files? 2) How do I use the 'yourdomain.com/ar/home' URI structure when home is my default controller? I don't know how that would work with other controllers either. Appreciate your help ![]()
[eluser]gon[/eluser]
Quote:1) In language files, should English and Arabic array keys be different from each other (e.g: $lang[’welcome_en’] and $lang[’welcome_ar’])? or are they supposed to use the same key name since they’re in different files? Using CI language files, you generate 2 files with the same name, and store them in its folder: arabic/mytranslations.php english/mytranslations.php Read the user guide for details on loading the correct file. Quote:2) How do I use the ‘yourdomain.com/ar/home’ URI structure when home is my default controller? I don’t know how that would work with other controllers either. Sometimes I extend the CI URI library to make it work the way I want. For example, store the first uri segment in the $language var, and consider the second segment to be the first, so controllers will load as usual. But you can try using .htaccess rules, or CI routing. Good luck
[eluser]KeyStroke[/eluser]
So you mean I should detect the language first in the URI and load the matching file in each controller?
[eluser]sikkle[/eluser]
gon : is it possible for you to send me sample of what you do (extend the ci uri libs) ? private or new thread whatever ![]()
[eluser]gon[/eluser]
Hi, I've reviewed the code. The class that gets extended is Router. Also, you'll see that some configuration params must be set. Don't have time to explain further!!!! hope this helps. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
[eluser]Блум[/eluser]
I found a different and IMHO a very easy and harmless way to have a language segment in your URL (http://yoursite.com/en/news/54) without changing the config options. The good thing is that you do not have to have Code: $config['uri_protocol'] = "ORIG_PATH_INFO"; Code: $config['enable_query_strings'] = TRUE; All you have to do is: 1. .htacces Code: RewriteEngine on 2. In index.php (in the root of your project) define a constant from the language query in the URL before CI spread it away. Code: <?php So now you can use the current LANG constant everywhere in your project, and URI Class is working the same: On url: http://yoursite.com/bg/news/42 Code: echo 'Lang:'.LANG."; ";
[eluser]wiredesignz[/eluser]
Or you could use the URI Language Identifier extension. http://codeigniter.com/wiki/URI_Language_Identifier/
|
Welcome Guest, Not a member yet? Register Sign In |