[英]Building a multi-language navigation menu in CodeIgniter
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). 我正在尝试创建一个构建并显示导航菜单的功能,同时尽可能保持MVC的功能(尽管我是新手,所以我并不完全了解它)。
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. 现在的想法是遍历这些参数,并为每个参数构建一个列表项,其中包含文件名(也是URL子句)和以适当语言显示的名称。
// 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?” 当我看着它时,这在我脑海中是很有意义的,但同时又引发了诸如“在给定上下文中$this
是什么?”之类的问题。
If I change $this->lang->line($token)
to just $token
, the script runs (though I don't get my multi-language functionality). 如果我将 $this->lang->line($token)
更改$token
, 脚本将运行 (尽管我没有多语言功能)。
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): …在这里您可以看到我正在将语言文件加载到控制器中(这几乎完全反映了CI文档中的页面控制器):
<?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)
在您的build_navigation($args)
try 尝试
$ci = &get_instance(); $ ci =&get_instance();
and instead of $this->
use $ci->lang
而不是$this->
使用$ci->lang
Als0, 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). 其中filename是您要加载的文件的名称(不带文件扩展名),而language是包含该文件的语言集(即英语)。 If the second parameter is missing, the default language set in your application/config/config.php file will be used. 如果缺少第二个参数,将使用在application / config / config.php文件中设置的默认语言。
You only need to load the required language not both of them. 您只需要加载所需的语言,而不是两者都加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.