2016-06-01 91 views
1

我有本土化的问题,本地化字符串中的iOS

这里是我的代码:

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() 

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

In 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>  

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

本地化.strings file,

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

错误的数据格式是错误的。

我认为这是由于“\ n”引起的。

回答

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”个字符串像你期望的工作,所以也许你应该重新考虑......我敢肯定,编译器会让你知道:)

希望这可以帮助你。