1

I am working on REST server side.

I will update the error_code and error_message in HTTP headers while responding to the client. This is fine until error_message is static.

e.g. error_code    : PC_USER_007
     error_message : You are logged out!

Client side will look at the error_code(PC_USER_007); in corresponding properties and they will do the localization. So now the GUI will show you the same error_message in Chinese if the user belongs to China.

If it is dynamic error string like below,

e.g. error_code    : PC_USER_008
     error_message : You have tried 2 attempts; only 1 attempts is remaining. 
Here 2, 1 are dynamic values..

Now GUI side how we can convert this error message to other language.

Any framework (struts, spring...) already supporting this kind of localization?

This is just opposite question to How to replace a set of tokens in a Java String?

Server side i followed the above link; now i want opposite case in client side.

Community
  • 1
  • 1
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
  • you could use ANTLR and write some kind of Grammar based on all possible "Dynamic" error messages. if i got it right the error messages always are in english. Then you just need to write the grammar that catches stuff like "You have tried " number " attempts; only " number "attempts is remaining". then with antlr you can access these numbers after the parsing step and generate your new locallized string – ITroubs Mar 20 '13 at 14:06
  • 2
    Passing a full error_message is pretty strange. Usually the server passes the error_code and a set of values. Then client side you have templates for that error code in many languages looking like "This is argument {1} and arg {2}", "Ceci est argument {1} et arg {2}", etc... and you replace {1}...{n} with the received values. – Bruno Grieder Mar 20 '13 at 15:04
  • +1 @BGR Now i have some light over here – Kanagavelu Sugumar Mar 20 '13 at 18:15
  • @KanagaveluSugumar I will make my comment an answer; it will be easier for others to comment on it. – Bruno Grieder Mar 21 '13 at 06:09

4 Answers4

6

Having the server pass a full error_message is not very appropriate.

Usually the server passes the error_code and a set of error values. e.g.

error_code: PC_USER_008
error_values: "42", "a value"

The client holds templates for that error_code in many languages looking like

"This is argument {1} and arg {2}" 
"Ceci est argument {1} et arg {2}"

etc..

then the client performs a simple string replacement of placeholders {1}...{n} with the received values

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
1

If I were you I'd first move the error codes from HTTP headers to a structure in the response content (say json). Headers are not hierarchical and have no structure. You are better off including them in the response with an appropriate status code.

As for localized messages and their templating, I have not come across any REST service that returns an error response without an error message. Take a look at the zoho, django (the REST API), S3 for example. They all return codes and messages.

  1. The problem with not returning a message is that your client will never know immediately what the problem was. This makes debugging harder.
  2. Clients will also not have access to new error codes that you introduce into your server code later, which reinforces #1.
  3. Deprecating error codes or changing their error messages is no longer in your control.

Implement templating but do it on the server side. Ask the client to send you an id that identifies its locale. Based on the locale the error message can be changed. Your clients never have to update their code due to server side changes and you will always have the correct localized message to show to the client.

Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
0

Usually, if you're dealing with multilingual applications, you should use an ID to identify your texts in server and in the GUI and you should use in the error_message field the text ID. There are some frameworks which use as ID the english text.

Rafa
  • 2,328
  • 3
  • 27
  • 44
0

Its not a good practice to send the entire error message in the response headers. You can send a error code and implement locale specific messages for those codes.

in javascript after a success request, use the callback methods to track the failure response code. create a bundle to read different properties file for specific locale user and read the key value and show the proper error message.

ex:

Properties file/ JS file
1. EnglishLabels.properties
2. MexicanLabels.properties
3. JapaneseLabels.properties

var bundle = read the specific property file and save the map (key : 0001 , value : Invalid Login)

later after the success request, use the callback method to show the locale specific error message.

Regards Punith

Punith Raj
  • 2,164
  • 3
  • 27
  • 45