i have a string in strings.xml file. clicks on some part of this string redirect to a task. that some part where made based on the index of the string.
Now i am trying to translate it to french but i am getting index out of bound exception as its less then the length of English strings.
Could anyone please say, what will be the best way to handle this scenario?
String separation is one thing we can do.
but i want to handle it in one text view itself.
Code for the English String:
SpannableString spannableString = new SpannableString(getResources().getString(R.string.desc));
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Log.v("dosomething", "dosomething");
}
@Override
public void updateDrawState(TextPaint ds) {
Log.v("task one", "task one");
}
};
spannableString.setSpan(clickableSpan, 87, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mDesc.setText(spannableString);
mDesc.setMovementMethod(LinkMovementMethod.getInstance());
Then you will have to detect the language that the device is using whether it uses the french strings or the english strings then, put this in each condition defining the length for the given strings file. e.g.
if (Locale.getDefault().getLanguage().equals("en")) {
spannableString.setSpan(clickableSpan, 87, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if (Locale.getDefault().getLanguage().equals("fr")) {
spannableString.setSpan(clickableSpan, 50, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
just change the start and the end length depending on the string that is in the strings file.
You can use annotations and then set the ClickableSpan
on the annotated part. That way, the translations should contain that information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With