Multiple Localizable.strings files in one iOS app
Asked Answered
D

4

46

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 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?

Dutyfree answered 27/1, 2011 at 0:21 Comment(0)
A
97

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:

  1. The string to localize
  2. The table name, which is just a file name
  3. 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.

Alexandraalexandre answered 7/4, 2011 at 16:34 Comment(5)
Thanks! I enjoyed Matt's article too up until he said "avoid localizing images - it will cause you pain and suffering", meh.Dutyfree
No problem! About localized images, it's very true. It's a pain to maintain images. It's much better to draw text on top of the image.Alexandraalexandre
Great! Now I can create a reusable component with its own classes/resources/localizable strings all in one directory and drop it into new projects, without having to worry about merging the global Localizable.strings file!Elrod
In swift: NSLocalizedString("String", tableName: "Table", comment: "Comment")Kohlrabi
@scot if these multiple strings are in different modules or frameworks in the project will this still work?Librate
A
0

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

Alvinaalvine answered 27/1, 2011 at 0:31 Comment(4)
But what does "the right folder" mean? In my case I want to have 2 files with the same name ("Localizable.strings") and I wonder how this could work.Dutyfree
ok, the good news is that you don't have to really worry about that... i mean: if you have just 1 single file called "Localizable.strings" now, just try to do as i said with that (select it, open info, got in the "general" tab, press the "make file localizable" button, in the new opened-window chose general-tab and press the "add localization" button, then chose a new language for that file)... when you do that xCode create the "right folder" for you move the old file "Localizable.strings" in the english filder and the new (duplcated) file in the italian (or what else) filder, so just edit itAlvinaalvine
? that's what you'll get. you'll have 2 different files with the same name in 2 different folders, one in the folder "Italian.lproj" and one in "English.lproj", and their text could be different, you'll write the italian text in one and the english in the other... wasn't this your question about?Alvinaalvine
No, I was asking about a more specific case of localization. In my case, I have a module in my app called ShareKit that is localized (has en.lproj and de.lproj fodlers with Localizable.strings inside) and I want to add my own localization to my own elemetns so I'm adding another pair of en.lproj and de.lproj folders with a strings file of the same name inside, Localizable.strings. But it's normal that Cocoa Touch doesn't like this. So i'm looking for a workaround.Dutyfree
A
0

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

Alvinaalvine answered 29/1, 2011 at 13:20 Comment(3)
sorry for my first version of this second answer, i had some problems with copy/past in safary...Alvinaalvine
mhm... i tried it in my project... for some unknown reason the dictionary file is always read in its default language, ignoring the settings, so you have to do different files and call the good one by yourself when you use: NSString *bundle = [[ NSBundle mainBundle] pathForResource:@"myDict-ita" ofType:@"plist"]; or: NSString *bundle = [[ NSBundle mainBundle] pathForResource:@"myDict-eng" ofType:@"plist"]; and you can know what language is setted just reading a localized word in the file used by ShareKit...Alvinaalvine
I CAN open it's Localizable.string files and add my strings there. But in that case I am altering SharerKit and I have problem when I want to update it later etc. So I was wondering if there is a nice solution to have an extra Localizable.strings file and still use NSLocalizedString() macro, but I guess there is no such thing. You solution with the NSDictionaries sounds good, but still I would have to change my localizing habits :)Dutyfree
N
0

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.

Nephrosis answered 7/4, 2011 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.