views:

394

answers:

2

How is text in a database localized for the iPhone? The examples I've seen have hard coded strings in the .m file or .xib. For example:

NSString firstName = NSLocalizedString(@"First Name", @"This is the first name");

What happens if the strings you need to localize are in the database? I haven't found discussion of this type of localization. However, I imagine it is extremely common given that many iPhone apps use sqlite.

+2  A: 

NSLocalizedString is simply a preprocessor macro that turns into a call to NSBundle's localizedStringForKey:value:table: -- so basically you can use strings fetched at runtime instead of a hardcoded string and pass that to NSBundle. The downside, of course, is that there's no quick way to compile a list of all the strings that a translator needs to localized.

Example:

NSString *key = fetchKeyFromDB(...);
NSString *localizedString = [[NSBundle mainBundle] localizedStringForKey:key value:@"default string" table:nil];
Daniel Dickison
Can you give a code example of what you're referring too?
4thSpace
Answer edited -- how's that?
Daniel Dickison
I haven't created any additional resource files so nothing is happening at this point. All of my text is in the database. Am I supposed to create string files for each language I want to localize and for each database row? That's a lot of stuff to keep in sync. If I'm localizing for five languages, that means I'll need five string files for each database (English) row entry right...since the database is all English? http://developer.apple.com/iphone/library/documentation/MacOSX/Conceptual/BPInternational/Articles/StringsFiles.html#//apple_ref/doc/uid/20000005-SW1
4thSpace
Yes, you'll need 5 .strings files. Of course, keeping 5 languages worth of localized strings is a lot to keep in sync no matter how you implement it.An alternative is to avoid using Cocoa's localization API and instead keep all of your localized strings in the database (i.e. a table with columns for key, language, country, and string).
Daniel Dickison
How would you implement this? Suppose I have name_en, name_fr, name_es in the DB/plist. How would you invoke the correct field for the current active locale?
mga
A: 

I was just wondering the same thing and couldn't find any mention of how this was dealt with (and hence how I ended up at this post). OTTOMH, I was thinking that you may just have different databases for each supported language. Perhaps still follow the general internationalization/localization guidelines and create a separate dir for each support language such as en.lproj and it.lproj and the then name your dbs as "en_data.db" and "it_data.db" (or whatever naming convention you prefer) and place them in their respective dirs. The trick is to then detect the runtime language in the app and load the appropriate database for that language. Again, this is all just a guess, I've never done it in practice (like I said, I was wondering the same thing)

Doug