是否有将属性字符串与本地化结合使用的方法? [英] Are there approaches for using attributed strings in combination with localization?

查看:68
本文介绍了是否有将属性字符串与本地化结合使用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设此字符串为英文,并且要求对文本字符串的特定部分使用粗体或颜色等:

Suppose there is this string in English and its a requirement to bold or color etc. to a particular part of a string of text:

"word1 word2 WORD3 word4"

"word1 word2 WORD3 word4"

这可以通过将属性设置为适当的范围来实现.

That can be accomplished by setting the attribute for the appropriate range.

可以将范围硬编码为(13,5),但是当然不能将其本地化.

The range could be hardcoded to (13,5) but then of course that wouldn't be localizable.

因此,也可以通过知道必须使用粗体的第三个单词来动态确定范围.

So alternatively the range could be dynamically determined by knowing that its the 3rd word that has to have the bold applied to it.

但这也不是可本地化的.假设将句子翻译成语言N后,它仅包含2个单词,或者使用语言M包含5个单词?

But that too isn't localizable. Suppose once that sentence is translated to language N it only contains 2 words, or contains 5 words in language M?

所以我的问题是,如何在没有大量硬编码的情况下将属性字符串与本地化结合使用?

So my question is how can you use attributes strings in combination with localization without lots of hardcoding?

推荐答案

您可以使用一些符号来表示应将哪些单词加粗,然后使用一个转换器来查找要加粗的范围并添加适当的属性.

You could use some notation to signify which words should be bolded, and then have a transformer that would find ranges to bold and add appropriate attributes.

word •word• word word

מילה מילה •מילה• מילה מילה

在这两个句子中,我使用字符表示中间的单词应为粗体.然后,我可以使用rangeOfString:options:range:进行迭代并找到所有以粗体显示的范围,并相应地插入属性.

In these two sentences, I use the character to signify that the word in between should be bold. I can then use rangeOfString:options:range: to iterate and find all ranges to bold and insert an attribute accordingly.

这篇关于是否有将属性字符串与本地化结合使用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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