SwiftUI,如何翻译带有计时器样式的文本?
SwiftUI, how to translate Text with timer style?
我们正在尝试创建一个 iOS 14 小部件,由于 API 的限制,我们无法使用计时器,但是我们发现我们可以使用带有日期样式的文本自我更新,但是可用的文档很少,而且文本似乎没有根据 phone 语言进行自我调整。
来自 Xcode 内部接口:
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
extension Text {
/// A predefined style used to display a `Date`.
public struct DateStyle {
/// A style displaying only the time component for a date.
///
/// Text(event.startDate, style: .time)
///
/// Example output:
/// 11:23PM
public static let time: Text.DateStyle
/// A style displaying a date.
///
/// Text(event.startDate, style: .date)
///
/// Example output:
/// June 3, 2019
public static let date: Text.DateStyle
/// A style displaying a date as relative to now.
///
/// Text(event.startDate, style: .relative)
///
/// Example output:
/// 2 hours, 23 minutes
/// 1 year, 1 month
public static let relative: Text.DateStyle
/// A style displaying a date as offset from now.
///
/// Text(event.startDate, style: .offset)
///
/// Example output:
/// +2 hours
/// -3 months
public static let offset: Text.DateStyle
/// A style displaying a date as timer counting from now.
///
/// Text(event.startDate, style: .timer)
///
/// Example output:
/// 2:32
/// 36:59:01
public static let timer: Text.DateStyle
}
/// Creates an instance that displays localized dates and times using a specific style.
///
/// - Parameters:
/// - date: The target date to display.
/// - style: The style used when displaying a date.
public init(_ date: Date, style: Text.DateStyle)
/// Creates an instance that displays a localized range between two dates.
///
/// - Parameters:
/// - dates: The range of dates to display
public init(_ dates: ClosedRange<Date>)
/// Creates an instance that displays a localized time interval.
///
/// Text(DateInterval(start: event.startDate, duration: event.duration))
///
/// Example output:
/// 9:30AM - 3:30PM
///
/// - Parameters:
/// - interval: The date interval to display
public init(_ interval: DateInterval)
}
我们的代码:
Text(entry.end!, style: .relative)
渲染:
我们需要将此文本调整为 phone 语言环境(截图中为德语),有人实现了吗?
无需担心本地化,在模拟器上它采用macOS语言配置,但在设备上采用设备语言。
我们正在尝试创建一个 iOS 14 小部件,由于 API 的限制,我们无法使用计时器,但是我们发现我们可以使用带有日期样式的文本自我更新,但是可用的文档很少,而且文本似乎没有根据 phone 语言进行自我调整。
来自 Xcode 内部接口:
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
extension Text {
/// A predefined style used to display a `Date`.
public struct DateStyle {
/// A style displaying only the time component for a date.
///
/// Text(event.startDate, style: .time)
///
/// Example output:
/// 11:23PM
public static let time: Text.DateStyle
/// A style displaying a date.
///
/// Text(event.startDate, style: .date)
///
/// Example output:
/// June 3, 2019
public static let date: Text.DateStyle
/// A style displaying a date as relative to now.
///
/// Text(event.startDate, style: .relative)
///
/// Example output:
/// 2 hours, 23 minutes
/// 1 year, 1 month
public static let relative: Text.DateStyle
/// A style displaying a date as offset from now.
///
/// Text(event.startDate, style: .offset)
///
/// Example output:
/// +2 hours
/// -3 months
public static let offset: Text.DateStyle
/// A style displaying a date as timer counting from now.
///
/// Text(event.startDate, style: .timer)
///
/// Example output:
/// 2:32
/// 36:59:01
public static let timer: Text.DateStyle
}
/// Creates an instance that displays localized dates and times using a specific style.
///
/// - Parameters:
/// - date: The target date to display.
/// - style: The style used when displaying a date.
public init(_ date: Date, style: Text.DateStyle)
/// Creates an instance that displays a localized range between two dates.
///
/// - Parameters:
/// - dates: The range of dates to display
public init(_ dates: ClosedRange<Date>)
/// Creates an instance that displays a localized time interval.
///
/// Text(DateInterval(start: event.startDate, duration: event.duration))
///
/// Example output:
/// 9:30AM - 3:30PM
///
/// - Parameters:
/// - interval: The date interval to display
public init(_ interval: DateInterval)
}
我们的代码:
Text(entry.end!, style: .relative)
渲染:
我们需要将此文本调整为 phone 语言环境(截图中为德语),有人实现了吗?
无需担心本地化,在模拟器上它采用macOS语言配置,但在设备上采用设备语言。