0

My application supports some languages through Localization. When user selects any language, application is set for that particular language.

Application interacts with server. Problem is, when user is in some other language other than English, that language data is going to the server.

So, irrespective of the language user selected, always English language data should go to the server.

Please let me know how can I do it.

Example:

I have an array of strings stored in strings.xml for all the languages. When I pick any item from the array through spinner, it gives me particular language string item. What I want is, get English language strings irrespective of language user chosen.

Braj
  • 2,164
  • 2
  • 26
  • 44

3 Answers3

1

I thought this would be a solution: Load language specific string from resource? But it's tested and doesn't seem to work. A naive solution is to add the strings you want to send to the server in English to all string.xml files in all languages and then call these strings when you need to send them to the server, or simply have constants with the english strings in your code:

public static final String STRING_TO_SEND_TO_SERVER = "your_string_in_English";   // Use this string to send to server and the strings from the strings.xml file for your UI.
Community
  • 1
  • 1
logcat
  • 283
  • 2
  • 12
  • This looks promising. Only thing is that, need to change Locale back to the user selected after getting my strings. – Braj Oct 16 '15 at 09:52
  • The server should convert the strings to the necessary language. If you want to know at runtime which language the response should be converted to, put some identifier as a string in each of your string.xml files and get this string at runtime. This can even be a numeric id (English - "1", Spanish - "2", ...). Then forward it to the server so that you know in server side to which language to convert. – logcat Oct 16 '15 at 10:17
  • Note that saving the strings to be sent to the server as constants is better than using more strings in the string.xml files, because if you do use your xml files, then every time you add support for a new language you must remember to add the strings as well and each time you want to change the strings you'll have to pass through all xml files and change them all. Having a constant in your Java file is much neater. – logcat Oct 16 '15 at 10:20
0

You can use the values folder: the strings contained in it are always use per default.

You then just have to remove the strings you don't want to use for the others languages from the strings file in their respective folder. Your app will then take the one in your values folder, which will be in english.

Hope it helps.

Virthuss
  • 3,142
  • 1
  • 21
  • 39
  • How can I use values folder? – Braj Oct 16 '15 at 09:09
  • It works exactly like your other values folder (like values-en, values-fr...) except this one is used per default. Create your file strings.xml in it, and just add the strings you want to use whatever the language is in it. – Virthuss Oct 16 '15 at 09:10
  • When I say `getString(R.id.my_string);` it always returns respective language strings. What I want is, get English language strings irrespective of language user chosen. – Braj Oct 16 '15 at 09:31
  • it's was I said in the answer: remove the specific string you want to always have in english from ALL your strings.xml file EXCEPT the one in the values folder. Then it will always use it, whatever your language. – Virthuss Oct 16 '15 at 09:35
0

Generally, xml resources should help you to modify the UI of your app. If you have to use some sort of constants to talk with your server, then, maybe it's not regarding the strings.xml files.

You could store those server-values in other ways and location.

EDIT TO RESPOND: I get it now. You could create a specific string.xml file for you spinner (or whatever you want), maybe named strings_for_spinner, and inside you enter values that are nowhere else already defined. In that way, the system is forced to use those strings (and, in your case, in english language).

Simone Leoni
  • 316
  • 1
  • 9
  • They are not server related constants. Say for ex, I have an array of strings stored in strings.xml for all languages. When I pick any item from the array through spinner, it gives me particular language string item. What I want is, get English language strings irrespective of language user chosen. – Braj Oct 16 '15 at 09:34