-2

Reading and writing .plist files is easy. So is .json files. Is there a similarly easy way for reading and writing the .strings files that are used for localization? Or is there third party code you'd recommend for the purpose?

I'd love not to reinvent the wheel, but I have been unable to find it myself

Cheers

Nik

Edit: this solution has been suggested, and although I was pleased to learn there was such a convenient function for writing and reading .strings files, it looses the comments. I'm really looking for a non-lossy solution

Community
  • 1
  • 1
niklassaers
  • 8,480
  • 20
  • 99
  • 146
  • look at this: http://stackoverflow.com/questions/3153600/iphone-reading-from-localizable-strings-file-as-a-key-value-in-a-dictionary – Dharmesh Kheni Jul 07 '15 at 12:57
  • Thanks for the link, Dharmesh. That is very interesting indeed, but it discards the comment part of the format when writing back the strings file. I was hoping for a solution that is not lossy – niklassaers Jul 07 '15 at 13:32
  • lmao at 3 downvotes. like what. – A O Jul 07 '15 at 18:27
  • Glad you're amused, @MattyAhOh, I've been looking for input and gotten a few partial answers but none that are not 'reinvent the wheel' as is my fall-back option. I think it'd be nice if people downvoting would leave a comment as to why they think it should be down-voted. – niklassaers Jul 08 '15 at 06:46
  • 1
    I've had the same problem, people downvote questions thinking they are easy, but don't realize you're looking for a unique solution. they can't even be bothered to find a duplicate question to justify the downvote – A O Jul 08 '15 at 14:37

1 Answers1

0

So a .strings file is just a text file, if you open it up with another plain text editor you can confirm it.

So you can just use the class method on NSString, -stringWithContentsOfFile:

Example:

NSString *fileContents = [NSString stringWithContentsOfFile:@"myapp.strings"];

You can also use the instance method -componentsSeperatedByString: if you wanted to split the string into an array of lines

NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];
A O
  • 5,516
  • 3
  • 33
  • 68
  • Thanks @MattyAhOh, but this is the start of writing a strings parser and writer myself. Which I can do just fine, I just assumed there must be some component out there that I should use but hadn't come accross yet. This is probably what I'll end up doing, then – niklassaers Jul 08 '15 at 06:47
  • 1
    Yeah sorry mate, this is just how I would go about it, I am unaware of any API that does this for you. Maybe you could write one ^^ – A O Jul 08 '15 at 14:31