While developing my new app, Wunderbar, I wanted to localize the app for German and Turkish. I already knew that I could just create a Strings file for each language and use keys for the texts in the app. But then I remembered that Apple introduced a new way of localizing apps called String Catalogs. I decided to give it a try.
It is very easy to use String Catalogs, you just need to create a new Strings Catalog file in Xcode, add the languages you want to support on Project > Info > Localizations, and then build your app. Xcode will automatically generate the keys for each language you added. The rest is just adding the translations for each key.
While testing my app, I realized that the texts that I used in Menu view were not listed in the Strings Catalog file because they were dynamicly generated from Enum values. First, I’ve tried to add the Enum values to the Strings Catalog file manually, but it didn’t work. Then after some research, I found out that I can use LocalizedStringResource
function to handle dynamic text, and it worked like a charm.
Menu {
ForEach(Language.allCases) { lang in
Button("\(LocalizedStringResource(stringLiteral: lang.rawValue.capitalized))") {
doSomething()
}
}
}
I hope this helps you too.