3

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??

FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
Nabeel
  • 557
  • 4
  • 15
  • Do you expect the exception message to appear to the user, or only on the server? At any rate, you're looking for [internationalization (abbreviated i18n)](http://en.wikipedia.org/wiki/I18n). – Matt Ball May 06 '11 at 14:21
  • 4
    You should _never_ display exceptions to the end user! Use a real error page instead. – KingCrunch May 06 '11 at 14:22
  • This sounds like you're talking about multiple human languages, not (what I thought from the title) multiple programming languages. You might want to clarify. – David Thornley May 06 '11 at 14:22
  • @Matt Ball : The message appear to the user. I just want to display the error messages like, 'User exist', 'cannot find results', etc. – Nabeel May 06 '11 at 14:26
  • @David Thornley : I edited the title, thanx. – Nabeel May 06 '11 at 14:27
  • @kingCrunch : What do you mean by "real error page"? I don't want to display default error messages that may contain secret info. I just want to display custom messages like "cannot delete SOMETHING, you need to delete ANOTHER THING before." – Nabeel May 06 '11 at 14:30
  • @Nabeel: Much better, thanks. – David Thornley May 06 '11 at 14:30
  • @Nabeel: An uncatched exception is an error of the application, not of the user. If you receive an uncatched exception, than your application is buggy. But then you want to know, whats the problem is, so you must put some information in it, that shows some stuff from the internal structure. But THIS information is nothing for an enduser. However, as mentioned: An exception is an error of your application, not from the user. If he does something wrong, show it with a error page, not with an exception. – KingCrunch May 06 '11 at 14:34

3 Answers3

5

In an application that will be used with more than one human language, never hard-code strings that the user might possibly be exposed to. They need to be in some sort of resource file. You can hard-code things like SQL statements and internal logging information if you like, but nothing the user might see. I know how to use string resource files in Microsoft Windows (I internationalized two C# and one C++ applications), and there have got to be similar things for Mac OSX, Linux, and any other OS you're likely to use.

In a case like this, you're better off throwing something like a UserAlreadyExists exception and handing it off to some sort of error display routine when you catch it. Otherwise, you're dealing with the UI in the processing part of your code, and that's usually not what you want to do. Don't worry about the multiplicity of exceptions, since if you're localizing the application you're going to have a multiplicity of string resource file references.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
1

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:

userExists.English = "Unable to add new user, user already exist"
userExists.Spanish = "<my Spanish isn't good enough to even try>"
userExists.ClassicalMongolian = ...

Then when you throw the exception, you have something like

//currentLanguage indicates what the language of the current session is.
//the function lookupExceptionString must be able to look up the
//correct string based on the value of currentLanguage
exceptionString = lookupExceptionString(currentLanguage);
throw new Exception(exceptionString );

...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

FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202