2016-04-15 29 views
0

我正在做動態本地化。我從服務器端獲取所有本地化字符串。在響應的基礎上,我創建了動態的localization.string文件。例如:swift中的動態本地化

"By Venue"="Par Lieu"; 
"Invitee Name"="Invité Nom"; 
"Email suffix:"="Email suffixe:"; 
"METRICS"="MESURES"; 
"MD - Moldova"="MD - Moldavie"; 
"Name:"="Nom:"; 
"Approval Manager"="Gestionnaire de Approbation"; 
"Last login on:"="Dernière connexion sur:"; 

但是在這個響應中也包含了一些HTML標記。例如:

"<b ljsid-1="">Note:</b> You can send this email manually once every 24 hours."="<b ljsid-1="">Remarque:</b> Vous pouvez envoyer cet e-mail manuellement une fois toutes les 24 heures."; 

如果我從動態創建的本地化文件中刪除此標記,那麼我能夠做loclaization。否則我不能loclaization。

所以請建議我,如何處理這個HTML標籤。在此先感謝

回答

0

要從HTML中獲取文本,您可以在項目中使用Kanna。在下面的代碼片段test.txt只是一個HTML字符串作爲源文件。

override func viewDidLoad() { 
     super.viewDidLoad() 

     do { 
      let path = NSBundle.mainBundle().pathForResource("test", ofType: "txt") 
      let str = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding) 
      // Use Kanna framework to get HTML text 
      if let doc = HTML(html: str, encoding: NSUTF8StringEncoding) { 
       let text = doc.text 
       print(text) 

       let file = "/output.txt" 

       if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) { 
        var path = dirs[0] //documents directory 
        path.appendContentsOf(file) 

        //writing 
        try text!.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding); 

        //reading 
        let text2 = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) 
        print(text2) 
       } 

      } 

     } 
     catch let error as NSError { 
      print(error.localizedDescription) 
     } 

    } 

的輸出是:

Optional("\"Note: You can send this email manually once every 24 hours.\"=\"Remarque: Vous pouvez envoyer cet e-mail manuellement une fois toutes les 24 heures.\"") 
"Note: You can send this email manually once every 24 hours."="Remarque: Vous pouvez envoyer cet e-mail manuellement une fois toutes les 24 heures."