Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

php - How to create Codeigniter language files from database?

I'm building a multi-language online site with Codeigniter. My question is how to pass data from database to the Codeigniter language files. My logic so far is to run a foreach query, which will populate the language file with translation_key and value. The Problem is that language files aren't some extended CI_class classes and now I don't know how to move on.

How would you approach to that problem? Documentation doesn't say nothing about how to use language class with database.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are on the right track. You’ll want to create a language file on the fly (e.g. whenever you update the language contents of your database)

1st: the database layout

Create a table lang_token with columns id, category, description, lang, token and populate its fields like this:

    CREATE TABLE IF NOT EXISTS `lang_token` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `category` text NOT NULL,
      `description` text NOT NULL,
      `lang` text NOT NULL,
      `token` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

    INSERT INTO `lang_token` (`id`, `category`, `description`, `lang`, `token`) 
    VALUES
      (1, 'error', 'noMail', 'english', 'You must submit a valid email address'),
      (2, 'error', 'noUser', 'english', 'You must submit a username');

2nd: About CodeIgniter language files

CodeIgniter will look first in your application/language directory, Each language should be stored in its own folder. Make sure you have your English or German, etc. subdirectories created e.g. application/language/english

3rd: Controller function to create language file on the fly

About The Codeigniter language files: It's a good practice to use a common prefix (category) for all messages in a given file to avoid collisions with similarly named items in other files There structure is like: $lang['category_description'] = “token”;

    function updatelangfile($my_lang){
        $this->db->where('lang',$my_lang);
        $query=$this->db->get('lang_token');

        $lang=array();
        $langstr="<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
                /**
                *
                * Created:  2014-05-31 by Vickel
                *
                * Description:  ".$my_lang." language file for general views
                *
                */"."


";



        foreach ($query->result() as $row){
            //$lang['error_csrf'] = 'This form post did not pass our security checks.';
            $langstr.= "$lang['".$row->category."_".$row->description."'] = "$row->token";"."
";
        }
        write_file('./application/language/'.$my_lang.'/general_lang.php', $langstr);

    }

Final notes:

  1. Whenever you change your database, you’ll call the function updatelangfile(‘english’)
  2. Don’t forget to load the file helper and language class in the constructor of the controller where updatelangfile() is located:

    function __construct(){
        parent::__construct();
        $this->load->helper('file');
        $this->lang->load('general', 'english');
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...