Skip to content

Instantly share code, notes, and snippets.

@rbresjer
Last active January 25, 2017 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbresjer/b4a9425f7b6bf14dc2282230ecd3be56 to your computer and use it in GitHub Desktop.
Save rbresjer/b4a9425f7b6bf14dc2282230ecd3be56 to your computer and use it in GitHub Desktop.
Dynamic content with NSLocalizedString
import Foundation
struct FaqCategory: Equatable {
let id: Int
let name: String
let items: [FaqItem]
}
func ==(lhs: FaqCategory, rhs: FaqCategory) -> Bool {
return lhs.id == rhs.id
}
struct FaqItem: Equatable {
let id: Int
let question: String
let answer: String
}
func ==(lhs: FaqItem, rhs: FaqItem) -> Bool {
return lhs.id == rhs.id
}
struct FaqCategoriesViewModel {
var categories: [FaqCategory] = []
init() {
var catId = 1
var catKey = String(format: "faq.c.%d.title", catId)
while NSLocalizedString(catKey, comment: "") != catKey {
var items: [FaqItem] = []
var itemId = 1
var questionKey = String(format: "faq.c.%d.q.%d.q", catId, itemId)
var answerKey = String(format: "faq.c.%d.q.%d.a", catId, itemId)
while NSLocalizedString(questionKey, comment: "") != questionKey
&& NSLocalizedString(answerKey, comment: "") != answerKey {
items.append(
FaqItem(
id: itemId,
question: NSLocalizedString(questionKey, comment: ""),
answer: NSLocalizedString(answerKey, comment: "")
)
)
itemId += 1
questionKey = String(format: "faq.c.%d.q.%d.q", catId, itemId)
answerKey = String(format: "faq.c.%d.q.%d.a", catId, itemId)
}
categories.append(
FaqCategory(
id: catId,
name: NSLocalizedString(catKey, comment: ""),
items: items
)
)
catId += 1
catKey = String(format: "faq.c.%d.title", catId)
}
}
}
struct FaqCategoryViewModel {
var category: FaqCategory
init(category: FaqCategory) {
self.category = category
}
}
"faq.c.1.title" = "Category 1";
"faq.c.1.q.1.q" = "Question 1";
"faq.c.1.q.1.a" = "Answer 1";
"faq.c.2.title" = "Category 2";
"faq.c.2.q.1.q" = "Question 1";
"faq.c.2.q.1.a" = "Answer 1";
"faq.c.2.q.2.q" = "Question 2";
"faq.c.2.q.2.a" = "Answer 2";
@rbresjer
Copy link
Author

func faqCategories() -> [FaqCategory] {
    var categories: [FaqCategory] = []
    
    var catIx = 1
    while let categoryTitle = category(catIx) {
        var questions: [FaqQuestion] = []
        
        var questionIx = 1
        while let question = question(questionIx, catIx: catIx), let answer = answer(questionIx, catIx: catIx) {
            let faqQuestion = FaqQuestion(question: question, answer: answer)
            questions.append(faqQuestion)
            questionIx += 1
        }
        
        let faqCategory = FaqCategory(title: categoryTitle, questions: questions)
        categories.append(faqCategory)
        
        catIx += 1
    }
    
    return categories
}

private func category(_ ix: Int) -> String? {
    return localizedStringOrNil(key: "faq.\(ix).title")
}

private func question(_ ix: Int, catIx: Int) -> String? {
    return localizedStringOrNil(key: "faq.\(catIx).\(ix).question")
}

private func answer(_ ix: Int, catIx: Int) -> String? {
    return localizedStringOrNil(key: "faq.\(catIx).\(ix).answer")
}

private func localizedStringOrNil(key: String) -> String? {
    let string = NSLocalizedString(key, comment: "")
    return string == key ? nil : string
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment