Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing Exception(s) in a multilanguage (e.g. English and French) application

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

like image 257
Nabeel Avatar asked Dec 28 '25 16:12

Nabeel


2 Answers

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.

like image 131
David Thornley Avatar answered Dec 30 '25 06:12

David Thornley


You want to internationalize your application. There are lots of questions on this already.

  • What are good tools/frameworks for i18n of a php codebase?
  • My multilingual website with only basic php (without zend_translate, gettext, etc...) will I have problems in the future?
  • How to build PHP application with multiple language support (English, French, Chinese etc..)
  • Design considerations for internationalization
like image 32
Matt Ball Avatar answered Dec 30 '25 06:12

Matt Ball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!