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

前端 未结 6 1502
清歌不尽
清歌不尽 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

提交回复
热议问题