Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions Sources/FOSMVVM/Localization/LocalizedProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ public extension RetrievablePropertyNames {
/// under the model name. If no value (default: nil) is provided, the name of the property
/// that the *PropertyWrapper* is attached to is used.
/// - index: An optional index into an arrayValue that is appended to *propertyName* (0...n-1)
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
///
/// - See also: ``LocalizableRef``*.init()*
public init(parentKey: String? = nil, propertyName: String? = nil, index: Int? = nil, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableString {
Expand All @@ -223,6 +225,8 @@ public extension RetrievablePropertyNames {
/// under the model name. If no value (default: nil) is provided, the name of the property
/// that the *PropertyWrapper* is attached to is used.
/// - index: An optional index into an arrayValue that is appended to *propertyName* (0...n-1)
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
///
/// - See also: ``LocalizableRef``*.init()*
public init(parentKeys: String..., propertyName: String? = nil, index: Int? = nil, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableString {
Expand Down Expand Up @@ -251,6 +255,8 @@ public extension RetrievablePropertyNames {
/// under the model name. If no value (default: nil) is provided, the name of the property
/// that the *PropertyWrapper* is attached to is used.
/// - index: An optional index into an arrayValue that is appended to *propertyName* (0...n-1)
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
///
/// - See also: ``LocalizableRef``*.init()*
public init(parentKeys: [String], propertyName: String? = nil, index: Int? = nil, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableString {
Expand Down Expand Up @@ -289,6 +295,8 @@ public extension RetrievablePropertyNames {
/// separated with the grouping separator (default: true)
/// - groupingSize: The number of digits in the group, if showing a grouping
/// separator (default: 3)
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
public init(value: Int? = nil, showGroupingSeparator: Bool = true, groupingSize: Int = 3, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableInt {
self.localizationId = .random(length: 10)
self.wrappedValue = .init(
Expand Down Expand Up @@ -319,6 +327,8 @@ public extension RetrievablePropertyNames {
/// - separatorKeyPath: An optional ``KeyPath`` to a property of type ``LocalizedString``
/// that contains the **String** to place between each **String** in the
/// pieces array
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
public init(pieces piecesKeyPath: KeyPath<Model, _LocalizedArrayProperty<Model, LocalizableString>> & Sendable, separator separatorKeyPath: (KeyPath<Model, _LocalizedProperty<Model, LocalizableString>> & Sendable)? = nil, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableCompoundValue<LocalizableString> {
self.wrappedValue = Value.stub() // Bound later when propertyName is set
self.localizationId = .random(length: 10)
Expand Down Expand Up @@ -368,8 +378,6 @@ public extension RetrievablePropertyNames {

/// Initializes the ``LocalizeSubs`` property wrapper
///
///
///
/// ## Example
///
/// ``` swift
Expand All @@ -386,8 +394,9 @@ public extension RetrievablePropertyNames {
/// ```
///
/// - Parameters:
/// - substitutions: A ``KeyPath`` to a property of type ``LocalizedStrings``
/// that contains the **String** pieces to combine into a single **String**
/// - substitutions: A ``KeyPath`` to a property of type ``Localizable`` that contains the ``Localizable`` pieces to combine into a single **String**
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
public init(substitutions: KeyPath<Model, [String: some Localizable]> & Sendable, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableSubstitutions {
self.wrappedValue = Value.stub() // Bound later when propertyName is set
self.localizationId = .random(length: 10)
Expand All @@ -413,7 +422,28 @@ public extension RetrievablePropertyNames {
self.vLast = vLast
}

public init(_ propertyName: String? = nil, substitutions: KeyPath<Model, [String: some Localizable]> & Sendable, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableSubstitutions {
/// Initializes the ``LocalizeSubs`` property wrapper
///
/// ## Example
///
/// ``` swift
/// @ViewModel struct MyViewModel {
/// @LocalizedSubs(substitutions: \.multiTypedSubs) var aLocalizedMultiTypedSubstitution
///
/// private var multiTypedSubs: [String: any Localizable] { [
/// "string": LocalizableString.constant("a constant string"),
/// "int": LocalizableInt(value: 42),
/// "date": LocalizableDate(value: .now)
/// ] }
/// }
/// ```
///
/// - Parameters:
/// - substitutions: A ``KeyPath`` to a property of type ``Localizable``
/// that contains the ``Localizable`` pieces to combine into a single **String**
/// - vFirst: The first *SystemVersion* that this property is valid in
/// - vLast: The last *SystemVersion* that this property is valid in
public init(substitutions: KeyPath<Model, [String: any Localizable]> & Sendable, vFirst: SystemVersion? = nil, vLast: SystemVersion? = nil) where Value == LocalizableSubstitutions {
self.wrappedValue = Value.stub() // Bound later when propertyName is set
self.localizationId = .random(length: 10)
self.bindWrappedValue = { model, propertyName, _ in
Expand All @@ -437,6 +467,7 @@ public extension RetrievablePropertyNames {
self.vFirst = vFirst ?? .vInitial
self.vLast = vLast
}

}

public extension _LocalizedProperty {
Expand Down
1 change: 1 addition & 0 deletions Tests/FOSMVVMTests/TestYAML/TestViewModel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ en:
substitutions:
sub1: "Number 1"
sub2: "Number 2"
aLocalizedMultiTypedSubstitution: "String: %{string}, Int: %{int}, Date: %{date}"

es:
TestLocalizableViewModel:
Expand Down
8 changes: 8 additions & 0 deletions Tests/FOSMVVMVaporTests/TestViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ struct TestViewModel: RequestableViewModel {
@LocalizedSubs(substitutions: \.substitutions) var aLocalizedSubstitution
private let substitutions: [String: LocalizableInt]

@LocalizedSubs(substitutions: \.multiTypedSubs) var aLocalizedMultiTypedSubstitution

private var multiTypedSubs: [String: any Localizable] { [
"string": LocalizableString.constant("a constant string"),
"int": LocalizableInt(value: 42),
"date": LocalizableDate(value: .now)
] }

var vmId = ViewModelId()

var displayName: LocalizableString { .constant("TestVM") }
Expand Down