Skip to content

Commit

Permalink
Add String.LocalizationValue support on macOS 12
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 committed Mar 4, 2023
1 parent cfb6d2f commit 12f02aa
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Sources/MenuBuilder/MenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@ public struct MenuItem: AnyMenuItem {
}

/// Creates a menu item with the given title.
///
/// When targeting macOS 12 or later, `MenuBuilder("some title")` will use
/// this initializer for automatic localization support.
@available(macOS 12, *)
public init(
_ s: String.LocalizationValue,
table: String? = nil,
bundle: Bundle? = nil,
locale: Locale = .current,
comment: StaticString? = nil
) {
modifiers = [{ item in
item.title = String(localized: s, table: table, bundle: bundle, locale: locale, comment: comment)
}]
}

/// Creates a menu item with the given (non-localized) title.
@_disfavoredOverload
public init(_ title: String) {
modifiers = [ { item in item.title = title }]
modifiers = [{ item in item.title = title }]
}

/// Creates a menu item with the given localized string key used as the title.
public init(localized title: String, table: String? = nil, bundle: Bundle = .main) {
modifiers = [ { item in item.title = bundle.localizedString(forKey: title, value: nil, table: table) }]
modifiers = [{ item in item.title = bundle.localizedString(forKey: title, value: nil, table: table) }]
}

/// Creates a menu item with the given attributed title.
Expand All @@ -36,6 +54,7 @@ public struct MenuItem: AnyMenuItem {

/// Creates a menu item with the given attributed title.
@available(macOS 12, *)
@_disfavoredOverload
public init(_ title: AttributedString) {
modifiers = [{ item in
item.title = title.description
Expand Down

0 comments on commit 12f02aa

Please sign in to comment.