views:

45

answers:

1

Hey everyone,

I am developing a mobile application (BlackBerry) with Java that makes use of the Google Translate API. I have it set up so that a user can specify a language before logging in, and then once they log in, I have wrappers around every piece of text displayed to screen which will translate the text based on the language chosen.

My problem is that multiple things need to be translated on a given screen. Therefore, I have to access the Google Translate API multiple times per screen. To do this, I open an HTTPConnection on a new thread. So I pass my Screen Interface to the Translator class, and then the Translator class will call my callback methods for that Screen (requestSucceeded() or requestFailed()).

Now, lets say I translate both the Screen title and the Screen body text. That's two separate HTTPConnections. So when the title is translated, I want my requestSucceeded() method to setText on my title LabelField. When the body text is translated, I want my requestSucceeded() method to setText on my body LabelField.

What is the best way to have my requestSucceeded() method differentiate between different fields to be updated? Currently, I am passing to the Translator two things (via a String[] array): The text to be translated and a "cookie" describing the item being translated. I then use if statements in my requestSucceeded() callback method to update the appropriate field based on the "cookie" (which the Translator will send back along with the translated text).

I feel like there has to be a better way to do all of this. Does anyone have any suggestions?

Thanks!

A: 

First, I suggest you read up on the Command Pattern. It is basically a design approach that can be used towards encapsulating each of your translation requests in an object that knows how to handle the results. I then suggest having a collection that these commands can be inserted into, and that your translation thread can get its input from. This way, you could (depending on how the API works) process a batch of translation requests on the same HTTP connection.

octo
I will take a look at the design pattern, thanks!
behrk2