0

I defined iTunes categories in my code like this:

public enum ItunesCategoryType: Int {
    case arts = 1
    case business = 8
    case comedy = 14
    case education = 15
    case gamesHobbies = 21
    case governmentOrganizations = 27
    case health = 32
    case kidsFamily = 37
    case music = 38
    case newsPolitics = 39
    case religionSpirituality = 40
    case scienceMedicine = 48
    case societyCulture = 52
    case sportsRecreation = 57
    case technology = 62
    case tvFilm = 67
}

public struct ItunesCategory {
    public var id: Int {
        return type.rawValue
    }
    public let type: ItunesCategoryType
    public var name: String {
        switch type {
        case .arts: return NSLocalizedString("arts", value: "Arts", comment: "")
        case .business: return NSLocalizedString("business", value: "Business", comment: "")
        case .comedy: return NSLocalizedString("comedy", value: "Comedy", comment: "")
        case .education: return NSLocalizedString("education", value: "Education", comment: "")
        case .gamesHobbies: return NSLocalizedString("games-hobbies", value: "Games & Hobbies", comment: "")
        case .governmentOrganizations: return NSLocalizedString("government-organizations", value: "Government & Organizations", comment: "")
        case .health: return NSLocalizedString("health", value: "Health", comment: "")
        case .kidsFamily: return NSLocalizedString("kids-family", value: "Kids & Family", comment: "")
        case .music: return NSLocalizedString("music", value: "Music", comment: "")
        case .newsPolitics: return NSLocalizedString("news-politics", value: "News & Politics", comment: "")
        case .religionSpirituality: return NSLocalizedString("religion-spirituality", value: "Religion & Spirituality", comment: "")
        case .scienceMedicine: return NSLocalizedString("science-medicine", value: "Science & Medicine", comment: "")
        case .societyCulture: return NSLocalizedString("society-culture", value: "Society & Culture", comment: "")
        case .sportsRecreation: return NSLocalizedString("sports-recreation", value: "Sports & Recreation", comment: "")
        case .technology: return NSLocalizedString("techology", value: "Technology", comment: "")
        case .tvFilm: return NSLocalizedString("tv-film", value: "TV & Film", comment: "")
        }
    }

    public init(type: ItunesCategoryType) {
        self.type = type
    }
}

As you can see I want the category names to be localized.

Localizable.strings contains (among others)

"arts" = "Kunst";

Somewhere in my code I run this:

print(ItunesCategory(type: .arts).name)

I run my app with Application Language and Region set to German. This does not print the translated value. It prints Arts instead of Kunst.

Why does it not print the correctly localized String? I assume this is due to compiler optimizations?

funkenstrahlen
  • 3,032
  • 2
  • 28
  • 40
  • Have you tried NSLocalizedString("arts", comment: "") instead of NSLocalizedString("arts", value: "Arts", comment: "") ? – Aravind A R Jun 22 '17 at 12:16
  • Yes I have tried that. In this case it prints `arts`. – funkenstrahlen Jun 22 '17 at 12:17
  • This should work..may be you missed outside of the question – LC 웃 Jun 22 '17 at 12:54
  • 1
    Compiler optimizations have nothing to do with it. Are your `Localizable.strings` files placed correctly? Do you have other languages localized? If so, do they work? Are you running ond device or simulator? Have you tried cleaning and rebuilding your project, doing a fresh install? – Losiowaty Jun 22 '17 at 14:31
  • 1
    @ThatlazyiOSGuy웃 You are correct. I was missing on something else I did not know. The localisation was placed inside a Framework. I did not define a bundle for NSLocalizedString. Therefore the localised Strings could not be found. This helped me: https://stackoverflow.com/questions/28724714/how-to-use-localizable-strings-stored-in-a-cocoatouch-framework – funkenstrahlen Jun 22 '17 at 14:42

1 Answers1

0

This is no compiler issue. The localisation was placed inside a Framework. I did not define a bundle for NSLocalizedString. Therefore the localised Strings could not be found.

How to use Localizable.strings stored in a CocoaTouch Framework?

funkenstrahlen
  • 3,032
  • 2
  • 28
  • 40