j2s
j2s copied to clipboard
Identify BCP 47 identifiers and turn them into Locale objects
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
}
}
}
fixed by https://github.com/zadr/j2s/commit/f0d5fa5c8be2672bcec1a9e734604ce8e2a8930d
this was un-fixed after starting to generate Codable-compliant code