Update: Somebody actually solved my issue. Internet is great. Scroll down to see how this is fixed.
When I wrote about the couple of problems I don’t know how to solve in Stoins, I didn’t anticipate to quickly run into the next one. Stoins is supposed to be available in English and German. With English as the default, I set out to add German translations and watched a couple of tutorials on how to do so.
It’s relatively straight-forward.
- Create a Localizable.strings file for every language you want
- Xcode finds all translatable strings automatically and you translate them one by one
- Done
Unfortunately that’s not exactly how it went. Most strings were found, sure, but those outside of Text()
views weren’t and I don’t understand why or how to force Xcode to translate them.
An example
Take everything with a grain of salt. I don’t know what I’m doing. I’m that much of a beginner that we can just accept the fact that literally anything I do can be done in a better way. For now my main goal is for stuff to just work. I don’t want to win any prizes for great code. Yet.
This is an object in Stoins. It’s the blueprint for an achievement:
struct Achievement: Identifiable {
let id = UUID()
let name: String
let description: String
let image: String
let badge: String
var conditionAchieved: (() -> Bool)
}
I have an array of objects like this that I use to create the interface. The array looks a little like this:
@Published var achievementsStreaks = [
Achievement(name: "Fortnight", description: "Have a 14 days streak", image: "badge_fortnight", badge: "badge_fortnight.gltf") {
var result = false
// Code that defines if 'result' is true or false
}
return result
},
// Second Achievement
// Third achievement
// etc
]
(I’m a bit proud of my use of closures here, ngl. They were very brain-breaking for me and now I’m able to use them without hurting myself in confusion.)
So far, so good. All of this works surprisingly well in terms of how the app is supposed to behave. The only remaining problem is that I can’t seem to figure out how to localize the name and description. Xcode doesn’t manage to automatically find the name
and description
attributes and when I add them manually to Localizable.strings
nothing happens.
I thought I found the answer in this WWDC video about localization in SwiftUI but using LocalizedStringKey
only works for views not structs, it seems.
For now I’ll leave it as it is. Only the about screen and achievements are affected which means it’s not an app-breaking issue for those who don’t speak English. Not great. Not terrible. A solid 3.6.
Still, I’d love to publish an update to this post where I explain how somebody helped me solve this and how it was a very easy fix. If that’s you, hit me up. There’s a very cozy comments section down below.
Update: As expected the answer to this couldn’t be more easy, I just wasn’t able to find it. Thanks Philipp for pointing me to this article. And thanks Bei Li! Every user facing string needs to be wrapped in String(localized:"")
. That’s it. Everything works now. lol.
đź‘€