Read this in other languages:
Language code in URL:
- jorider.com/en/about
- jorider.com/ar/about
The CodeIgniter Language Class is used in order to fetch and display (directly in the HTML) the appropriate text defined in the respective language file.
View file
<p><?php echo lang('I\'m a man')?></p>
English language file
$lang['I\'m a man'] = "I'm a man";
Arabic language file
$lang['I\'m a man'] = "انا رجل";
Result with jorider.com/en/about
<p>I'm a man</p>
Result with jorider.com/ar/about
<p>انا رجل</p>
Before using this library, please read and learn about the CodeIgniter Language Class concepts and methods.
- Clone repository;
- Place the
MY_Lang.php
andMY_Config.php
files together in theapplication/core
folder.
In application/config/routes.php add:
// example: '/en/about' -> use controller 'about'
$route['^en/(.+)$'] = "$1";
$route['^ar/(.+)$'] = "$1";
// '/en' and '/ar' -> use default controller
$route['^(en|ar)$'] = $route['default_controller'];
Other routing examples:
$route['^(en|ar)/contact'] = "pages/contact";
$route['^(en|ar)/privacy-policy$'] = "pages/index/privacy_policy";
$route['^(en|ar)/terms-of-use$'] = "pages/index/terms_of_use";
Important Note: make sure to add other routs before wildcards regular expression routes above:
$route['^(en|ar)/contact'] = "pages/contact";
$route['^(en|ar)/privacy-policy$'] = "pages/index/privacy_policy";
$route['^(en|ar)/terms-of-use$'] = "pages/index/terms_of_use";
// example: '/en/about' -> use controller 'about'
$route['^en/(.+)$'] = "$1";
$route['^ar/(.+)$'] = "$1";
// '/en' and '/ar' -> use default controller
$route['^(en|ar)$'] = $route['default_controller'];
-
application/language/english/about_lang.php
<?php $lang['I\'m a man'] = "I'm a man";
-
application/language/arabic/about_lang.php
<?php $lang['I\'m a man'] = "انا رجل";
application/controllers/about.php
<?php
class About extends CI_Controller {
function index()
{
// you might want to just autoload these two helpers
$this->load->helper(['language', 'url']);
// load language file
$this->lang->load('about');
$this->load->view('about');
}
}
application/views/about.php
<p><?php echo lang('I\'m a man')?></p>
http://your_base_url/en/about
<p>I'm a man</p>
http://your_base_url/ar/about
<p>انا رجل</p>
-
You might need to translate some of CodeIgniter's language files in
system/language
. Example: if you're using the Form Validation library for Arabic pages, translatesystem/language/english/form_validation_lang.php
toapplication/language/arabic/form_validation_lang.php
. -
links to internal pages are prefixed by the current language, but links to files are not.
site_url('about/my_work'); // http://mywebsite.com/en/about/my_work base_url('css/styles.css'); // http://mywebsite.com/css/styles.css
-
Get the current language:
$this->lang->lang(); // en
-
Switch to another language:
anchor($this->lang->switch_uri('ar'),'Display current page in Arabic');
-
the root page (
/
) is supposed to be some kind of splash page, without any specific language. However, this can be changed. Read the No splash page section below. -
the CodeIgniter
system/core/Config.php
site_url()
method is overridden inMY_Config.php
: a language segment can be added (when appropriate) to generated URLs. It is also used byanchor()
,form_open()
... -
If a key is not found in the language file's array, a file called
strings_lang.php
will be created and the key and value will be added to it. E.g. ifline('xyz')
is not available,strings_lang.php
will be created and will contain:<?php defined('BASEPATH') OR exit('No direct script access allowed'); $lang['xyz'] = "xyz";
A special URI is not prefixed by a language. The root URI (/
) is by default a special URI.
You might need other special URIs, like for an admin section in just one language.
In application/core/MY_Lang.php
, by adding the 'admin'
string to the $special
array, links to the admin page will not be prefixed by the current language.
site_url('admin');
// http://your_base_url/admin
In application/core/MY_Lang.php
:
- remove
""
from the$special
array; - set
$default_uri
to something else likehome
; - if English is your default language, now a request to
/
will be redirected toen/home
; - the default language is the first item of the
$languages
array.
application/core/MY_Lang.php
: add a new language to $languages
array
// example: German (de)
'de' => 'german',
application/config/routes.php
: add new routes
// example: German (de)
$route['^de/(.+)$'] = "$1";
$route['^(en|ar|de)$'] = $route['default_controller'];
- create the corresponding language folder in application/language. For this German example, it would be called
german
.