简体   繁体   English

从主bundle获取本地化字符串

[英]Get localized strings from main bundle

How do i get inside the UITest target localizations from the main Bundle? 如何从主Bundle进入UITest目标本地化?

func localizedText(_ key: String) -> String {
    return Bundle.main.localizedString(forKey: key, value: nil, table: nil)
}

I tried accessing Bundle.main but isn't localizing the string. 我尝试访问Bundle.main但没有本地化字符串。 And it seems i can't import the main target of the app to do Bundle(for: ClassName.self).localized... 而且似乎我无法导入应用程序的主要目标来做Bundle(for:ClassName.self).localized ...

Any hints? 任何提示?

To use your localized strings in UITests you have to do two things: 要在UITests中使用本地化字符串,您必须做两件事:

1) Add your Localizable.strings file to your UITest target 1)将您的Localizable.strings文件添加到您的UITest目标

2) Access the string via the UITest bundle: 2)通过UITest包访问字符串:

func localizedText(_ key: String) -> String {
    let uiTestBundle = Bundle(for: AClassFromYourUITests.self)
    return NSLocalizedString(key, bundle: uiTestBundle, comment: "")
}

Just make sure that you use a class that is part of your UITest target to access the bundle. 只需确保使用属于UITest目标的类来访问该捆绑包。

Using Bundle.main does not work when running UITests because it gives you the bundle of the UITest Runner App instead of the UITest bundle 使用Bundle.main在运行UITests时不起作用,因为它为您提供了UITest Runner App而不是UITest bundle的捆绑包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM