Localization of chart legend texts does not work in my app. Obviously I am doing something wrong. Here is the code:
Chart {
ForEach(morningTemp) { item in
LineMark(
x: .value("Date", item.date),
y: .value("Morning Temp", item.value),
series: .value("Morning Temp", "B")
)
.foregroundStyle(Color.red)
}
ForEach(eveningTemp) { item in
LineMark(
x: .value("Date", item.date),
y: .value("Evening Temp", item.value),
series: .value("Evening Temp", "C")
)
.foregroundStyle(Color.blue)
}
}
.chartForegroundStyleScale(["Morning Temp": Color.red, "Evening Temp": Color.blue])
This works, but the texts in .chartForegroundStyleScale, "Morning Temp" and "Evening Temp", do not change when I change language. The legend texts are still in English. What am I doing wrong?
I worked around the problem by letting the app do the localization, i.e. detecting the language used, preparing the texts accordingly, and then inserting them in .chartForegroundStyleScale. This may work for two or three languages, but for more languages than that it is not possible. There must be a better way. Please help.
You can use an alternative String
initializer
.chartForegroundStyleScale([
String(localized: "Morning Temp"): Color.red,
String(localized: "Evening Temp"): Color.blue
])