douliang1900 2014-05-29 18:02
浏览 20
已采纳

如何从数据库中创建Codeigniter语言文件?

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.

  • 写回答

1条回答 默认 最新

  • dqnf28092 2014-05-31 17:22
    关注

    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');
      }
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?