Skip to content

Identify BCP 47 identifiers and turn them into Locale objects #1

@zadr

Description

@zadr

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
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions