如何从数据库创建Codeigniter语言文件? [英] How to create Codeigniter language files from database?

查看:65
本文介绍了如何从数据库创建Codeigniter语言文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter建立一个多语言在线网站。
我的问题是如何将数据从数据库传递到Codeigniter语言文件
到目前为止,我的逻辑是运行一个 foreach 查询,它将使用translation_key和value填充语言文件。问题是,语言文件不是一些扩展的CI_class类,现在我不知道如何继续。

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.

推荐答案

您将需要即时创建语言文件(例如,每当您更新数据库的语言内容时)

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:数据库布局

使用列 id 创建一个表 lang_token 类别描述 lang token 并填充其字段,如下所示:

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');

第二:关于CodeIgniter语言文件

CodeIgniter会首先在你的应用程序/语言目录中,每种语言都应该存储在自己的文件夹中。确保您有您的英语或德语等子目录创建例如。 application / language / english

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功能即时创建语言文件

关于Codeigniter语言文件:
对于给定文件中的所有消息使用通用前缀(类别)以避免冲突其他文件中的命名项
其结构如下: $ lang ['category_description'] =token;

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
                *
                */"."\n\n\n";



        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\";"."\n";
        }
        write_file('./application/language/'.$my_lang.'/general_lang.php', $langstr);

    }

最终备注: p>

Final notes:


  1. 每当更改数据库时,您将调用 updatelangfile('english')
  2. )位于:
function __construct(){
    parent::__construct();
    $this->load->helper('file');
    $this->lang->load('general', 'english');
}


这篇关于如何从数据库创建Codeigniter语言文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆