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

查看:22
本文介绍了如何从数据库创建 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)

第一:数据库布局

用列idcategorydescriptionlang创建一个表lang_token>, 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 目录,每种语言都应存储在其自己的文件夹中.确保您创建了英语或德语等子目录,例如申请/语言/英文

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

第三:控制器功能,即时创建语言文件

关于 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);

    }

最后说明:

  1. 每当您更改数据库时,您都会调用函数 updatelangfile('english')
  2. 不要忘记在updatelangfile()所在控制器的构造函数中加载file helper语言类:

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

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

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