IPhone localization: Is it possible to translate nib files easily without having to copy the nib for each language?

前端 未结 6 1388
清歌不尽
清歌不尽 2021-02-02 02:41

I\'m trying to find a manageable way to translate every visible string in an iPhone application. The official apple documentation says to use .strings files for programmatic st

相关标签:
6条回答
  • 2021-02-02 02:47

    The best you can do is recursively loop through each view and then set the text on them:

    static void translateView(NSBundle *bundle, UIView *view)
    {
        id idView = view;
        if ([idView respondsToSelector:@selector(text)] && [view respondsToSelector:@selector(setText:)])
            [idView setText:[bundle localizedStringForKey:[idView text] value:nil table:nil]];
        if ([idView respondsToSelector:@selector(title)] && [view respondsToSelector:@selector(setTitle:)])
            [idView setTitle:[bundle localizedStringForKey:[idView title] value:nil table:nil]];
        if ([idView respondsToSelector:@selector(placeholder)] && [view respondsToSelector:@selector(setPlaceholder:)])
            [idView setPlaceholder:[bundle localizedStringForKey:[idView placeholder] value:nil table:nil]];
        if ([idView respondsToSelector:@selector(prompt)] && [view respondsToSelector:@selector(setPrompt:)])
            [idView setPrompt:[bundle localizedStringForKey:[idView prompt] value:nil table:nil]];
        if ([idView respondsToSelector:@selector(titleForState:)] && [view respondsToSelector:@selector(setTitle:forState:)])
            [idView setTitle:[bundle localizedStringForKey:[idView titleForState:UIControlStateNormal] value:nil table:nil] forState:UIControlStateNormal];
        if ([idView isKindOfClass:[UITabBar class]] || [idView isKindOfClass:[UIToolbar class]])
            for (UIBarItem *item in [idView items])
                [item setTitle:[bundle localizedStringForKey:[item title] value:nil table:nil]];
        for (UIView *subview in [view subviews])
            translateView(bundle, subview);
    }
    

    Warning: You may of have to check other sets of selectors to catch everything. This is not a best-practice, but it seems like much less work

    0 讨论(0)
  • 2021-02-02 02:55

    I think you want to read about the command line tool: ibtool. It should simplify what you're looking to do.

    0 讨论(0)
  • 2021-02-02 02:55

    I always set all strings needed in the viewDidLoad() method with only having placeholders in the .xib. This way I can localize any .xib easily with only updating .string files

    0 讨论(0)
  • 2021-02-02 03:01

    Apple's built-in command line program ibtool lets you do this. You can dump the strings from a XIB, localize them, then create a new XIB based on the existing XIB but using the localized strings. So you can always have a base set of XIBs and recreate them all whenever the localized strings change. More info here: http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/.

    0 讨论(0)
  • 2021-02-02 03:02

    Perhaps you should update every string manually. Let's say you have an array, or an xml file, of strings in different languages, when the user changes the language just get the translated string from the array and then change it to the UI.

    Marco

    0 讨论(0)
  • 2021-02-02 03:05

    You can localize automatically your nibs without duplicating them following this simple guide:

    http://programminghacks.net/2011/06/03/ios-nib-localization-in-localizable-strings/

    0 讨论(0)
提交回复
热议问题