So I just started web development on my new job, meaning I only have very basic knowledge of php, my html, css and js knowledge is a lot better. I'm trying to build a basic multiple language site via php sessions but I just can't get it to work the way I want. I have an index.php file and two more folders called EN and DE in which I have the Englisch.html and the Deutsch.html file. The html files only contain some text and it's my goal to have two buttons (or similar) at the top of my site called EN and DE which switch the session to include the files in said folders and display the text beneeth the buttons and keep the language switch function on every page if I had more then one file per language. By default I want the language to be english so load the Englisch.html first. I'm guessing I need to create an if else statement on every html file that checks the session so something like: if session EN include Englisch.html elseif session DE include Deutsch.html, and on the index.php I somehow need to set the parameters for the session so something like: startSession EN include Englisch.html startSession DE include Deutsch.html ? I have no idea how far off I am and any help, espacially actual code examples would be greatly appreciated. I hope this described my problem precisely enough.
multiple language via session, two directories
1k Views Asked by joko AtThere are 2 best solutions below
On
Your attempted solution is going to bite you in the long run.
It may seem like an easy solution to switch between different files for different languages, yet assume that your website becomes more dynamic, instead of *.html files you want to deal with *.php files, and then would need to have the same logic inside each of your localized files. It doesn't scale well.
I recommend using a translation libary, there are many available, I had good success with symfony's translation, that you can also include in any php project.
Then translation becomes:
$translatedString = $translator->trans("My content");
And the translation can then be maintained in yaml files and depending on the locale, the right language is chosen and each untranslated string will default to English.
And now, whenever your logic changes it is just at one place where you need to adapt it.
I agree with K0pernikus in that your solution won't scale well. You can write a simple translation library yourself in under an hour and you can then test it to see if it will be robust enough for your needs.
The idea is to have language files without any logic in them. You simply call the file you need. All the logic about which language, file, translation key etc. is contained in your library.
I'll keep it very simple and down to a single file; of course, the actual translations will each need to be stored in a file too.
Directory structure:
Within each
messages.phpfile you need to return an array of translation keys and their respective translation. The translation keys are what you will use in your views. These files will become large with many hundreds or thousands of lines for larger applications. If you intend to keep this home-grown solution you'll need to implement caching. That being said, I have files with hundreds of translations and don't notice any significant performance hit.Next, you need a library to read these translations and return the actual translation text to you. This file could simply be a collection of helper functions or a full-blown OOP system. For simplicity, here is a collection of helper methods that get the job done. It does not implement parameterized substitution and you can add that feature relatively easily by making
t()accept a 2nd argument, but that is a whole different topic for another time.The main method in here is
t(). It is very simple and accepts a single translation key. Ex.applicationNameorgreeting.Firstly, it tries to determine which language to use. It does this in a sequence of priority: URL, session, browser, fallback.
lang. If you think about it, that makes sense because a user intends to switch their language by clicking a link that says "English" or "French".$defaultLanguage.Once a language has been found, it puts it into the session so the next request doesn't need to go through all that again. It also loads the appropriate
messages.phpfile based on the discovered language.Finally, once the language has been found and right file has been loaded into memory it searches for the given
$keyand returns the appropriate translation. If the$keyis not found then it simply returns the given$keywhich will show up in your views so you know something went horribly wrong and you need to start debugging.To use it, save these methods into a file called
translator.phpand then include that file in every page you want to use translations.Sample:
Edit
The last thing I will say is that there is a whole world out there of localization, internationalization (often abbreviated as i18n) which you can learn about.
In my example, I simplistically called it language but often people referer to it as locale but that has a different meaning and syntax. For example, there is a difference between en_CA and en_US and en_GB; all of which are English but have regional differences.