Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Use protocol for generated code #82

Open
Jeehut opened this issue Mar 1, 2023 Discussed in #62 · 0 comments
Open

[Feat] Use protocol for generated code #82

Jeehut opened this issue Mar 1, 2023 Discussed in #62 · 0 comments
Assignees
Labels
Feature Request New functionality the app doesn't provide yet Size: Small Probably less than 1 week of work

Comments

@Jeehut
Copy link
Member

Jeehut commented Mar 1, 2023

Discussed in #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 Jeehut added Feature Request New functionality the app doesn't provide yet Size: Small Probably less than 1 week of work labels Mar 1, 2023
@Jeehut Jeehut self-assigned this Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New functionality the app doesn't provide yet Size: Small Probably less than 1 week of work
Projects
None yet
Development

No branches or pull requests

1 participant