Autocompletion for LocalizableString files

Hello Enginners,


I'm still looking for a way that would make Localization much easier for developers in Xcode. The current implementation is more complex, and it creates many ways to duplicate keys and to have runtime issues. Avoiding magic keys is one of the best practices that we can have.


Proposal:


Localizable. string en
"HelloWorld" = "Hello World"
"NumberOfRooms" = "Number of Rooms are %d"


Once the user writes into localizable string file. Xcode automatically will generate a struct file to be used in the project. So, a developer can use it in this way


Localizable.HelloWorld // print Hello World in en
Localizable.NumberOfRooms(value: 5) // print Number of Rooms are 5 in en


This practice will avoid magic keys in the source code. Also, it will push the developer to remove duplicate keys, and we can make sure one hundered percent developer hits the right key in the localizable file.


Best Regards,

Faisal

Idea is interesting.

A detail point, what would you do if your key is

"Hello World" and not "HelloWorld" ?


In fact, what you is just a parser to generate the enum (more than a struct) with all the keys, as you do with extension Notification.Name


public enum Localized {
   
    case HelloWorld
    case NumberOfRooms (value: Int)     // With associated value
    case NameOfHotel (value: String)     // With associated value  "NameOfHotel" = "Name of hotel is %@"
   
    var text : String {
        switch (self) {
        case .HelloWorld: return NSLocalizedString("Hello World", comment: "HelloWorld")
        case .NumberOfRooms(let value): return String(format: NSLocalizedString("Number of Rooms are %d", comment: "HelloWorld"), value)
        case .NameOfHotel(let value): return String(format: NSLocalizedString("name of Hotel is %@", comment: "HelloWorld"), value)
        }
    }
   
}

And use as:

        let sNum = Localizable.NumberOfRooms(value: 5).text
        print(sNum)
        let sName = Localizable.NameOfHotel(value: "Best Hotel").text
        print(sName)

We can force the developer not to have any space between words, and relay on camel case code pattern to parse keys. This way would resolve the issue.

Not sure.


When you have multiple languages and need to export to xliff to give it to translate, it is much better to have a format as:


"Hello World" = "Hello World";

and

"Hello World" = "Bonjour";


To know exactly what is to be translated.


Anyway, could file an enhancement request.

Autocompletion for LocalizableString files
 
 
Q