开发者

Restrictions on resource name or how to do generic internationalisation

Currently porting an iPhone application to Android. I've encountered an issue that I haven't been able to overcome.

On the iPhone, translating your text is trivial. Put all your strings/text in Localizable.strings in the format: "sentence to translate" = "sentence translated"

The original string can contain any characters of any type.

Trying to reproduce something similar on Android, in the strings.xml I added all keys like: sentence translated

Then I was hoping to do something like:

int resID = getResources().getIdentifier(tmp, "strings", "com.name.app_name"); where tmp contains the string. Once I get the resID, I can get the translated string with findresourcebyId.

Unfortunately, found out that it ain't so easy. The name of the string can't contain space, can't start with a digit and so on.

Eclipse doesn't show me anything, but when trying to run the code, I simply get a "Your project contains errors. Please fix them before running your application" and that's it. Can't see anything in the error log etc..

So, what are the restrictions in the name of a resource on Android. Or is there a way to circumvent it and easily replace one string with anot开发者_如何学Goher. I'd like to keep the original dictionary of text I was using on the iPhone to keep things simple and not introduce any unnecessary errors.

Thanks


You basically just create a new values directory for every language you want to support using following naming pattern values-en for English localization and so on. It uses the two-letter ISO 639-1 standard.

Now in every language values directory you have your strings.xml. In this XML file every string has its own ID and its value (translated string). The IDs are the same in every strings.xml in the different language directories just the string content differs.

For an in depth read in this I recommend the Localization article on the Android developers site.

EDIT: You can only use uppercase, lowercase, numbers and underscores as a string identifier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜