开发多语言系统 [英] Develop multilingual system

查看:184
本文介绍了开发多语言系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是一个分析性问题.

This is more of an analytical question.

我需要知道如何最好地制作多语言系统,也就是用户可以更改语言的系统.该语言将存储在cookie或数据库中.
过去,我为每种语言使用了不同的文件,例如:

I need to know how best to make a multilingual system, a.k.a. a system where the user can change the language. The language will be stored in a cookie or a database.
I've worked in the past with different files for each language, for example:

nl.php

$lang['hi'] = 'Hoi';
$lang['howareyou'] = 'Hoe gaat het?';

en.php

$lang['hi'] = 'Hi'];
$lang['howareyou'] = 'How are you?';

index.php

include($language . '.php');

如您所见,该系统既低效又不安全.有更好的方法吗? 我可以想出几种方法来完成此操作,但是我真的不知道哪种方法会好.

As you can see, this system is both inefficient and insecure. Is there a better way to do it? I can think of a few ways to do this this instant, but I don't really know which one would be good.

有人可以帮我吗?请不要只说这样做!",还请告诉我为什么这样做是一种好方法.

Can anyone help me with this? Please don't just say "Do it like this!", also tell me why it is a good way to do it.

推荐答案

好吧,如果您不需要提供通过Web界面更改本地化文本的功能,则可以这样做:

Well, if you don't need to provide ability to change localization texts via web interface, you can just do it like this:

include/locales.php

<?php
    if (!isset($locales)) {
        $locales = array(
            "en" => array(
                "hi" => "Hi",
                "howareyou" => "How are you?"
            ),
            "nl" => array(
                "hi" => "Hoi",
                "howareyou" => "Hoe gaat het?"
            )
        );
    }
?>

index.php

include("include/locales.php");
if (!isset($locales[$language])) $locale = $locales[$deflang]; // $deflang could be "en"
else $locale = $locales[$deflang];

echo $locale["hi"]." ".$locale["howareyou"];

这是最快的方法,因为使用哈希解析单个包含文件是非常快的操作. 如果您需要提供通过Web界面修改本地化字符串的功能,那么您将需要在每次显示页面时将本地化字符串存储在DB中并从那里读取em ...这种方法会更慢.

This is the fastest approach, because parsing single include file with hash is very fast operation. If you need to provide ability to modify localization strings via web interface, then you will need to store localization strings in DB and read em from there each time you show a page... This approach is way more slow.

这篇关于开发多语言系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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