In my applications I make use of Exception(s) to control the flow.
I use something like this:
throw new Exception("Unable to add new user, user already exist");
This method is perfect in an application with one language. However, when I intend to make a multilingual application, I don’t know what to do.
The previous line of code should produce a message in the default language that the user is using.
What are some possible solutions??
The general solution is that you have some setting to indicate the current language that the message should be displayed in. Then you have a file or a database table (though this might not be good if messages about database connection errors are stored in the database – you want the error messages as easily accessible as possible – might be best to load them ALL into some cache when the app starts up) somewhere that contains all your error strings, and in multiple languages, such as:
Then when you throw the exception, you have something like
…But luckily for you there are frameworks that already do a lot of it. Do a web search for “php internationalization”, or “php i18n”, The first link I found was this:
http://php-flp.sourceforge.net/getting_started_english.htm