2

I have a spinner in my app where the user chooses whether he wants to search by "Contains" "Starts with" "Ends with" or "Equals", The option user selects is stored into a json with other information and sent to server to retrieve results. Now I'm using:

String searchtypeval=searchtype.getSelectedItem().toString(); 

and adding searchtypeval into my json. The String-array in the spinner is

 <string-array name="search_options"> <item>Starts With</item> <item>Equals</item> <item>Ends With</item> <item>Contains</item> </string-array> 

But now I'm adding language support so in values-fr/strings.xml the string array for that spinner is

<string-array name="search_options"> <item>Commence par </item> <item>Égal </item> <item>Se termine par </item> <item>Contient </item> </string-array> 

Now if the user selects equals in french , Egal is stored into the JSON which of course the server doesn't accept. Is there any way I can make a connection between the french and the english strings.xml? All I can think of now is to use searchtype.getSelectedItemPosition() and hard code the value into String searchtypeval since I know which option is which position, but this seems very cumbersome, is there any method to solve this issue that is more elegant?

3
  • compare by position is the best bet, another option is fetch the search_options list from server, depending on the language user has selected. Commented Jun 10, 2014 at 5:55
  • possibly send an integer to the server instead of a string Commented Jun 10, 2014 at 5:56
  • @todd I can't make any changes on server side, have to send the english string Commented Jun 10, 2014 at 7:04

2 Answers 2

2

You can send to the server index of a selected element, but this isn't a good way, cause of index is not informated. The better way is sending readable string key to the server. See the following code:

1) create file nontranslatable_string.xml in res/values

<resources> <string-array name="search_options_keys"> <item>Starts With</item> <item>Equals</item> <item>Ends With</item> <item>Contains</item> </string-array> </resources> 

2) create your Item class like SpinnerItem

public class SpinnerItem { public final String key; public final String value; private SpinnerItem(String key, String value) { this.key = key; this.value = value; } public static SpinnerItem create(String key, String value) { return new SpinnerItem(key, value); } @Override public String toString() { return value; } } 

3) fill your adapter with values

 String[] keys = context.getResources().getStringArray(R.id.search_options_keys); String[] values = context.getResources().getStringArray(R.id.search_options); List<SpinnerItem> items = new ArrayList<SpinnerItem>(); for (int i = 0; i < keys.length; i++) { items.add(SpinnerItem.create(keys[i], values[i])); } spinner.setAdapter(new ArrayAdapter<SpinnerItem>(context, android.R.layout.simple_spinner_item, android.R.id.text1, items)); 

4) select your value

String valueForSendingToServer = ((SpinnerItem) spinner.getSelectedItem()).key; 

UPDATE

Or you can use another way and get neccessary value for any location you use:

 Configuration config = context.getResources().getConfiguration(); // Save originla location Locale originalLocal = config.locale; // Set new one for single using config.locale = new Locale("en"); context.getResources().updateConfiguration(config, null); // Get search_options array for english values String[] searchOptionsEn = getResources().getStringArray(R.array.search_options); // Set previous location back config.locale = originalLocal; getResources().updateConfiguration(config, null); String valueForSendingToServer = searchOptionsEn[spinner.getSelectedItemPosition()]; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the update bit is the thing I was looking for
1

You can reference string resources in the string-array for localization.

<string-array name="search_options"> <item>@string/starts_with</item> <item>@string/equals</item> <item>@string/ends_with</item> </string-array> 

and then in res/values/strings.xml:

<string name="starts_with">Starts with </string> 

and in res/values-fr/string.xml:

<string name="starts_with">Commence par </string> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.