我正在使用CodeIgniter的库来更改站点上的语言。这是我从控制器加载语言文件的方式:
$this->load->helper('language');
$this->load->helper('url');
$this->lang->load('custom','english');但是我需要将一些数据从数据库传递到那个语言文件(custom_lang.php),而我不知道怎么做?敬请指教...
发布于 2012-08-10 07:19:43
我猜你想要处理翻译文本中的变量,比如“你有XYZ新消息”?
简单地在翻译文本中放置一些特定的标记,然后使用str_replace填充所需的值,就像这样"You %num_messages% new message“。
在控制器中使用以下命令:
$parsed_text = str_replace ("%num_messages%", $msg_count, $input_translation_text);然后将$parsed_text分配给模板/视图。
发布于 2014-06-01 01:35:35
这是针对Codeigniter2.0的。
您需要动态创建一个语言文件(custom_lang.php) (例如,每当您更新数据库的语言内容时)
layout:数据库布局
创建一个包含列id、category、description、lang、token的表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, 'error', 'noMail', 'english', 'You must submit a valid email address'),
(2, 'error', 'noUser', 'english', 'You must submit a username');language :关于CodeIgniter语言文件
CodeIgniter将首先在你的应用程序/语言目录中查找,每种语言都应该存储在它自己的文件夹中。确保创建了英语或德语等子目录,例如application/language/english
3:动态创建语言文件的控制器函数
关于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){
//$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.'/custom_lang.php', $langstr);
}结束语:
updatelangfile每当您更改数据库时,您将调用函数updatelangfile(‘english’)
function __construct(){ parent::__construct();$this->load->helper('file');$this->lang->load(‘自定义’,‘英语’);}
https://stackoverflow.com/questions/11892925
复制相似问题