Is there a way to make the localization or Localizable.strings read from directories outside NSBundle?

I am trying to make my app read localizations from a file that is downloaded via a server, is there a way good way to do this?

Thanks in advance.

有帮助吗?

解决方案

You will have to write your own MyLocalizedString function which reads the file manually. A .strings file is actually an old type of property list, so it can be read using the NSPropertyListSerialization class like this:

id plist = [NSPropertyListSerialization
               propertyListWithData:[NSData dataWithContentsOfFile:stringsFilePath]
                            options:0
                             format:NULL
                              error:&error];

plist is just an NSDictionary, so you can read a string value from the result like this:

[plist objectForKey:@"my_string"];

You should probably implement some sort of cache so that you aren't parsing the whole file for each string lookup.

Note that if you are using genstrings and the other related command line tools, you can use the following option to specify the name of your custom lookup function:

Usage: genstrings [OPTION] file1.[mc] ... filen.[mc]

...[snip]...

-s substring             substitute 'substring' for NSLocalizedString.

If these strings files are being uploaded to a server, you can improve performance in the app a bit by first converting them to the binary plist format (this is what Xcode would normally do during a build):

plutil -convert binary1 myStrings.strings

其他提示

You will have to put together the pieces yourself. There isn't any built-in facility to do that. Your localizations can just be serialized dictionaries with the development language string as the key and the localized string as the value. You can serialize as a plist, which may be most convenient to edit, or using a keyed archive, which may be more compact.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top