iosswiftswiftuiuikitinternationalization

What's the best way to handle translatable strings in Swift?


I'm currently exploring the different options Swift provides for handling string localization. I would like to choose the right option. Meanwhile I'm more confused, instead of becoming more sure. There are LocalizedStringKey, LocalizedStringResource and String with arguments.

If I know in advance that a string will be displayed in the UI and might need to be translated, which of these options is the most appropriate and future-proof for localization?

Which approach is generally considered best practice for strings intended to be shown and translated?


Solution

  • When dealing with localized strings in Swift, especially for UI elements, choosing the right approach is crucial. Here’s a breakdown of the options:

    1️⃣ LocalizedStringKey (Best for SwiftUI)


    2️⃣ LocalizedStringResource (Best for Performance)


    3️⃣ String with NSLocalizedString (Best for Non-SwiftUI Code)