Genstrings on the fly

Discussion in 'iOS Development' started by boopyman, Apr 25, 2010.

  1. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    Hey guys,
    I'm making an app where the user should be able to modify a Localizable.strings file. This seems pretty simple, except I have no idea how to recreate a Localizable.strings file without using genstrings (on the fly, I guess), which only works with XCode. Is there any way to modify a Localizable.strings without a computer?
    Thanks in advance,
    Elton
  2. Dabone

    Dabone Well-Known Member

    Joined:
    Sep 24, 2008
    Messages:
    2,231
    Likes Received:
    48
    Device:
    4G iPod touch
    You mean through ifile or something like that?
  3. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    No, through the actual application. Like an objective c function, or even a ported version of genstrings.
  4. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    A .strings file is nothing more than a plist with fancy (legacy NeXT, I think) "syntax." You should be able to use the standard NS[Mutable]Dictionary APIs to access and interact with them.
  5. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    Thanks for the info man, it's great stuff

    Please Register or Log in to view images


    Just wondering, when I save my NSMutableDictionary using writeToFile, it saves it as a plist even if it has a .strings extension. Can I convert a plist to .strings? Or is it possible to directly save the dictionary to .strings?
    Thanks,
    Elton
    ------------------double post merged------------------
    Sorry to double post, but I may have found an answer to my problem. If I use the plutil tool on the .strings (actually a plist) file, I get the exact format of a .strings file, except I have curly braces at the top and button of the output. Once I'm able to remove these from the output, I can save it to a file, this time a .strings, and I should be able to achieve my goal.
    Now I have the obvious question, how do I remove the curly braces from the Terminal's output?
    Thanks to everyone,
    Elton
  6. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I don't see any curly braces when I run plutil...

    I don't see how hard it is remove them, but if you need automation, a perl one-liner with regex would do, otherwise a small C program like this would work.

    [objc]
    #include <stdio.h>
    int main() {

    int ch;
    while ((ch = getchar()) != EOF) {
    if (ch=='{' || ch=='}') continue;
    else putchar(ch);
    }
    }
    /* edit: the 'continue' statement is not necessary in this particular program,
    but in a slightly more functional program, in which you were keeping track
    of how many chars read/written, etc. you may want to
    skip to the beginning of the loop */
    [/objc]

    just redirect stdin (or pipe input) and stdout at the command line to write to a file.
  7. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    Thanks for the information Axis, I figured it out using sed.
  8. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    You don't NEED to do that; NSBundle reads the .strings files the same way we do (maybe not exactly, but it reads them using a normal plist parser). You don't need to put the effort into making them look like "regular" .strings files.
    In addition, the Binary Plist format (I'm assuming that's the default on the iPhone) is also slightly more efficient and takes less space.
    ------------------double post merged------------------
    FYI: There is no real "format" of a .strings file; it's just a standard dictionary:
    [OBJC][[NSDictionary dictionaryWithObjectsAndKeys: @"Haus", @"house", @"Zehn", @"ten", nil] writeToFile: @"/Applications/RandomlyLocalizedApp.app/German.lproj/Localizable.strings"];[/OBJC]

    [OBJC]// RandomlyLocalizedAppDelegate.m
    /* ... */
    NSLocalizedString(@"house", nil); // would return @"Haus" on a German iPod/iPhone/iPad
    [/OBJC]

    I know most of this is kind of obvious, but I just wanted to elaborate that there is no need for curly braces, etc.
    That's just the syntax of the old-style plist format (or -[NSDictionary description]).
  9. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    I see what you mean, but I am trying to make a .strings file to replace SpringBoard.strings... I assume that it doesn't work with a plist file.
  10. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    SpringBoard.strings is no different.

Share This Page