0

I have such enums:

enum Gender {
    case male
    case female

    func toString() -> String {
        switch self {
        case .male:
            return R.string.localizable.male()
        case .female:
            return R.string.localizable.female()
        }
    }
}

enum Relationship {
    case mother
    case father
    case son
    case daughter
    case brother
    case sister
    case grandmother
    case grandfather
    case grandson
    case granddaugther
    case aunt
    case uncle
    case cousin
    case stepsister
    case stepbrother
    case grandgrandmother
    case grandgrandfather
    case other
}

Is there easier way to get enum string values localized? For small enum like first this is not a problem but for longer as second one this ads many more lines to code.

Maybe there is some easier way to create localizable string description in such case?

I've tried also:

enum Gender: String {
    case male = R.string.localizable.male()
    case female
}

but this makes compiler error (it needs plain literal here)

1 Answer 1

0

Making the enum conform to RawRepresentable is a suitable way, just localize the raw value

enum Gender : String {
    case male
    case female

    var localizedString : String {
       return NSLocalizedString(self.rawValue, comment: "")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.