Search code examples
localizationwatchos-3apple-watch-complication

Is it possible to have in a complication localized text with arguments?


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?


Solution

  • One can do it exactly like in iOS:

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

    where the Localizable.strings file contains an entry

    "TEST" = "Available: %i";  
    

    This works, if Localizable.strings is target of the iOS app, and the watch extension.
    However, I don’t know how to do the same with CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: „TEST“).
    Actually, I cannot imagine why a localizableTextProvider exists at all, if it cannot be used together with run time arguments (apparently), and does not provide any advantage (apparently) compared to the solution above.