UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: What is the best way to refactor strings into one file?

Forums > Swift

SwiftUI.

I do have a lot of repetitions of the same strings, and I've collected all of them into a separate AppStrings public class with Published variables, then place them within NSLocalizedString. Then I call them with a StateObject property throughout the application. The thing is, is this too complicated? Is there a way to make this easier and more importantly nicer?

3      

Do these strings need to change? If not, they don't need to be Published properties and you can avoid that overhead. They can just be regular let properties. And you probably don't need a StateObject either; it would probably be better to use a struct and you could pass it as an EnvironmentValue.

Beyond that, I'm not sure I understand what you are going for.

3      

If these are only strings you use in your app you could also use a localizable.strings file. You don't need necessarily translations for it.

Personally, I have an extension on String for translation purposes which works well with localized strings files.

var localized: String {
        return NSLocalizedString(self, comment: "")
    }

3      

@roosterboy, thanks for the tip, got rid of the Published stuff.

And you probably don't need a StateObject either; it would probably be better to use a struct and you could pass it as an EnvironmentValue.

How to do this? If I understood it right:

struct Strings: EnvironmentKey {
    static var defaultValue: String = ""
}

extension EnvironmentValues {
    var strings: String {
        get { self[Strings.self] }
    }
}

Where to place the strings within this setup? And call them through an @Environment variable?

3      

Here's an example.

struct StringStore: EnvironmentKey {
    let firstName = "Charlotte"
    let lastName = "Grote"
    //...plus whatever other strings you have

    //can even use computed properties
    var bestFriend: String {
        "Shauna Wickle"
    }

    var role: String {
        NSLocalizedString("ROLE", comment: "")
    }

    static let defaultValue = StringStore()
}

extension EnvironmentValues {
    var strings: StringStore {
        get { self[StringStore.self] }
        set { self[StringStore.self] = newValue }
    }
}

//this just gives us a nicer way to add to the
//  environment; it's not necessary
extension View {
    func addStringStore() -> some View {
        return self.environment(\.strings, StringStore())
    }
}

struct EnvParentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: EnvExampleView(),
                label: {
                    Text("Example")
                })
        }
        //using .environment(_:_:)
        .environment(\.strings, StringStore())
        //or using our View extension func
        //.addStringStore()
    }
}

struct EnvExampleView: View {
    @Environment(\.strings) var stringStore

    var body: some View {
        VStack {
            Group {
                Text(stringStore.firstName)
                Text(stringStore.lastName)
            }
            .font(.largeTitle)
            Text(stringStore.role)
                .font(.title)
            Spacer()
            Text(stringStore.bestFriend)
        }
    }
}

3      

Thank you very much! This is really neat.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.