在CodeIgniter中构建多语言导航菜单 [英] Building a multi-language navigation menu in CodeIgniter
问题描述
我的脚本死了,没有提供错误消息。让我们来调查一下!
在我看来,我调用一个函数来构建菜单的内容,并发送应该存在于菜单中的页面的名称:
// application / views / templates / header.php
< ul class =navigation>
<?php
//以英文名称发送,这也是。子。
//函数应返回相应语言的名称,
//加上英语中的slug。
$ args = ['home','compete','gallery','finalists','about'];
build_navigation($ args);
?>
< / ul>
现在的想法是循环遍历这些参数,并为每个参数构建一个列表项
// application / helpers / navigation_helper .php
//在autoload.php中加载 - 确认正在工作
<?php
if(!defined('BASEPATH'))exit允许访问);
if(!function_exists('build_navigation')){
function build_navigation($ args){
foreach($ args as $ token)
echo< li>< a href = \{$ token} \> {$ this-> lang-> line($ token)}< / a>< / li& \\ n;
}
}
?>
当我看到这一点时,在我的头上是有意义的,问题如在给定上下文中 $ this
如果我更改 $ this-> lang-> line($ token)
至 $ token
,脚本运行(虽然我没有得到我的多语言功能)。
我需要的语言文件...
// application / language / english / en_lang.php
<?php
$ lang ['home'] =家;
$ lang ['compete'] =竞争;
$ lang ['gallery'] =画廊;
$ lang ['finalists'] =决赛入围者;
$ lang ['about'] =关于;
?>
// application / language / swedish / sv_lang.php
<?php
$ lang ['home'] =Hem ;
$ lang ['compete'] =Tävla;
$ lang ['gallery'] =加勒里
$ lang ['finalists'] =决赛者;
$ lang ['about'] =信息;
?>
...在这里你可以看到我正在加载我的语言文件在我的控制器反映CI文档中的页面控制器):
<?php
/ **
* Pages
*
*用于构建静态页面的类。
*
* /
类扩展CI_Controller {
public function view($ page ='home'){
if !file_exists('application / views / pages /'.$ page。'。php'))
show_404();
$ data ['title'] = ucfirst($ page); //大写首标的第一个字母
$ this-> lang-> load('en','english')
$ this-> lang-> load('sv','swedish');
$ this-> load-> view('templates / header',$ data);
$ this-> load-> view('pages /'.$ page,$ data);
$ this-> load-> view('templates / footer',$ data);
}
}
?>
解决方案在 build_navigation args)
请尝试
$ ci =& get_instance / p>
而不是 $ this->
使用 $ ci-> lang
Als0,
$ this-> ; lang-> load('filename','language');
其中filename是要加载的文件的名称(不带文件扩展名)是包含它的语言集(即,英语)。如果缺少第二个参数,将使用应用程序/ config / config.php文件中设置的默认语言。
您只需加载所需的语言
I'm trying to create a function that builds and displays a navigation menu, whilst keeping to MVC as much as possible (though I'm new to it, so I don't understand it completely).
My script dies without providing an error message. Let's investigate!
In my view, I call a function that builds the menu's contents, and send in the names of the pages that should exist in the menu:
// application/views/templates/header.php
<ul class="navigation">
<?php
// Send in the English name, which also becomes the slug.
// Function should return the name in the appropriate language,
// plus the slug in English.
$args = ['home','compete','gallery','finalists','about'];
build_navigation($args);
?>
</ul>
The idea now is to loop through those arguments, and build a list-item for each argument containing the file name — which is also the URL slug — and the display name in the appropriate language.
// application/helpers/navigation_helper.php
// This is loaded in autoload.php — confirmed working
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('build_navigation')) {
function build_navigation($args) {
foreach ($args as $token)
echo "<li><a href=\"{$token}\">{$this->lang->line($token)}</a></li>\n";
}
}
?>
When I look at that, it sort of makes sense in my head, but at the same time raises questions like "what is $this
in the given context?"
If I change $this->lang->line($token)
to just $token
, the script runs (though I don't get my multi-language functionality).
I have the language files I need…
// application/language/english/en_lang.php
<?php
$lang['home'] = "Home";
$lang['compete'] = "Compete";
$lang['gallery'] = "Gallery";
$lang['finalists'] = "Finalists";
$lang['about'] = "About";
?>
// application/language/swedish/sv_lang.php
<?php
$lang['home'] = "Hem";
$lang['compete'] = "Tävla";
$lang['gallery'] = "Galleri";
$lang['finalists'] = "Finalister";
$lang['about'] = "Info";
?>
…And here you can see that I'm loading my language files in my controller (which almost exactly mirrors the pages controller in the CI docs):
<?php
/**
* Pages
*
* Class for building static pages.
*
*/
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists('application/views/pages/'.$page.'.php'))
show_404();
$data['title'] = ucfirst($page); // Capitalise first letter of title
$this->lang->load('en','english');
$this->lang->load('sv','swedish');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
解决方案 In your build_navigation($args)
try
$ci = &get_instance();
and instead of $this->
use $ci->lang
Als0,
$this->lang->load('filename', 'language');
Where filename is the name of the file you wish to load (without the file extension), and language is the language set containing it (ie, english). If the second parameter is missing, the default language set in your application/config/config.php file will be used.
You only need to load the required language not both of them.
这篇关于在CodeIgniter中构建多语言导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!