2014년 8월 31일 일요일

안드로이드에서 다국어 작업시 String.format 사용

안드로이드에서 스트링 리소스 작업시

String.format 사용시 %s를 사용하게 되는데

이때
Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" 
 attribute?
- error: Unexpected end tag string
라는 에러가 발생한다.


xml에 formatted="false" 를 추가하면 해결이 되지만


다국어 작업시 두개 이상의 스트링 %s가 포함될 경우 다국어 문맥에 따라 %s의 순서가 변경 될 수 있다.

이때 하드코딩으로 변경하지 말고 안드로이드에서 권고하는 방식을 사용하자.
http://developer.android.com/guide/topics/resources/string-resource.html

Formatting strings

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

댓글 없음:

댓글 쓰기