Is it possible to have multiple components that have their separate localizations in an app?
For example I want to localize strings in my app but also use ShareKit which is localized itself (so my bundle will contain 2 Localizable.strings in different开发者_StackOverflow中文版 locations). So, ShareKit contains folders like en.lproj and de.lproj that contain Localizable.strings files and I want to create the same for the rest of my project but keep them separate.
I have tried just keeping them separate and that resulted in the strings of my app not being used. Any ideas?
There is a way to do this that is quite simple.
There are several macros for localizing, one of which is NSLocalizedStringFromTable()
. This takes three parameters:
- The string to localize
- The table name, which is just a file name
- The comment, just like in
NSLocalizedString()
If you use a table name then you can have another strings file, i.e. if I do NSLocalizedStringFromTable(@"name", @"MyStrings", @"whatever");
then put my strings in MyStrings.strings
it will use that.
See Matt Gallagher's post about this.
every file/resource can be localizable, also the IB .xib files or images jpg/png, you just need to select a file in your project, open info, go in the "general" tab/section and press the "make file localizable"... then you can add any language you want, xCode will create a new file duplicating the first one and will put it in the right folder...
luca
It is not possible to do what you are asking. You can have multiple Localizable.strings files, but only one per language in their respective .lproj folders.
I understand that you do not want to edit the ShareKit Localizable.strings files because that would be a pain when updating but I think you need to look at how much work it would actually take. Depending on how many languages you need to support it may be less work to localize your own strings and add them to the bottom of the ShareKit Localizable.strings files than to implement your own localization scheme.
BTW, to detect what language the device is currently set to you can use:
NSArray *preferedLocalizations = [[NSBundle mainBundle] preferredLocalizations];
This gives you an array of two letter language code strings. The item at index 0 is the currently selected language. This could be helpful if you need to make logic decisions based on the language.
oh ok, I don't know ShareKit, but i guess you cannot edit its text files and just add your new words to that files, isn't it?
then you may consider to create your own "dictionary" file and try to localize it, then read it and put it in a NSDictionary where you can read the single words/value... you can insert a record in your file "myDict.plist" with key "greeting" of type String with value "ciao" for the italian one and "hallo" in the english one
NSString *bundle = [[ NSBundle mainBundle] pathForResource:@"myDict" ofType:@"plist"];
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:bundle];
NSString *greetingsTranslatedInTheGoodLanguage = [savedStock objectForKey:@"greeting"];
// just to try what you have read:
NSLog (@"translated by your own dictionary: %@", greetingsTranslatedInTheGoodLanguage);
[savedStock release];
luca
ps read the comments too...
精彩评论