簡體   English   中英

CodeIgniter - 從數據庫中存儲和讀取語言。

[英]CodeIgniter - Store and read Language from database.

我將語言參數名稱如$lang['list_add']在數據庫表中作為list_add 現在我想將它的值存儲在多語言的 db 中,並在我的基於 CI 的應用程序中檢索。

如何訪問我的應用程序中的 lang 文件,以便他們可以從 Db 讀取和寫入語言值?

謝謝

您需要動態創建語言文件(例如,每當您更新數據庫的語言內容時)

第一:數據庫布局

使用列idcategorydescriptionlangtoken創建一個表lang_token並像這樣填充其字段:

    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, 'list', 'add', 'english', 'add to list'),
      (2, 'list', 'remove', 'english', 'remove from list');

第二:關於CodeIgniter語言文件

CodeIgniter 將首先查看您的 application/language 目錄,每種語言都應存儲在其自己的文件夾中。 確保您創建了英語或德語等子目錄,例如application/language/english

第三:控制器功能,即時創建語言文件

關於 Codeigniter 語言文件:為給定文件中的所有消息使用公共前綴(類別)是一個很好的做法,以避免與其他文件中類似命名的項目發生沖突 結構類似於: $lang['category_description'] = “token”;

    function updatelangfile($my_lang){
        $this->db2->where('lang',$my_lang);
        $query=$this->db2->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
                *
                */"."\n\n\n";



        foreach ($query->result() as $row){

            $langstr.= "\$lang['".$row->category."_".$row->description."'] = \"$row->token\";"."\n";
        }
        write_file('./application/language/'.$my_lang.'/general_lang.php', $langstr);

    }

最后說明:

  1. 每當您更改數據庫時,您都會調用函數updatelangfile('english')
  2. 不要忘記在 updatelangfile() 所在控制器的構造函數中加載文件助手語言類

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM