j2s icon indicating copy to clipboard operation
j2s copied to clipboard

Identify BCP 47 identifiers and turn them into Locale objects

Open zadr opened this issue 8 years ago • 2 comments

BCP 47 defines Tags for Identifying Languages.

Given the following JSON:

{
    "language": "en",
    "foo": true
}

j2s will currently output the following Swift code:

public struct Root {
	let foo: Bool
	let language: String

	init?(_ dictionary: [String: Any]) {
		if let foo = dictionary["foo"] as? Bool {
			self.foo = foo
		} else {
			return nil
		}
		
		if let language = dictionary["language"] as? String {
			self.language = language
		} else {
			return nil
		}
	}
}

but it might be useful if could identify BCP 47 identifiers and instead output code using Locales, such as:

public struct Root {
	let foo: Bool
	let language: String

	init?(_ dictionary: [String: Any]) {
		if let foo = dictionary["foo"] as? Bool {
			self.foo = foo
		} else {
			return nil
		}
		
		if let language = dictionary["language"] as? String {
			Locale(localeIdentifier: language)
		} else {
			return nil
		}
	}
}

zadr avatar Jan 02 '17 00:01 zadr

fixed by https://github.com/zadr/j2s/commit/f0d5fa5c8be2672bcec1a9e734604ce8e2a8930d

zadr avatar Jan 04 '17 06:01 zadr

this was un-fixed after starting to generate Codable-compliant code

zadr avatar Jun 27 '17 18:06 zadr