dral3x / StringsLint

📱Ensure your localized strings are complete and never unused 👍
MIT License
68 stars 10 forks source link

Using enums / rawValues rather that hard coded LocalizedStringKeys ? #22

Closed JulesMoorhouse closed 2 years ago

JulesMoorhouse commented 2 years ago

Hi,

I love the idea of StringsLint, however it doesn't work out of the tin for me. I use enums and rawValue instead of hard coding my LocalizedStringKeys.

I'm guessing this will be too complicated for the StringsLint to pick up?

Or maybe there's a configuration setting I can use ?

Thanks in advance.

Jules.

enum Strings: LocalizedStringKey {
    case settingsAcknowledgements
    case settingsAddExampleData
    case settingsDeleteAllData
}

extension String {
    init(_ localizedString: Strings) {
        let output = NSLocalizedString(
            localizedString.rawValue.stringKey,
            comment: ""
        )

        self.init(format: output)
    }
}

extension LocalizedStringKey {
    var stringKey: String {
        let description = "\(self)"

        let components = description.components(separatedBy: "key: \"")
            .map { $0.components(separatedBy: "\",") }

        return components[1][0]
    }
}
dral3x commented 2 years ago

Hi @JulesMoorhouse, that's an interesting approach!

StringsLint relies on simple pattern matching on text, so (I guess) it's too naive to reliably work with your approach.

Maybe you can modify it so at least it recognizes all cases of the enum Strings as "used keys". It's not perfect but better than nothing I guess.

JulesMoorhouse commented 2 years ago

Thanks @dral3x .

I take it you mean…

case settingsAcknowledgements = “settingsAcknowledgements”

?

dral3x commented 2 years ago

Exactly @JulesMoorhouse

JulesMoorhouse commented 2 years ago

I suppose I really need to generate my enums based on my strings file to be safe.

I know this is unrelated but are you aware of a templating tool that o could run as a build phase?

Thanks.

dral3x commented 2 years ago

The easiest option is SwiftGen https://github.com/SwiftGen/SwiftGen If that's not enough, you can try Sourcery https://github.com/krzysztofzablocki/Sourcery for more customized code generation.

JulesMoorhouse commented 2 years ago

Thanks that’s awesome.