I have 2 languages in my website. I want to include appropriate file according to selected language. For example, <a href='index.php?lang=en'>English</a>
and <a href='index.php?lang=tr'>Turkish</a>
. If user clicks over first hyperlink (English) I want to display this information (with HTML tags):
<h1>Hello</h1>
How are you
Otherwise:
<h1>Selam</h1>
Nasılsın
I also dont want user selected language to change if user go to other page inside website.
How can I do that?
You could use .ini files to hold the translated versions of you text for example, en.ini will contain:
While tr.ini will contain:
Both are simple text files (saved in UTF-8 format). Now you can see that the strings have common names in all languages (hello & greeting) which cab be used in the code like this:
One thing you should be concious of:
parse_ini_file
reads a file from the file system, you must validate the value in $lang very carefully! don't rely on user input in any way for the value of$lang
as it might be malicious (link topage.php?lang=en
is not good enough). Check the value of $lang against your own white list for example:$langs = array( "en", "tr" );
and if that fails, redirect to a general error page or something...I guess the best way to map all your words in the database , detect which language is , after that retrieve all the data from the database by mapping the language ID (EN for English and TU for Turkish) and the specified word..You can use "If , else" for the same approach ...
The best solution for you is named Gettext
See :
PHP manual Gettext
Localizing PHP web sites using gettext
This is, without any discussion, the best way to have a multi language web site. Gettext is faster than any other solution.