Multi-language websites present a challenge when it comes to managing language content. This article explores a method to dynamically create Codeigniter language files from a database, allowing you to update translations directly from the database and populate your language files on the fly.
Create a table called lang_token to store the translation data:
<code class="sql">CREATE TABLE lang_token ( id INT NOT NULL AUTO_INCREMENT, category TEXT NOT NULL, description TEXT NOT NULL, lang TEXT NOT NULL, token TEXT NOT NULL, PRIMARY KEY (id) );</code>
Create a controller function to generate the language file:
<code class="php">function updatelangfile($my_lang) { $query = $this->db->where('lang', $my_lang)->get('lang_token'); $langstr = "<?php $lang = array(); "; foreach ($query->result() as $row) { $langstr .= "$lang['{$row->category}_{$row->description}'] = \"{$row->token}\";\n"; } write_file('./application/language/' . $my_lang . '/general_lang.php', $langstr); }</code>
<code class="php">function __construct() { parent::__construct(); $this->load->helper('file'); $this->lang->load('general', 'english'); }</code>
By utilizing this method, you can seamlessly manage language content for your website from a central database, simplifying translation updates and ensuring consistency across language versions.
The above is the detailed content of How Can I Dynamically Generate CodeIgniter Language Files from a Database?. For more information, please follow other related articles on the PHP Chinese website!