I have an array with over 100 strings with the same setup as below. Is it any way to localise all the strings in the array or is it a better way of setting this up?
var listOfThings = ["Cars", "Mopeds"]
I have an array with over 100 strings with the same setup as below. Is it any way to localise all the strings in the array or is it a better way of setting this up?
var listOfThings = ["Cars", "Mopeds"]
I would suggest putting the strings in a plist file. Then the plist file can be localized as needed. Much better than putting all of the string in your code.
When you use the Bundle
class to get the path to the plist file, it will automatically give you the path to the appropriate localized version of the file. Then load the array from the plist file (using NSArray
and then bridged to a Swift array).
Assuming you have created a localized plist file named Things.plist
, you can load it as follows:
if let plistURL = Bundle.main.url(forResource: "Things", withExtension: "plist") {
if let plistArray = NSArray(contentsOf: plistURL) as? [String] {
listOfThings = plistArray
}
}