由于编译器优化,无法本地化String?

时间:2017-06-22 12:13:15

标签: ios swift nslocalizedstring

我在代码中定义了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

为什么不打印正确本地化的字符串?我假设这是由编译器优化引起的?

1 个答案:

答案 0 :(得分:0)

这不是编译器问题。本地化被置于框架内。我没有为NSLocalizedString定义一个包。因此无法找到本地化的字符串。

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