0

I want to display in a watch complication localized text that contains an argument set at runtime, e.g. „Available: 3“, where "3" should be set at runtime.
On iOS, this is easy: One defines a localized format string and insets into this format the actual parameter, like:

let str = String.init(format: NSLocalizedString("TEST", comment:"Test"), 3)  

where the Localizable.strings file contains an entry

"TEST" = "Available: %i";

In watchOS 3, if one wants to update a complication, one can use

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void)  

There, if the right complication type is provided, one could choose a complication template, and set some text, e.g. using:

let modularLargeTemplate = CLKComplicationTemplateModularLargeStandardBody()
modularLargeTemplate.headerImageProvider = CLKImageProvider.init(onePieceImage: UIImage(named: "Complication/Modular")!)
modularLargeTemplate.headerTextProvider = CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: „TEST“)  

where a file ckcomplication.strings contains e.g. an entry

"TEST" = "Available"

In this case, the complication would display „Available“.

The question is, how do I add an actual value, e.g. „3“, to the displayed text?

4

1 回答 1

0

可以像在 iOS 中一样做到这一点:

let str = String.init(format: NSLocalizedString("TEST", comment:" "), 3)
modularLargeTemplate.body1TextProvider = CLKSimpleTextProvider(text: str, shortText: str)  

Localizable.strings文件包含条目的 位置

"TEST" = "Available: %i";  

如果Localizable.strings是 iOS 应用程序和手表扩展的目标,则此方法有效。
但是,我不知道如何对CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: „TEST“).
实际上,我无法想象为什么 alocalizableTextProvider存在,如果它不能与运行时参数一起使用(显然),并且与上述解决方案相比(显然)没有提供任何优势。

于 2016-11-16T13:43:51.077 回答