到现在,我的网站还只是一种语言的网站.
By now my site was only a one-language site.
我的文件存放在(假设)上:
I had my files on (suppose):
www.mysite/files/index.php www.mysite/files/header.php www.mysite/files/page1.php www.mysite/files/page2.php现在,我向这些文件添加了多语言支持.所有的单词/文本都存储在数据库中,并且一个php类完成了这些工作.
Now I added a multilanguage support to those files. All the words/texts are stored on the database and a php class complete the stuff.
现在,我(非常)在header.php这行上:
By now I had (badly) on header.php this line:
define('LNG', 'en');,我在每个语言目录中都有一个header.php文件:
and I had a header.php file placed on each language directory:
www.mysite/en/header.php www.mysite/de/header.php ...,然后根据LNG常量包含文件,例如
and then I included the files depending on the LNG constant, e.g.
include_once "/files/page1.php?l=".LNG但是我知道这是非常糟糕的做法!
but I know this is a very bad practise!
我该如何解决这个问题?我将在www.mysite/files/...上保留我的文件,然后在URL中写入用户语言,以便该文件可以从url中读取语言并从数据库中加载正确的文本和单词.
How can I solve this problem? I would mantain my files on www.mysite/files/... and then write the user language in the URL, so that the file can read the language from the url and load the right textes and words from the databases.
排序:
www.mysite/en/index.php实际上加载
www.mysite/index.php使用正确的语言en.我可以从URL中获取en并在页面上定义一个常量,但是,如果我指向www.mysite/en/index.php,我显然会得到一个404 ...
with the correct language en. I'm able to get the en from the URL and define a constant on the page, but if I point to www.mysite/en/index.php I obviously get a 404...
推荐答案处理多语言Web应用程序
LANGUAGE CONSTANTS 可以放在这样的语言目录中的语言文件中
Dealing with multi-language web-applications
LANGUAGE CONSTANTS could be placed in language files within a language directory like this
/ languages en.php it.php ...语言文件可能如下所示
// en.php define("THANK_YOU", "Thank you!"); define("PRODUCTS", "Products"); define("PRODUCT", "Product"); ... // it.php define("THANK_YOU", "Grazie!"); define("PRODUCTS", "Prodotti"); define("PRODUCT", "Prodotto"); ....,也可以将它们存储到数据库中,如下所示.我都用过,但是我更喜欢第一个解决方案
or they could be stored into database as shown below. I've used both but I prefer the first solution
+------------+------------+------------+----- | constant | en | it | ... +------------+------------+------------+----- | THANK_YOU | Thank you! | Grazie! | | PRODUCTS | Products | Prodotti | | PRODUCT | Product | Prodotto | | ... | ... | ... | +------------+------------+------------+-----FRIENDLY URLs 可能如下所示
FRIENDLY URLs may look like these below
www.domain www.domain/en www.domain/it www.domain/en/products www.domain/it/prodotti www.domain/en/product/our-best-product/555 www.domain/it/prodotto/il-nostro-miglior-prodotto/555记住应该在.htaccess文件
RewriteRule ^([a-z]+)(/)?$ index.php?lang=$1 [QSA,L] RewriteRule ^([a-z]+)/(products|prodotti)(/)?$ index.php?lang=$1&action=$2 [NC,L] RewriteRule ^([a-z]+)/(product|prodotto)/(.*)/([0-9]+)(/)?$ index.php?lang=$1&action=$2&id=$4&mode=details [NC,L] # these rules are for a demonstration purpose; otherwise, all three directives # could be written in one singe line / rule或者您可以将请求(如下所示)重定向到入口点,并使用parse_url()进行解析.
or you can redirect the request (as following) to an entry point and parse it with parse_url().
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?param=$1 [QSA,L]ACCESSING THE lang VARIABLE -在多语言项目中,变量lang具有特殊意义,否则将像使用GET,POST, COOKIE,XMLHttpRequest,HTTP headers,HTTP referer,URL hash parameter,database session,APC (Alternative PHP Cache) ...
ACCESSING THE lang VARIABLE - On multi-language projects variable lang has a special significance, otherwise it would be treated and passed around just like any other variable using GET, POST, COOKIE, XMLHttpRequest, HTTP headers, HTTP referer, URL hash parameter, database session, APC (Alternative PHP Cache) ...
如果您关心UX和SEO,则应在URL中写入lang变量.我更喜欢使用$ _SESSION或$ _GET,应该指出的是,上述某些方法并非总是可以工作,或者可能被Proxy阻止.
If you care about UX and SEO the lang variable should be written within URL. I prefer using $_SESSION or $_GET and it should be pointed that some of the methods mentioned above doesn't work always or may get blocked by a Proxy.
下面的代码段显示了如何使用$ _SESSION或$ _GET来访问它
The snippet below shows how to access it using $_SESSION or $_GET
// config.php // ... $cfgObj->default_language = "en"; // ... $_SESSION["lang"] = isset($_GET["lang"]) ? $_GET["lang"] : $cfgObj->default_lang; include($cfgObj->dir_languages . $_SESSION["lang"] . ".php"); // index.php switch(isset($_GET["action"]) ? $_GET["action"] : "default"){ case PRODUCTS: case PRODUCT: case 3: // deal with products break: case ARTICLES: case ARTICLE: case 5: // deal with articles break: default: // deal with short-links // domain/3/555 <-- this would retrieve product with id 555 break; }DATABASE SCHEMA 可以设计如下
DATABASE SCHEMA could be designed like following
+-----------------+ +-----------------------+ +--------------------+ | products | | products_to_languages | | languages | +-----------------+ +-----------------------+ +--------------------+ | product_id [PK] |---| | ptl_id [PK] | |---| language_id [PK] | | price | |---| product_id [FK] | | | language_code | | status | | languge_id [FK] |---| +--------------------+ | ... | | product_name | +-----------------+ | product_description | +-----------------------+ * I use language_code as a primary key as well, and an index on all foreign keys更多推荐
多语言站点:URL上的语言作为dir,在其他位置文件.如何重定向
发布评论