2016-06-01 90 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」個字符串像你期望的工作,所以也許你應該重新考慮......我敢肯定,編譯器會讓你知道:)

希望這可以幫助你。