I have a variable that I am interpolating into a Text view:
struct Example : View {
var example : String?
var body : some View {
ZStack {
Text("Example text: \(example ?? "")")
}
}
}
This text view is localized:
"Example text: %@" = "Example text: %@";
However, to get this variable "example" that is passed into this view, I have to form it based on certain variables. This view is first found within another view that gives it its "example" variable; this parent view has a class as a state object:
struct ContentView : View {
@StateObject var exampleClass = ExampleClass()
var body: some View {
ZStack {
Example(example: exampleClass.exampleText)
}
}
}
Here is that state object class where the string is derived:
class ExampleClass : ObservableObject {
var exampleVariable : String = "data includes"
var exampleText : String = ""
var data : [String] = ["data1", "data2", "data3", "data4"]
init(){
setExample()
}
func setExample(){
for i in data {
exampleVariable = exampleVariable + "\(i)"+"\(data.last != i ? ", " : "")"
}
exampleText = exampleText + exampleVariable
}
}
Each of those data variables are also localized:
"data1" = "data1"; //etc.
I am unable to make a translation of the end result. How would I go about ensuring that the final string includes the localized sum of all the data objects ("data1", "data2", etc.) into the exampleText into the struct Example? Any help at all would be greatly appreciated!
I solved this by just changing all the String variables into Text variables, and adding Text(.init()) around the strings. I was unaware that Text can be treated as a data type rather than a view.