How To Add Together Multiple Languages Back Upwards Inward Codeigniter ?
CodeIgniter comes amongst multi-language support, too known every bit internationalization which enables us to dynamically introduce our application’s interface inwards dissimilar languages without duplicating the existing root code for each language.
Configuring Multi-Language Support
First nosotros bespeak to configure the necessary files before nosotros tin start using linguistic communication support. The CodeIgniter config file, located inwards the application/config directory, contains an pick called linguistic communication which defines the default linguistic communication of the application.
Also croak on inwards heed that for every cardinal must live introduce inwards all of your linguistic communication files. So if your ../language/english/message_lang.php file looks similar this:
Of course, you lot tin accept multiple linguistic communication files within a unmarried linguistic communication directory. It’s recommended to grouping your messages into dissimilar files based on their context in addition to purpose, in addition to prefix your message keys amongst a file-specific keyword for consistency.
Loading the Language Files
There are 2 ways to charge a linguistic communication files inwards Codeigniter. Applying claw is the best alternative to add together multi languages support.
1. Only Controller
2. Using Hook
1. Only Controller
Even though nosotros practise linguistic communication files, they are non effective until nosotros charge them within controllers. The next code shows how nosotros tin charge the files within a controller:
The start parameter to the lang->load() method volition live the language’s filename without the _lang suffix. The instant parameter, which is optional, is the linguistic communication directory. It volition signal to default linguistic communication inwards your config if it’s non provided here.
We tin straight reference the entries of a linguistic communication file using the lang->line() method in addition to assign it’s render to the information passed into the sentiment templates. Inside the view, nosotros tin thence usage the higher upwards linguistic communication message every bit $language_msg.
Luckily, nosotros tin usage CodeIgniter hooks to build a quick in addition to effective solution for loading linguistic communication files automatically for each controller. CodeIgniter calls a few built-in hooks every bit component subdivision of its execution process. We’ll usage the post_controller_constructor claw which is called straightaway afterward our controller is instantiated in addition to prior to whatever other method calls.
We enable hooks inwards our application yesteryear setting the enable_hooks parameter inwards the top dog config file.
Switching Between Different Languages
Once nosotros accept established back upwards for multiple languages, a link for each linguistic communication tin live provided to the user, by in addition to large inwards 1 of our application’s menus, which the users tin click in addition to switch the language. H5N1 session or cookie value tin live used to croak on rails of the active language.
Let’s run across how nosotros tin care linguistic communication switching using the hooks shape nosotros generated earlier. First nosotros bespeak to practise a shape to switch the language; we’ll live using a split controller for this every bit shown below:
Source: http://www.sitepoint.com/multi-language-support-in-codeigniter/ Sumber http://developer-paradize.blogspot.com
Configuring Multi-Language Support
First nosotros bespeak to configure the necessary files before nosotros tin start using linguistic communication support. The CodeIgniter config file, located inwards the application/config directory, contains an pick called linguistic communication which defines the default linguistic communication of the application.
<?php $config['language'] = 'english';CodeIgniter allows you lot to driblet inwards every bit many languages every bit you lot desire inwards the ../application/language/ folder. For example, if I wanted to add together back upwards for Swedish, I would practise a folder within linguistic communication called "swedish". All linguistic communication files must destination inwards _lang. Let’s practise unopen to linguistic communication files that incorporate mistake messages for a sample application. Create the file english/message_lang.php (it’s of import that all of the linguistic communication files accept the suffix _lang.php). The next code contains unopen to sample entries for the content of our linguistic communication file.
Also croak on inwards heed that for every cardinal must live introduce inwards all of your linguistic communication files. So if your ../language/english/message_lang.php file looks similar this:
<?php $lang["msg_first_name"] = "First Name"; $lang["msg_last_name"] = "Last Name"; $lang["msg_dob"] = "Date of Birth"; $lang["msg_address"] = "Address"; // to a greater extent than keys...Then your ../language/swedish/message_lang.php file must await something similar this:
<?php $lang["msg_first_name"] = "Förnamn"; $lang["msg_last_name"] = "Efternamn"; $lang["msg_dob"] = "Födelsedatum"; $lang["msg_address"] = "adress"; // to a greater extent than keys...Note that lodge does non affair inwards the linguistic communication files. All keys are component subdivision of a larger array.
Of course, you lot tin accept multiple linguistic communication files within a unmarried linguistic communication directory. It’s recommended to grouping your messages into dissimilar files based on their context in addition to purpose, in addition to prefix your message keys amongst a file-specific keyword for consistency.
Loading the Language Files
There are 2 ways to charge a linguistic communication files inwards Codeigniter. Applying claw is the best alternative to add together multi languages support.
1. Only Controller
2. Using Hook
1. Only Controller
Even though nosotros practise linguistic communication files, they are non effective until nosotros charge them within controllers. The next code shows how nosotros tin charge the files within a controller:
<?php shape TestLanguage extends CI_Controller { world role __construct() { parent::__construct(); $this->lang->load("message","english"); // or ($this->lang->load("message","swedish");) } role index() { $data["language_msg"] = $this->lang->line("msg_first_name"); $this->load->view('language_view', $data); } }Here we’ve used a controller’s constructor to charge the linguistic communication file thence it tin live used throughout the whole class, thence nosotros reference it inwards the class’ index() method.
The start parameter to the lang->load() method volition live the language’s filename without the _lang suffix. The instant parameter, which is optional, is the linguistic communication directory. It volition signal to default linguistic communication inwards your config if it’s non provided here.
We tin straight reference the entries of a linguistic communication file using the lang->line() method in addition to assign it’s render to the information passed into the sentiment templates. Inside the view, nosotros tin thence usage the higher upwards linguistic communication message every bit $language_msg.
<?php $this->lang->line("language_msg");There may live an occasion when nosotros bespeak to charge linguistic communication files straight from the views every bit well. For example, using linguistic communication items for shape labels mightiness live considered a adept argue for straight loading in addition to accessing messages within views. It’s possible to usage the same access method for these files within views every bit within controllers.
<?php lang("language_msg");2. Using Hooks
Luckily, nosotros tin usage CodeIgniter hooks to build a quick in addition to effective solution for loading linguistic communication files automatically for each controller. CodeIgniter calls a few built-in hooks every bit component subdivision of its execution process. We’ll usage the post_controller_constructor claw which is called straightaway afterward our controller is instantiated in addition to prior to whatever other method calls.
We enable hooks inwards our application yesteryear setting the enable_hooks parameter inwards the top dog config file.
<?php $config['enable_hooks'] = TRUE;Then nosotros tin opened upwards the hooks.php file within the config directory in addition to practise a custom claw every bit shown inwards the next code:
<?php $hook['post_controller_constructor'] = array( 'class' => 'LanguageLoader', 'function' => 'initialize', 'filename' => 'LanguageLoader.php', 'filepath' => 'hooks' );This defines the claw in addition to provides the necessary information to execute it. The actual implementation volition live created inwards a custom shape within the application/hooks directory. This hooks shape volition charge the linguistic communication dynamically from the session.
<?php shape LanguageLoader { role initialize() { $ci =& get_instance(); $ci->load->helper('language'); $site_lang = $ci->session->userdata('site_lang'); /* If you lot desire to usage cookie instead of session $site_lang = $ci->input->cookie('site_lang'); */ if ($site_lang) { $ci->lang->load('message',$ci->session->userdata('site_lang')); } else { $ci->lang->load('message','english'); } } }Inside the LanguageLoader shape nosotros become the active linguistic communication in addition to charge the necessary linguistic communication files, or nosotros charge the default linguistic communication if the session cardinal is absent. We tin charge multiple linguistic communication files of a unmarried linguistic communication within this class.
Switching Between Different Languages
Once nosotros accept established back upwards for multiple languages, a link for each linguistic communication tin live provided to the user, by in addition to large inwards 1 of our application’s menus, which the users tin click in addition to switch the language. H5N1 session or cookie value tin live used to croak on rails of the active language.
Let’s run across how nosotros tin care linguistic communication switching using the hooks shape nosotros generated earlier. First nosotros bespeak to practise a shape to switch the language; we’ll live using a split controller for this every bit shown below:
<?php shape LangSwitch extends CI_Controller { world role __construct() { parent::__construct(); $this->load->helper('url'); } role switchLanguage($language = "") { $language = ($language != "") ? $language : "english"; $this->session->set_userdata('site_lang', $language); redirect(base_url()); } }Then nosotros bespeak to define links to switch each of the available languages.
<a href='<?php echo $base_url; ?>langswitch/switchLanguage/english'>English</a>Whenever the user chooses a specific language, the switchLanguage() method of the LangSwitch shape volition assign the selected languages to the session in addition to redirect the user to the habitation page. Now the active linguistic communication volition live changed inwards the session in addition to volition charge the specific linguistic communication file for the active language.
<a href='<?php echo $base_url; ?>langswitch/switchLanguage/swedish'>Swedish</a>
Source: http://www.sitepoint.com/multi-language-support-in-codeigniter/ Sumber http://developer-paradize.blogspot.com
Comments
Post a Comment