如何制作多语言网站 [英] How to make a Multilanguage website

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

问题描述

有人能告诉我如何用PHP和MySQL创建一个动态的多语言网站吗?我对此一无所知.我在Google上进行搜索,但找不到任何好的解决方案.

Can any body tell me how to make a dynamic multilanguage website in PHP and MySQL? I have no idea about it. I searched on Google and didn't find any good solutions.

任何人都可以为我提供逐步指导吗?如果可能,请为多语言网站进行演示.或者,请让我转到说明其详细信息的任何链接.

Can any one provide me with a step by step guide? If possible make a demo for a multilanguage website. Or please refer me to any link where it explains the details about it.

推荐答案

简短答案:没有简短答案,因为要考虑的变量很多,工作量很大.所以...

Short answer: there is no short answer, as there are a lot of variables to consider, and plenty of work to do. So...

长答案:我将尽我所能对其进行细分,但是对于您所提的问题,没有一个全民皆宜"的答案.

Long answer: I'm going to break it down as well as I can, but there isn't a "good for all" answer to a question as broad as yours.

首先,手头任务的变量:

First, variables of the task at hand:

  1. 语言列表:您的网站将使用一组预定义的语言,还是会变化/异构?例如,一个网站可能完全使用两种定义明确的语言双语(或者,例如,我经营一个英语/加泰罗尼亚/西班牙文网站);或不同的部分可能在不同的语言集上可用(例如,查看MS的网站:它们大多是同质的,但博客,KB文章和某些文档之类的东西仅在部分支持的语言中可用).

  1. List of languages: will your site be in a predefined set of languages, or will it be varied/heterogeneous? For example, a site may be entirely bilingual in two well defined languages (or, to put another example, I run an English/Catalan/Spanish site); or different sections could be available on different sets of languages (for an example, look at MS's sites: they are mostly homogeneous, but stuff like blogs, KB articles, and some docs are just available in a subset of the supposedly supported languages).

翻译来源:您或合作者是否以每种相关语言提供内容?还是某些版本通过单一基本"语言的翻译软件运行?第一种方法需要花费很多额外的工作来制作内容,但是比第二种方法可以获得更高质量的结果.

Translations source: is content provided in each relevant language by you or some collaborator? Or are some versions run through translation software from a single "base" language? The first approach takes a lot of extra work to produce the contents, but yields higher quality results than the second.

语言本身:一旦回答了1)和2),您将需要确切地知道要使用哪种语言.请注意,在包含方言的情况下(例如:美国英语+英国英语,或阿根廷西班牙语+西班牙西班牙语),搜索引擎可能会遇到一些内容重复"的问题,但有关细节在这里太离题了(只是提及,这样您就可以意识到潜在的问题.

Languages themselves: once you have 1) and 2) answered, you will need to be aware of exactly which languages are you working with. Note that in the case you include dialects (ex: US English + UK English, or Argentina Spanish + Spain Spanish), you may encounter some "duplicate content" issues with search engines, but details on that are too off-topic here (just mentioning so you are aware of the potential issues).

您是否以抽象语言定位语言(例如,我的网站提供了三种语言,却根本不关心访问者所在的位置:这就是我所拥有的,因此请选择您喜欢的语言);还是针对不同的地区/国家?在后一种情况下,事情可能变得更加复杂,因为您可能需要关心除语言之外的其他内容(例如,时区,货币或日期时间格式约定),但是您却可以使用特定国家/地区的TLD.

Are you targeting languages in the abstract (for example, my site offers the three languages without caring at all where the visitor is: that's what I have, so choose what you prefer); or rather targeting different regions/countries? In the later case, things can get extra complex, as you may need to care about other stuff besides languages (like timezones, currencies, or date-time format conventions, to name some), but you get the benefit of being able to use country-specific TLDs.

一旦您定义了上面的内容,就开始工作.这些是您需要处理的最突出的任务:

Once you have the above well-defined, let's start working. These are the most prominent tasks you'd need to do handle:

  1. 语言检测:最合理的方法是使用GET参数(例如URL上的?lang = en-us).另外,当请求不带语言参数的URL时,您可能会使用一些cookie和/或IP地理位置进行回退.另外,如果可以的话,请考虑使用URL美化主题(哪个看起来更好:example.com/index.php?lang=en-usexample.com/en-us/home?).就个人而言,我喜欢ModRewrite授予.htaccess文件的功能,但这仅适用于Apache驱动的服务器.

  1. Language detection: the most reasonable approach is to use a GET parameter (something like ?lang=en-us on the URL). Also, you might use some cookie and/or IP geolocation to fall back when a URL with no language argument is requested. Also, if you have the means, consider the topic of URL beautification (what looks better: example.com/index.php?lang=en-us or example.com/en-us/home?). Personally, I love the power ModRewrite grants to my .htaccess file, but that'll only work on Apache-powered servers.

内容管理:无论您是要从数据库中获取内容(如文章内容),还是要包含文件(通常是面包屑,菜单,站点范围标题等)或任何其他方式,您都需要分离内容的每个版本(语言)的某种方式.以下是一些有关如何完成此操作的示例:

Content management: regardless of whether you are fetching content from a DB (like article content), include files (typical for breadcrumbs, menus, site-wide headings, etc), or any other means, you will need some way to separate each version (language) of the content. Here are some examples of how it can be done:

  • 对于数据库内容,我最好的建议是提出一些可靠的字段命名模式并坚持使用.例如,我将_en_es_ca附加到数据库的所有与语言相关的字段中.这样,我可以使用$row["title_$lang"]这样的表达式访问正确的内容.
  • 对于包含文件,再次使用文件命名约定是最明智的方法.就我而言,我的文件名以.en.php.ca.htm等结尾.然后,我的包含调用看起来像include("some-filename.$lang.php).
  • 您有时会直接从您的PHP代码中吐出一小段文本(例如,在标记表标题时).您可以使用每种语言的包含文件来定义具有相同键的块"数组,也可以使用诸如Geert建议的DB表.前一种方法开发工作量少,后一种方法维护工作量少(特别是如果您不是一个人工作时).
  • For DB content, my best advise is to come up with some solid field naming pattern and stick to it. For example, I append _en, _es, or _ca to all language-dependent fields of my DB. This way, I can access the right content with expressions like $row["title_$lang"].
  • For include files, again a file naming convention is the sanest approach. In my case, I have file names ending with .en.php, .ca.htm, etc. My include calls then look like include("some-filename.$lang.php).
  • From time to time, you will be spitting out small chunks of text directly from your PHP code (for example, when labeling the headings of a table). You can use an include file per language defining a "chunks" array with the same keys, or a DB table like Geert suggested. The former approach takes less work to develop, the latter should take less work to maintain (especially if you aren't working alone).

语言选择:非常重要,除了调整URL本身的GET参数之外,您还应该为用户提供一种选择自己的语言的方法.对于少数几种语言,标志"通常效果很好,因为即使页面最初回到用户完全不知道的语言,也可以理解它们.对于更多语言,下拉菜单似乎更有效(就视口空间而言),但是您应确保添加一些视觉(即非文本)提示.某些网站会强迫您在输入时选择一种语言,并且只有每种语言的首页链接.就我个人而言,我的三个标志位于站点菜单的顶部,每个标志都指向当前地址,但仅更改了语言参数.这样的代码可以很好地工作:

Language pick: quite essential, you should provide your users a way to choose their own language, other than tweaking the GET arguments on the URL itself. For few languages, "flags" often work great, since they can be understood even if the page has initially fallen back to a language the user doesn't know at all. For more languages, a dropdown menu seems more efficient (in terms of viewport space), but you should make sure to add some visual (ie: non-textual) hints. Some sites force you to pick a language upon entering, and only have links to the home-page on each language. Personally, I have my three flags standing out on top of my site's menu, each pointing to the current address with only the language argument changed. A code like this can work quite well:


function translatedURI($new_lang) {
    return str_replace("lang=$lang", "lang=$new_lang", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
}


  1. CMS调整:如果您的网站(或网站的一部分)使用某种CMS,讨论区等,事情可能会变得很混乱.从我自己的经验来看,我在我的网站上有一个phpBB论坛,该论坛分为三个主要类别(每种语言一个),使它们看起来像三个独立的论坛(但用户只需要登录/注册其中一个即可).获得所有语言的访问权限,因为它们实际上只是同一板的类别).我必须进行的调整才能顺利进行,这威胁到了我仍然保留的最后一部分理智:S.对于这些情况,建议您查找正在使用的特定软件的文档和支持功能.

好吧,这就是我现在能想到的一切.我认为您应该有足够的能力来拉起袖子并开始工作.然后,如果您遇到困难,请再提出一些具体问题,我相信您会得到更多具体答案.

Well, that's everything I can come out with for now. I think you should have enough to pull up your sleeves and get to work. Then, if you hit some wall on your path, come back with specific questions and I'm confident you'll get more specific answers.

希望这会有所帮助.

这篇关于如何制作多语言网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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