And*_*ath 74 localization nslocalizedstring ios xliff swift
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(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare")
Run Code Online (Sandbox Code Playgroud)
or
NSLocalizedString("Notifica per \(medicina!) della prescrizione \(prescription!)\nMemo: \(memoTextView.text)", comment: "Messaggio della Local Notification")
Run Code Online (Sandbox Code Playgroud)
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.
zis*_*oft 113
You can use the sprintf format parameters within NSLocalizedString, so your example can look like this:
let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)
Run Code Online (Sandbox Code Playgroud)
Mar*_*ark 90
在WWDC2014"使用Xcode 6进行本地化"的会话#412中,Swift的正确方法如下:
String.localizedStringWithFormat(
NSLocalizedString(" - %d Notifica",
comment: "sottotitolo prescrizione per le notifiche al singolare"),
count)
Run Code Online (Sandbox Code Playgroud)
Jas*_*our 19
我已经按照创建String扩展的方法,因为我有很多字符串要本地化.
extension String {
var localized: String {
return NSLocalizedString(self, comment:"")
}
}
Run Code Online (Sandbox Code Playgroud)
要在代码中使用它进行本地化:
self.descriptionView.text = "Description".localized
Run Code Online (Sandbox Code Playgroud)
对于具有动态变量的字符串如下
self.entryTimeLabel.text = "\("Doors-open-at".localized) \(event.eventStartTime)"
Run Code Online (Sandbox Code Playgroud)
在String文件中声明不同语言的字符串(例如:阿拉伯语和英语)
希望会有所帮助!
the*_*end 11
这是我在 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")
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
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)
Run Code Online (Sandbox Code Playgroud)
请注意不要使用与 String 相同的函数和属性名称。我通常为所有框架函数使用 3 个字母的前缀。
Pra*_*tik 11
我尝试了上述解决方案,但下面的代码对我有用
快速 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? \"%@\"";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40349 次 |
| 最近记录: |