IPhone本地化:是否可以轻松翻译nib文件而无需复制每种语言的笔尖? [英] IPhone localization: Is it possible to translate nib files easily without having to copy the nib for each language?

查看:284
本文介绍了IPhone本地化:是否可以轻松翻译nib文件而无需复制每种语言的笔尖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种可管理的方式来翻译iPhone应用程序中的每个可见字符串。官方苹果文档说使用.strings文件编写程序字符串,同时使用xcode中内置的添加本地化文件选项来本地化nib文件。

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 strings, while using the built in "add localized file" option in xcode to localize nib files.

我看到的问题是,如果UI在本地化发生后需要更改,那么我们需要单独更新每种语言的nib,这不是最佳的。是否有更简单的方法可能将nib文件中显示的字符串绑定到.strings文件中的相关值?或者我是否必须以编程方式为每个ui元素设置这些字符串(这稍微好些但仍然很烦人)?

The problem i see with this is that if the UI needs to change after localization happens, then we would need to update the nibs for each language individually which is less than optimal. Is there a simpler way to perhaps tie strings displayed in nib files to relevant values in a .strings file? or would i have to programmatically have to set those strings for each ui element (which is slightly better but still annoying)?

推荐答案

您可以做的最好是递归遍历每个视图,然后在其上设置文本:

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

这篇关于IPhone本地化:是否可以轻松翻译nib文件而无需复制每种语言的笔尖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆