在iOS中本地化字符串

时间:2016-06-01 05:01:44

标签: swift localization nsstring

我有本地化问题,

这是我的代码:

 func attributedText()->NSAttributedString{ <br>
 self.lbltext.text = 
 NSLocalizedString("\n" + “Best Friends“ + "\n" + "\n" + “James” + “Kelvin” +“Favorite Food” + "\n" + "\n" +"(1) Burger” + "\n" +"(2) Fried Food” + "\n" +"(3) Beer”, comment: "")
 let string = "\n" + “Best Friends“ + "\n" + "\n" + “James” + “Kelvin” + “Favorite Food” + "\n" + "\n" +
                "(1) Burger” + "\n" +
                "(2) Fried Food” + "\n" +
                "(3) Beer” as NSString<br><br>
 let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(14.0)])

 let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(17.0)]

//字符串的一部分为粗体

 attributedString.addAttributes(boldFontAttribute, range: string.rangeOfString(“Best Friends“))
 attributedString.addAttributes(boldFontAttribute, range: string.rangeOfString(“Favorite Food”))
 return attributedString
 }<br><br>
 self.lbltext = attributedText()

====================

Main.strings 文件中,我的代码为

/* Class = "UILabel"; text = "\n" + “Best Friends“ + "\n" + "\n" + “James” + “Kelvin” +“Favorite Food” + "\n" + "\n" + "(1) Burger” + "\n" + "(2) Fried Food” + "\n" + "(3) Beer”; ObjectID = "kDi-LM-j5f"; */ <br><br>
"kDi-LM-j5f.text" = "\n" + “Best Friends“ + "\n" + "\n" + “James” + “Kelvin” + “Favorite Food” + "\n" + "\n" + "(1) Burger” + "\n" + "(2) Fried Food” + "\n" +"(3) Beer”;  <br>     

=======================

Localizable.strings 文件中,

"\n" + “Best Friends“ + "\n" + "\n" + “James” + “Kelvin” + “Favorite Food” + "\n" + "\n" +"(1) Burger” + "\n" +"(2) Fried Food” + "\n" +"(3) Beer” = "my translate text....";

错误是不正确的数据格式。

我认为这是因为&#34; \ n&#34;

1 个答案:

答案 0 :(得分:0)

我应该尝试简化您要求NSLocalizedString提供给您的内容。

如果你查看当前的代码:

NSLocalizedString("\n" + “Best Friends“ + "\n" + "\n" + “James” + “Kelvin” +“Favorite Food” + "\n" + "\n" +"(1) Burger” + "\n" +"(2) Fried Food” + "\n" +"(3) Beer”, comment: "")

然后我猜你真正喜欢本地化的是“最好的朋友”,“最喜欢的食物”,“汉堡”(或许不是汉堡,因为它的名字几乎全是一样的,来想一想它:)),“油炸食品”和“啤酒”。

所以,我会将字符串分成几部分,让我的Localizable.strings包含各个部分的键,然后让NSLocalizedString提供这些单独的部分,然后担心自己把它放在一起。

类似的东西:

let bestFriends = NSLocalizedString("Best Friends", comment: "")
let favoriteFood = NSLocalizedString("Favorite Food", comment: "") 
let burger = NSLocalizedString("Burger", comment: "")
let friedFood = NSLocalizedString("Fried Food", comment: "")
let beer = NSLocalizedString("Beer", comment: "")

let text = "\(bestFriends) \n \n James Kelvin \(favoriteFood) \n\n (1)\(burger) \n(2) \(friedFood) \n(3)  \(beer)"

然后......我不确定“\ n”是否像你期望的那样在Strings中工作,所以也许你应该重新思考......我确信编译器会让你知道:)

希望这会对你有所帮助。