Last active
January 25, 2017 14:18
-
-
Save rbresjer/b4a9425f7b6bf14dc2282230ecd3be56 to your computer and use it in GitHub Desktop.
Dynamic content with NSLocalizedString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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"; |
Author
rbresjer
commented
Jan 25, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment