2

In android, it is easy to translate strings that are in the strings.xml file. Is there another way I can translate a string in java? For example, I need to retrieve the string from a server, then translate it, which can't be done with the strings.xml file.

Is there any official documentation on this?

1
  • You could transfer a key that you map inside your app to a concrete string resource. Commented Dec 20, 2016 at 16:19

2 Answers 2

3

What you are looking for is an on demand translation. There isn't any free solution afaik. The best way for you is probably doing the translation on your server side and retrieving the correct string to the device.

You can call on your Android application the active lenguage with

Locale.getDefault().getDisplayLanguage();

And you can pass this value as parameter to your server.

Here, with a switch, an if or any other method, you can return the translated value of the string.

Hope this helps

Edit

Let's imagine you are calling the string from your server with something similar to

http://myurl.com/getMyStrings

Now you can add an input parameter to your call like

http://myurl.com/getMyString?lng=myLenguage

the myLenguage should be the locale result.

Now, server side, you can add some code. for example:

switch(myLngParam){
  case "English":
       return "My english string";
  case "Italiano":
       return "La mia stringa in italiano";
  case "Español":
       return "something else";
  default:
       return "default";
}

This way, based on your locale, you can have a different string.

NOTE: Obiouvsly this is an example, I don't know how you are calling the server but in any case for this goal you should add a parameter with the locale result

Sign up to request clarification or add additional context in comments.

5 Comments

So you are saying, I can manage to translate a string on my server without paying for on demand translation?
@espa_network yes, I don't know about any client-side solution. Maybe someone offer some tool like online api for translation or similar. I add a few rows in my question
Can you reference me to a on demand translation provider's documentation?
@espa_network client-side = on your phone = on demand. unfortunatly I don't know any on demand solution. I think the best approach for you would be the server-side work. I updated with a little example, consider it. Good luck!
@espa_network you can probably use something about google translate, but it's not that accurate imho
1

What I actually do on my server is creating at runtime an hashmap that maps keys from a language with values in another language (translations) built in initialization from an external dictionary file.

Then when a client requests a "server string" I let him pass me the locale language and I look in the hashmap for the corresponding value, returning it to the client.

Another alternative would be pass the strings to an external translation API like Google translate's, but this would add latency and costs. If you know which are the strings you need to translate before they are requested, you don't need it.

3 Comments

You said, " If you know which are the strings you need to translate before they are requested, you don't need it." I will not know because the strings will be app user's input.
Well, if you don't know them in advance, you need a translation API, or build yourself your translation engine, there's no other magic.
That is exactly what I needed to know. Can you reference me to official documentation about pricing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.