2017-06-22 48 views
0

我在代碼中定義的iTunes類是這樣的:由於編譯器優化而無法本地化字符串?

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

正如你可以看到我想要本地化類別名稱。

Localizable.strings包含

"arts" = "Kunst"; 

某處在我的代碼運行這個(其中包括):

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

我跑我的應用程序與應用程序語言和區域設置爲德語。這不會打印翻譯的值。它打印Arts而不是Kunst

爲什麼不打印正確本地化的字符串?我認爲這是由於編譯器優化?

+0

您是否嘗試過NSLocalizedString( 「藝術」,評論: 「」),而不是NSLocalizedString( 「藝術」,值: 「藝」,評論: 「」)? –

+0

是的,我已經嘗試過。在這種情況下,它會打印'arts'。 – funkenstrahlen

+0

這應該工作..可能是你錯過了 –

回答