string formatting issue in android

911 Views Asked by At

I am facing an issue with "string.format" in android application. In my application when the user changes his language preferences from default (english) to any other foreign language (japanese,german etc) the variable string positioning is giving a force close error. Please refer the code below:

temp = String.format(locale,getResources().getString(R.string.temp_string), value, name);

where, temp_string = "The parcel number %1$d belongs to %2$s" for default selection (english) when other languages are selected in some of them %2$s comes before %1$d . Due to which the the application force closes. Is there a way around to dynamically handle the variable strings(value,name).

1

There are 1 best solutions below

0
On BEST ANSWER

I'd do something like:

temp = getResources().getString(R.string.temp_string, value, name);

As you see, getString() method can also receive parameters to format. Then, place different strings resources on different folders. For instance:

res/
   values/
       string.xml <--- here you put "The parcel number %1$d belongs to %2$s"
   values-de/
       string.xml <--- here you put "The parcel number %2$d belongs to %1$s"

I'm just giving you an example; I actually do not know how germany order is. I just want to explain what you actually have to try.