By now my site was only a one-language site.
I had my files on (suppose):
www.mysite.com/files/index.php
www.mysite.com/files/header.php
www.mysite.com/files/page1.php
www.mysite.com/files/page2.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.
By now I had (badly) on header.php
this line:
define('LNG', 'en');
and I had a header.php
file placed on each language directory:
www.mysite.com/en/header.php
www.mysite.com/de/header.php
...
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!
How can I solve this problem? I would mantain my files on www.mysite.com/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.
Sort of:
www.mysite.com/en/index.php
which in fact loads
www.mysite.com/index.php
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.com/en/index.php
I obviously get a 404
...
Dealing with multi-language web-applications
LANGUAGE CONSTANTS
could be placed in language files within a language directory like thiswhile language files may look as following
or they could be stored into database as shown below. I've used both but I prefer the first solution
FRIENDLY URLs
may look like these belowhaving in mind rules that should be defined in
.htaccess
fileor you can redirect the request (as following) to an entry point and parse it with
parse_url()
.ACCESSING THE lang VARIABLE
- On multi-language projects variablelang
has a special significance, otherwise it would be treated and passed around just like any other variable usingGET
,POST
,COOKIE
,XMLHttpRequest
,HTTP headers
,HTTP referer
,URL hash parameter
,database session
,APC (Alternative PHP Cache)
...If you care about
UX
andSEO
thelang
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.The snippet below shows how to access it using $_SESSION or $_GET
DATABASE SCHEMA
could be designed like following