Localize-Swift icon indicating copy to clipboard operation
Localize-Swift copied to clipboard

Does not work with Obj-C Code.

Open varun-naharia opened this issue 7 years ago • 3 comments

Code should work with mix project Obj-C and Swift, Please check

varun-naharia avatar Nov 27 '17 09:11 varun-naharia

Out of the box it seems not, but you can add an extension so that it can. refer to #52. Although this suggests editing the library directly, i would advise against that and just write an extension like so

extension Localize {
    @objc open class func thisString(_ wantedString: String) -> String {
        return wantedString.localized()
    }
}

Genhain avatar Nov 30 '17 07:11 Genhain

@varun-naharia

After some consideration of usage and syntax i came up with the following very simple extension for using localized in Objc.

extension NSString {

    @objc var localized: NSString {
        let string = self as String
        return string.localized() as NSString
    }
}

this allows for using it like so @"Hello world".localized;

Now this is a little atypical for Objc code but I was not so keen on some of the alternatives which were

[Localize string:@"Hello World"]; [@"Hello World" localized];

Also would this work with the genstrings script @marmelroy?

Genhain avatar Nov 30 '17 09:11 Genhain

I created a pr for the obj-c support at genstrings here Still waiting to be merged.

alicanbatur avatar Dec 15 '17 09:12 alicanbatur