故事板中UITextView的本地化 [英] Localization for UITextView in storyboard

查看:87
本文介绍了故事板中UITextView的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在故事板中有一个带有不同按钮,标签和一些文本视图的应用程序,我直接在故事板中输入文本。我启用了基本本地化并添加了几种语言。

So I have an application with different buttons, labels and some Text Views in storyboard where I entered the text directly in the storyboard. I enabled base localization and added a couple of languages.

这为Base(英语)和其他语言生成了故事板,其中包含项目对象ID列表。
我翻译了所有内容,标签和按钮(ALL OF THEM)工作并以我设置的语言显示。

This generated storyboards for Base (English) and the other languages with a list of items objectIDs. I translated everything, and the labels and buttons (ALL OF THEM) work and show in the language I set the device to.

无论我设置哪种语言,文本字段都会显示初始英文文本...
文本视图是否还有其他步骤?

The text fields however keep showing the initial English text no matter which language I set... Are there any extra steps involved for Text View?

推荐答案

所以,我做了一些研究,似乎为了使其正常工作,UITextView的文本需要是以编程方式设置。

So, I did some research, and it seems that in order for this to work correctly, the text for the UITextView needs to be set programmatically.

来源: Devforums.apple

引用:


据我所知,字符串如此因为必须使用NSLocalizedString在代码中设置文本视图/字段的text属性。 WWDC 2013视频会议的第一个半小时#219让你的应用程序世界准备就绪,如果你有时间观看它,

as I understand it, strings such as the text property of a text view/field have to be set in code using NSLocalizedString. The first 1/2 hour of WWDC 2013 video session #219 Making Your App World Ready covers this if you have the time to watch it

因此,似乎解决方法(如果您不想以编程方式设置文本)是在发布应用程序之前将字符串文件转换为故事板。这似乎按预期工作,并显示UITextView正确本地化。

So, it seems that the workaround (if you don't want to set the text programmatically) is to convert the strings file into a storyboard before shipping the app. This does seem to work as intended and shows the UITextView properly localized.

编辑:找到另一种允许保留.strings文件的解决方法。

Found another workaround that allows to keep .strings file.

- (void)viewDidLoad

for(UIView* v in self.view.subviews)
{
    if([v isKindOfClass:[UITextView class]])
    {
        UITextView* txv = (UITextView*)v;
        NSString *loctxt = [txv.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        txv.text = NSLocalizedString(loctxt, @"");
    }
}

这会产生一个Percent Escapes编码的字符串故事板,如下所示:

This produces a Percent Escapes encoded string from whatever is inside the storyboard, like this:

Hello%20World

Localizable.strings 文件中,您使用上面的键作为键,这将在应用程序中生成本地化文本在运行时为所选语言环境,如下所示:

In your Localizable.strings file, you use the above as the key, and this will produce the localized text in the app at runtime for the selected locale, like this:

"Hello%20World" = "Hallo Welt";

Percent转义会处理基本字符串中的所有转义字符。

The Percent escaping takes care of all escape characters in the base string.

这篇关于故事板中UITextView的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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