I have a visualization web application (small project) that creates charts and graphs from a database and presenting them in a html/css template. All the strings are currently hardcoded in English inside the code (not in a special place/array/container, but echo’ed or < p >’ed as we go along). What do you think is the best way to offer localized version of my web app? I estimate that I have around 60-70 small strings.
I think I should create a lang.en.php file with the strings as constants strings variables, and include the file in my config.php (contains stuff like DB info etc) which is then is already included/required() in all the pages I use. Inside the pages, I will replace all the strings with their constant, eg TOP_10_WHATEVER_CHART.
If someone wants to create a new language, he copies the lang.en.php file, renames it and changes all the strings inside it. He then just has to change the include statement in config.php and everything is set.
Sounds good? Some of the other solutions I have seen seem a little overkill. If there is a more “standardized” or scalable way but still simple, let me know please.
What you describe is a pretty standard way that I have seen it done especially for smaller projects. The only issue with this approach is that you need N instances per language of your framework (unless you are doing something dynamically that I did not catch in the description above). You may want to create a function that takes in the string constant you are looking for and the language. The function returns the translated string. The language can be chosen based upon the http request and if you don’t support that language the function can return a default language.