views:

42

answers:

1

Hi.

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 another. 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

A: 

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.

Octavian Damiean
I'm afraid that you haven't read my original predicament. I have read the Localization article, but it doesn't apply. I have already a table with key = value. I do not want to change it. There are thousands of translated keywords there. The issue is about the characters a key can contain.
jyavenard
@jyavenard: Seems I have misunderstood you. What is it now what you are trying to do? Localization or something else? EDIT: I see.
Octavian Damiean
Here is an example of the key/value used. "2-RL" = "Number of Rails for 2-Rail Fence". In this case, compilation fails because the name start with a digit (2) and i contains a "-" which for some reason it doesn't like either. On iPhone I would do [[NSBundle mainBundle] localizedStringForKey:(@"2-RL") value:@"" table:nil]. This would return the string I need.
jyavenard
Ultimately, it is localization. It just happens that the key/value used, in the original code, the key was either a 4 characters code or the whole english sentence. Say your French localization file on iPhone is "my name is jean-yves" = "je m'appelle jean-yves". Set your iPhone to English it shows the first part, in French it shows the 2nd one. On Android, I would have expected the key to be like <string name="2-RL">Number of Rails for 2-Rail Fence</string>. After all there are already entries like <string name="app_name">My Application</string>
jyavenard
@jyavenard: Got you know. Well another idea would be to use Perl for example to format all identifiers for you. I mean let it convert all "-" characters to "_" and so on. I don't really see another possibility.
Octavian Damiean
jyavenard
@jyavenard: "So I was wondering what were all the restrictions." This is just a wild guess but not starting with number, no spaces... sounds like an ID value in XML. Again, an ID must be unique and lexically like an NCName in XML http://www.w3.org/TR/REC-xml-names/#NT-NCName
jasso
@jyavenard: The reason why it doesn't take minus signs is, cause every resource id is an variable stored in R.java, which you can access by R.string.some_string_id. If you'd have minus signs there, compiler couldn't tell if it's part of the variable or if you're trying to do an substract operation
Tseng