ios 带有 swift 变量的 NSLocalizedString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26277626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSLocalizedString with swift variable
提问by Andorath
I'm trying to localize my app using NSLocalizedString. When i import the XLIFF file, most works like a charm but something do not and some string is not localized. I have noticed that the problem is from NSLocalizedString containing something variable inside like:
我正在尝试使用 NSLocalizedString 本地化我的应用程序。当我导入 XLIFF 文件时,大多数工作就像一个魅力,但有些没有,有些字符串没有本地化。我注意到问题出在 NSLocalizedString 中,其中包含一些变量,例如:
NSLocalizedString(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare")
or
或者
NSLocalizedString("Notifica per \(medicina!) della prescrizione \(prescription!)\nMemo: \(memoTextView.text)", comment: "Messaggio della Local Notification")
Maybe this is not the correct syntax for thi kind of stuff. Some one can explain me how to do that in swift? Thank you very much.
也许这不是这类东西的正确语法。有人可以解释我如何快速做到这一点?非常感谢。
回答by zisoft
You can use the sprintf
format parameters within NSLocalizedString
, so your example can look like this:
您可以在 中使用sprintf
格式参数NSLocalizedString
,因此您的示例如下所示:
let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)
回答by Mark
In Session #412 of the WWDC2014 "Localizing with Xcode 6" the proper way to this in Swift is the following:
在 WWDC2014“使用 Xcode 6 本地化”的 Session #412 中,在 Swift 中正确的方法如下:
String.localizedStringWithFormat(
NSLocalizedString(" - %d Notifica",
comment: "sottotitolo prescrizione per le notifiche al singolare"),
count)
回答by JaspreetKour
I have followed the approach of creating extension to String as i have many strings to localize.
我遵循了创建 String 扩展的方法,因为我有很多字符串要本地化。
extension String {
var localized: String {
return NSLocalizedString(self, comment:"")
}
}
To use it for localization in code do:
要在代码中使用它进行本地化,请执行以下操作:
self.descriptionView.text = "Description".localized
For strings with dynamic variables follow :
对于具有动态变量的字符串,请遵循:
self.entryTimeLabel.text = "\("Doors-open-at".localized) \(event.eventStartTime)"
Declare the strings in String files for different languages (example : Arabic and English)
声明不同语言的字符串文件中的字符串(例如:阿拉伯语和英语)
Hope will be helping!
希望会有所帮助!
回答by Pratik
I tried the above solutions however the code below worked for me
我尝试了上述解决方案,但下面的代码对我有用
SWIFT 4
快速 4
extension String {
/// Fetches a localized String
///
/// - Returns: return value(String) for key
public func localized() -> String {
let path = Bundle.main.path(forResource: "en", ofType: "lproj")
let bundle = Bundle(path: path!)
return (bundle?.localizedString(forKey: self, value: nil, table: nil))!
}
/// Fetches a localised String Arguments
///
/// - Parameter arguments: parameters to be added in a string
/// - Returns: localized string
public func localized(with arguments: [CVarArg]) -> String {
return String(format: self.localized(), locale: nil, arguments: arguments)
}
}
// variable in a class
let tcAndPPMessage = "By_signing_up_or_logging_in,_you_agree_to_our"
.localized(with: [tAndc, pp, signin])
// Localization File String
"By_signing_up_or_logging_in,_you_agree_to_our" = "By signing up or logging in, you agree to our \"%@\" and \"%@\" \nAlready have an Account? \"%@\"";
回答by the Reverend
Here is an extension I use in String, it adds a localizeWithFormat function with variable arguments,
这是我在 String 中使用的扩展,它添加了一个带有可变参数的 localizeWithFormat 函数,
extension String:{
func localizeWithFormat(arguments: CVarArg...) -> String{
return String(format: self.localized, arguments: arguments)
}
var localized: String{
return Bundle.main.localizedString(forKey: self, value: nil, table: "StandardLocalizations")
}
}
Usage:
用法:
let siriCalendarText = "AnyCalendar"
let localizedText = "LTo use Siri with my app, please set %@ as the default list on your device reminders settings".localizeWithFormat(arguments: siriCalendarTitle)
Just be careful not to use the same function and property names that String has. I normally use a 3 letter prefix for all my framework functions.
请注意不要使用与 String 相同的函数和属性名称。我通常为所有框架函数使用 3 个字母的前缀。
回答by Santosh
I created an extension
to String
since I had many strings
to be localized
.
我创建了一个extension
来String
,因为我有很多strings
要localized
。
extension String {
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}
For example:
例如:
let myValue = 10
let anotherValue = "another value"
let localizedStr = "This string is localized: \(myValue) \(anotherValue)".localized
print(localizedStr)