RemafoX icon indicating copy to clipboard operation
RemafoX copied to clipboard

[Feat] Use protocol for generated code

Open Jeehut opened this issue 1 year ago • 0 comments

Discussed in https://github.com/FlineDev/RemafoX/discussions/62

Originally posted by ConfusedVorlon November 15, 2022 I have a suggestion. Instead of generating code for string and locStringKey in every single enum, why not make the enum conform to a protocol - and provide the code in a default conformance.

This would simplify the code somewhat - but it would also set you up to let users add their own protocols as an app setting (more below)

Here is a simple generated interface rewritten with a FoxKey protocol

protocol FoxKey {
    static var table: String {get}
    static var tableLookupKey: String {get}
    static var string:String {get}
    static var locStringKey: LocalizedStringKey {get}
}

extension FoxKey {
    static var string: String { Bundle.main.localizedString(forKey: self.tableLookupKey,
                                                            value: nil,
                                                            table: self.table) }
    
    static var locStringKey: LocalizedStringKey { LocalizedStringKey(self.tableLookupKey) }
}

internal typealias Loc = Res.Str

/// Top-level namespace for safe resource access. Managed by ReMafoX (https://remafox.app).
internal enum Res {
    /// Root namespace for safe access to localized strings. Managed by ReMafoX (https://remafox.app).
    internal enum Str {
        internal enum Alarm {
            internal enum Canopy {
                /// 🇺🇸 English: "Canopy Alarms"
                internal enum Title:FoxKey {
                    /// The lookup table
                    internal static var table: String { "Localizable" }
                    
                    /// The lookup key in the translation table (= the key in the `.strings` or `.stringsdict` file).
                    internal static var tableLookupKey: String { "Alarm.Canopy.Title" }
                }
            }
            
            internal enum Climb {

                /// 🇺🇸 English: "Climb Alarms"
                internal enum Title:FoxKey {
                    /// The lookup table
                    internal static var table: String { "Localizable" }
                    
                    /// The lookup key in the translation table (= the key in the `.strings` or `.stringsdict` file).
                    internal static var tableLookupKey: String { "Alarm.Climb.Title" }
                }
            }
        }
    }
}

However - I'd like to actually rewrite that code in my own project. I can simply check an option somewhere to ask that you don't add the protocol and conformance definition at the top of the generated file

Then I provide my own implementation of FoxKey

protocol FoxKey {
    static var table: String {get}
    static var tableLookupKey: String {get}
    static var string:String {get}
    static var locStringKey: LocalizedStringKey {get}
}

extension FoxKey {
    static var string: String { 
     //my custom implementation gets the localisation based on a user preference in the app rather than the system language
    }
    
  //unchanged
    static var locStringKey: LocalizedStringKey { LocalizedStringKey(self.tableLookupKey) }
}

Jeehut avatar Mar 01 '23 09:03 Jeehut