使用 Codeigniter 进行国际化导航

发布于 2024-11-07 16:37:42 字数 874 浏览 0 评论 0原文

我正在尝试找到构建多语言网站导航的最佳方法。

我知道 CI 中的语言类,但它似乎更多地用于定义整个网站中常用的随机单词和文本行。似乎为每种语言创建 lang 文件,然后定义所有链接的翻译似乎是标准方法?

在过去的非 codeigniter 项目中,我设置了一个这样的类

class Link{
 var $name = array();   
 var $url; 
 var $links = array(); 

function add_link($links){ 
 $this->links[] = $links;
 }
}

$all_sections = array();

$section                = new Link();
$section->name['en']    = "Home";
$section->name['fr']]    = "Uberdurky";
$section->url           = "/";

$sub_section                = new Link();
$sub_section->name['en']    = "About Acme Ltd";
$sub_section->name['fr']    = "Fabuka Acme Ltd";
$sub_section->url           = "/about/";
$section->add_link($sub_section); 

然后我有一个函数来循环并输出导航,它只查看由会话或 URL 定义的当前名称[Lang]

这对我来说似乎更简单更少的开销 - 好处是导航结构和翻译都在一个地方定义。但我是 CI 新手,所以我可能会误解标准方法……?我用谷歌搜索了很多,但没有看到这里详细的解决方案。

I'm trying to find the best way of structuring a multi-language site navigation,.

I'm aware of the language class in CI but it seems to be more for defining random words and lines of text that are used commonly throughout the site. It appears that creating lang files for each language and then defining translations of all the links seems like the standard approach?

In past on non-codeigniter projects I’ve setup one class like this

class Link{
 var $name = array();   
 var $url; 
 var $links = array(); 

function add_link($links){ 
 $this->links[] = $links;
 }
}

$all_sections = array();

$section                = new Link();
$section->name['en']    = "Home";
$section->name['fr']]    = "Uberdurky";
$section->url           = "/";

$sub_section                = new Link();
$sub_section->name['en']    = "About Acme Ltd";
$sub_section->name['fr']    = "Fabuka Acme Ltd";
$sub_section->url           = "/about/";
$section->add_link($sub_section); 

Then I have a function to loop through and output the nav, which just looks at the current name[Lang] as defined by session or URL

This to me seems simpler and less overhead - with the benefit that both the nav structure and translations are defined in one place. But I’m new to CI so I might be misunderstanding the standard approach… ? I've googled quite a bit and haven't seen a solution here in detail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-11-14 16:37:42

重要的是它对有用。使用单独的语言文件有很多好处:

  • 干净的分离
  • 只加载您需要的内容
  • 易于跟踪哪些语言可用
  • 能够让其他人轻松翻译文件

我没有发现任何错误按照你正在做的方式,但如果你想优化 - 不要费心定义所有不同的语言线。如果语言是英语,则不需要定义法语版本。仅使用您需要的,您不必将整个数组传递给 add_link()Link 类应该检测语言并仅加载适当的数组...

...听起来语言文件实际上可能是个好主意。

现在你只有法语和英语。我假设您了解这两种语言,并且(Uberdurky?)是唯一致力于这方面的语言,因此您可以更轻松地“内联”定义它们。当您想要支持 3 种、4 种或 10 种语言时会发生什么?事情很快就会变得杂乱无章。

但是,您不必必须使用 Codeigniter Language 类,您最好使用自己的系统进行导航之类的操作,该系统往往散布着一两个单词的翻译,并且会有所变化频繁(每个站点或站点之间)。

再次由您决定。现在做最适合你的事情,然后再优化。

The important thing is that it works for you. There are a lot of benefits to using separate language files:

  • Clean separation
  • Only load what you need
  • Easy to keep track of which languages are available
  • Ability to let others easily translate the files

I don't see anything wrong with the way you're doing it, but if you want to optimize - don't bother defining all the different language lines. You don't need the French version defined if the language is English. Use only the ones you need, you shouldn't have to pass the whole array to add_link(), the Link class should be detecting the language and loading the appropriate array only...

...it's starting to sound like a language file might be a good idea actually.

For now you just have French and English. I'm assuming you know both languages and (Uberdurky?) are the only one working on this aspect, so it's easier for you to define them "inline". What happens when you want to support 3, 4, or 10 languages? Things will quickly become disorganized and cluttered.

However, you don't have to use the Codeigniter Language class, you might be better off using your own system for something like navigation, which tends to be littered with 1 or two word translations, and changes somewhat frequently (either per site or between sites).

Once again, it's your call. Do what works best for you now and optimize later.

孤独难免 2024-11-14 16:37:42

这可能对遇到这个问题的任何人都有帮助。
https://github.com/cflynn07/CodeIgniterInternationalizationUtility

这是一个获取结构清晰的 HTML 语言表的脚本翻译,并将其转换为 codeigniter 中使用的所需语言文件。

This might be helpful to anyone coming across this question.
https://github.com/cflynn07/CodeIgniterInternationalizationUtility

It's a script to take a cleanly structed HTML table of language translations, and convert them into the required language files used in codeigniter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文