diff --git a/Contexts/Strings/empty.plist b/Contexts/Strings/empty.plist index a824feb..2c510c7 100644 --- a/Contexts/Strings/empty.plist +++ b/Contexts/Strings/empty.plist @@ -3,13 +3,6 @@ tables - - - levels - - name - Localizable - - + diff --git a/Contexts/Strings/multiline.plist b/Contexts/Strings/multiline.plist index 4a6ccbe..2944f04 100644 --- a/Contexts/Strings/multiline.plist +++ b/Contexts/Strings/multiline.plist @@ -53,7 +53,7 @@ Key name - Localizable + LocMultiline diff --git a/Contexts/Strings/multiple.plist b/Contexts/Strings/multiple.plist new file mode 100644 index 0000000..b75102b --- /dev/null +++ b/Contexts/Strings/multiple.plist @@ -0,0 +1,296 @@ + + + + + tables + + + levels + + children + + + name + Apples + strings + + + key + apples.count + name + count + translation + You have %d apples + types + + Int + + + + + + name + Bananas + strings + + + key + bananas.owner + name + owner + translation + Those %d bananas belong to %@. + types + + Int + String + + + + + + children + + + children + + + children + + + children + + + children + + + children + + + children + + + children + + + children + + + children + + + children + + + name + Is + strings + + + key + settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep + name + deep + translation + DeepSettings + + + + + name + This + + + name + Really + + + name + No + + + name + Handle + + + name + Can + + + name + We + + + name + Than + + + name + Deeper + + + name + Even + strings + + + key + settings.navigation-bar.title.even.deeper + name + deeper + translation + Settings + + + + + name + Title + + + name + Navigation_Bar + strings + + + key + settings.navigation-bar.self + name + self + translation + Some Reserved Keyword there + + + + + name + User_Profile_Section + strings + + + key + seTTings.uSer-proFile-sectioN.footer_text + name + footer_text + translation + Here you can change some user profile settings. + + + key + SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE + name + HEADER_TITLE + translation + User Profile Settings + + + + + name + Settings + + + strings + + + key + alert_message + name + alert_message + translation + Some alert body there + + + key + alert_title + name + alert_title + translation + Title of the alert + + + key + ObjectOwnership + name + ObjectOwnership + translation + These are %3$@'s %1$d %2$@. + types + + Int + String + String + + + + key + private + name + private + translation + Hello, my name is %@ and I'm %d + types + + String + Int + + + + + name + Localizable + + + levels + + strings + + + key + MULTILINE + name + MULTILINE + translation + multi\nline + + + key + multiLine\nKey + name + multiLine +Key + translation + test + + + key + MULTILINE2 + name + MULTILINE2 + translation + another\nmulti\n line + + + key + SINGLELINE + name + SINGLELINE + translation + single line + + + key + SINGLELINE2 + name + SINGLELINE2 + translation + another single line + + + + name + LocMultiline + + + + diff --git a/Contexts/Strings/structuredonly.plist b/Contexts/Strings/structuredonly.plist index 244dc05..2746b17 100644 --- a/Contexts/Strings/structuredonly.plist +++ b/Contexts/Strings/structuredonly.plist @@ -95,7 +95,7 @@ name - Localizable + LocStructuredOnly diff --git a/Contexts/Strings/utf8.plist b/Contexts/Strings/utf8.plist index 7a46328..db294bb 100644 --- a/Contexts/Strings/utf8.plist +++ b/Contexts/Strings/utf8.plist @@ -167,7 +167,7 @@ name - Localizable + LocUTF8 diff --git a/Documentation/strings/flat-swift2.md b/Documentation/strings/flat-swift2.md index 48b61ca..95488ac 100644 --- a/Documentation/strings/flat-swift2.md +++ b/Documentation/strings/flat-swift2.md @@ -44,8 +44,8 @@ enum L10n { ```swift // Simple strings -let message = L10n.AlertMessage.string -let title = tr(.AlertTitle) +let message = L10n.AlertMessage +let title = L10n.AlertTitle // with parameters, note that each argument needs to be of the correct type let apples = L10n.ApplesCount(3) diff --git a/Documentation/strings/flat-swift3.md b/Documentation/strings/flat-swift3.md index 4309750..3a51b5f 100644 --- a/Documentation/strings/flat-swift3.md +++ b/Documentation/strings/flat-swift3.md @@ -44,8 +44,8 @@ enum L10n { ```swift // Simple strings -let message = L10n.alertMessage.string -let title = tr(.alertTitle) +let message = L10n.alertMessage +let title = L10n.alertTitle // with parameters, note that each argument needs to be of the correct type let apples = L10n.applesCount(3) diff --git a/Tests/Expected/Strings/flat-swift2-context-localizable-customname.swift b/Tests/Expected/Strings/flat-swift2-context-localizable-customname.swift index 8a28815..0b8c394 100644 --- a/Tests/Expected/Strings/flat-swift2-context-localizable-customname.swift +++ b/Tests/Expected/Strings/flat-swift2-context-localizable-customname.swift @@ -7,68 +7,43 @@ import Foundation // swiftlint:disable identifier_name line_length type_body_length enum XCTLoc { /// Some alert body there - case AlertMessage + static let AlertMessage = XCTLoc.tr("Localizable", "alert_message") /// Title of the alert - case AlertTitle + static let AlertTitle = XCTLoc.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. - case ObjectOwnership(Int, String, String) + static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { + return XCTLoc.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } /// Hello, my name is %@ and I'm %d - case Private(String, Int) + static func Private(p1: String, p2: Int) -> String { + return XCTLoc.tr("Localizable", "private", p1, p2) + } /// You have %d apples - case ApplesCount(Int) + static func ApplesCount(p1: Int) -> String { + return XCTLoc.tr("Localizable", "apples.count", p1) + } /// Those %d bananas belong to %@. - case BananasOwner(Int, String) + static func BananasOwner(p1: Int, p2: String) -> String { + return XCTLoc.tr("Localizable", "bananas.owner", p1, p2) + } /// Some Reserved Keyword there - case SettingsNavigationBarSelf + static let SettingsNavigationBarSelf = XCTLoc.tr("Localizable", "settings.navigation-bar.self") /// DeepSettings - case SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep + static let SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = XCTLoc.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") /// Settings - case SettingsNavigationBarTitleEvenDeeper + static let SettingsNavigationBarTitleEvenDeeper = XCTLoc.tr("Localizable", "settings.navigation-bar.title.even.deeper") /// Here you can change some user profile settings. - case SeTTingsUSerProFileSectioNFooterText + static let SeTTingsUSerProFileSectioNFooterText = XCTLoc.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - case SettingsUserProfileSectionHeaderTitle + static let SettingsUserProfileSectionHeaderTitle = XCTLoc.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } +// swiftlint:enable identifier_name line_length type_body_length -extension XCTLoc: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - case .AlertMessage: - return XCTLoc.tr("alert_message") - case .AlertTitle: - return XCTLoc.tr("alert_title") - case .ObjectOwnership(let p1, let p2, let p3): - return XCTLoc.tr("ObjectOwnership", p1, p2, p3) - case .Private(let p1, let p2): - return XCTLoc.tr("private", p1, p2) - case .ApplesCount(let p1): - return XCTLoc.tr("apples.count", p1) - case .BananasOwner(let p1, let p2): - return XCTLoc.tr("bananas.owner", p1, p2) - case .SettingsNavigationBarSelf: - return XCTLoc.tr("settings.navigation-bar.self") - case .SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep: - return XCTLoc.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") - case .SettingsNavigationBarTitleEvenDeeper: - return XCTLoc.tr("settings.navigation-bar.title.even.deeper") - case .SeTTingsUSerProFileSectioNFooterText: - return XCTLoc.tr("seTTings.uSer-proFile-sectioN.footer_text") - case .SettingsUserProfileSectionHeaderTitle: - return XCTLoc.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") - } - } - - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") +extension XCTLoc { + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") return String(format: format, locale: NSLocale.currentLocale(), arguments: args) } } -// swiftlint:enable identifier_name line_length type_body_length - -func tr(key: XCTLoc) -> String { - return key.string -} private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift2-context-localizable-no-comments.swift b/Tests/Expected/Strings/flat-swift2-context-localizable-no-comments.swift index 99966b6..97fbc3c 100644 --- a/Tests/Expected/Strings/flat-swift2-context-localizable-no-comments.swift +++ b/Tests/Expected/Strings/flat-swift2-context-localizable-no-comments.swift @@ -6,58 +6,33 @@ import Foundation // swiftlint:disable identifier_name line_length type_body_length enum L10n { - case AlertMessage - case AlertTitle - case ObjectOwnership(Int, String, String) - case Private(String, Int) - case ApplesCount(Int) - case BananasOwner(Int, String) - case SettingsNavigationBarSelf - case SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep - case SettingsNavigationBarTitleEvenDeeper - case SeTTingsUSerProFileSectioNFooterText - case SettingsUserProfileSectionHeaderTitle -} - -extension L10n: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - case .AlertMessage: - return L10n.tr("alert_message") - case .AlertTitle: - return L10n.tr("alert_title") - case .ObjectOwnership(let p1, let p2, let p3): - return L10n.tr("ObjectOwnership", p1, p2, p3) - case .Private(let p1, let p2): - return L10n.tr("private", p1, p2) - case .ApplesCount(let p1): - return L10n.tr("apples.count", p1) - case .BananasOwner(let p1, let p2): - return L10n.tr("bananas.owner", p1, p2) - case .SettingsNavigationBarSelf: - return L10n.tr("settings.navigation-bar.self") - case .SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep: - return L10n.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") - case .SettingsNavigationBarTitleEvenDeeper: - return L10n.tr("settings.navigation-bar.title.even.deeper") - case .SeTTingsUSerProFileSectioNFooterText: - return L10n.tr("seTTings.uSer-proFile-sectioN.footer_text") - case .SettingsUserProfileSectionHeaderTitle: - return L10n.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") - } + static let AlertMessage = L10n.tr("Localizable", "alert_message") + static let AlertTitle = L10n.tr("Localizable", "alert_title") + static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) } - - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") - return String(format: format, locale: NSLocale.currentLocale(), arguments: args) + static func Private(p1: String, p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) } + static func ApplesCount(p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } + static func BananasOwner(p1: Int, p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } + static let SettingsNavigationBarSelf = L10n.tr("Localizable", "settings.navigation-bar.self") + static let SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let SettingsNavigationBarTitleEvenDeeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") + static let SeTTingsUSerProFileSectioNFooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + static let SettingsUserProfileSectionHeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } // swiftlint:enable identifier_name line_length type_body_length -func tr(key: L10n) -> String { - return key.string +extension L10n { + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") + return String(format: format, locale: NSLocale.currentLocale(), arguments: args) + } } private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift2-context-localizable.swift b/Tests/Expected/Strings/flat-swift2-context-localizable.swift index ca652b4..8f2c0f6 100644 --- a/Tests/Expected/Strings/flat-swift2-context-localizable.swift +++ b/Tests/Expected/Strings/flat-swift2-context-localizable.swift @@ -7,68 +7,43 @@ import Foundation // swiftlint:disable identifier_name line_length type_body_length enum L10n { /// Some alert body there - case AlertMessage + static let AlertMessage = L10n.tr("Localizable", "alert_message") /// Title of the alert - case AlertTitle + static let AlertTitle = L10n.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. - case ObjectOwnership(Int, String, String) + static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } /// Hello, my name is %@ and I'm %d - case Private(String, Int) + static func Private(p1: String, p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) + } /// You have %d apples - case ApplesCount(Int) + static func ApplesCount(p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } /// Those %d bananas belong to %@. - case BananasOwner(Int, String) + static func BananasOwner(p1: Int, p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } /// Some Reserved Keyword there - case SettingsNavigationBarSelf + static let SettingsNavigationBarSelf = L10n.tr("Localizable", "settings.navigation-bar.self") /// DeepSettings - case SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep + static let SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") /// Settings - case SettingsNavigationBarTitleEvenDeeper + static let SettingsNavigationBarTitleEvenDeeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") /// Here you can change some user profile settings. - case SeTTingsUSerProFileSectioNFooterText + static let SeTTingsUSerProFileSectioNFooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - case SettingsUserProfileSectionHeaderTitle + static let SettingsUserProfileSectionHeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } +// swiftlint:enable identifier_name line_length type_body_length -extension L10n: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - case .AlertMessage: - return L10n.tr("alert_message") - case .AlertTitle: - return L10n.tr("alert_title") - case .ObjectOwnership(let p1, let p2, let p3): - return L10n.tr("ObjectOwnership", p1, p2, p3) - case .Private(let p1, let p2): - return L10n.tr("private", p1, p2) - case .ApplesCount(let p1): - return L10n.tr("apples.count", p1) - case .BananasOwner(let p1, let p2): - return L10n.tr("bananas.owner", p1, p2) - case .SettingsNavigationBarSelf: - return L10n.tr("settings.navigation-bar.self") - case .SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep: - return L10n.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") - case .SettingsNavigationBarTitleEvenDeeper: - return L10n.tr("settings.navigation-bar.title.even.deeper") - case .SeTTingsUSerProFileSectioNFooterText: - return L10n.tr("seTTings.uSer-proFile-sectioN.footer_text") - case .SettingsUserProfileSectionHeaderTitle: - return L10n.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") - } - } - - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") +extension L10n { + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") return String(format: format, locale: NSLocale.currentLocale(), arguments: args) } } -// swiftlint:enable identifier_name line_length type_body_length - -func tr(key: L10n) -> String { - return key.string -} private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift2-context-multiple.swift b/Tests/Expected/Strings/flat-swift2-context-multiple.swift new file mode 100644 index 0000000..bae243b --- /dev/null +++ b/Tests/Expected/Strings/flat-swift2-context-multiple.swift @@ -0,0 +1,63 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +import Foundation + +// swiftlint:disable file_length + +// swiftlint:disable identifier_name line_length type_body_length +enum L10n { + enum Localizable { + /// Some alert body there + static let AlertMessage = L10n.tr("Localizable", "alert_message") + /// Title of the alert + static let AlertTitle = L10n.tr("Localizable", "alert_title") + /// These are %3$@'s %1$d %2$@. + static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } + /// Hello, my name is %@ and I'm %d + static func Private(p1: String, p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) + } + /// You have %d apples + static func ApplesCount(p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } + /// Those %d bananas belong to %@. + static func BananasOwner(p1: Int, p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } + /// Some Reserved Keyword there + static let SettingsNavigationBarSelf = L10n.tr("Localizable", "settings.navigation-bar.self") + /// DeepSettings + static let SettingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + /// Settings + static let SettingsNavigationBarTitleEvenDeeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") + /// Here you can change some user profile settings. + static let SeTTingsUSerProFileSectioNFooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + /// User Profile Settings + static let SettingsUserProfileSectionHeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + } + enum LocMultiline { + /// multi\nline + static let Multiline = L10n.tr("LocMultiline", "MULTILINE") + /// test + static let MultiLineNKey = L10n.tr("LocMultiline", "multiLine\nKey") + /// another\nmulti\n line + static let Multiline2 = L10n.tr("LocMultiline", "MULTILINE2") + /// single line + static let Singleline = L10n.tr("LocMultiline", "SINGLELINE") + /// another single line + static let Singleline2 = L10n.tr("LocMultiline", "SINGLELINE2") + } +} +// swiftlint:enable identifier_name line_length type_body_length + +extension L10n { + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") + return String(format: format, locale: NSLocale.currentLocale(), arguments: args) + } +} + +private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift3-context-localizable-customname.swift b/Tests/Expected/Strings/flat-swift3-context-localizable-customname.swift index 2ec5483..a480d87 100644 --- a/Tests/Expected/Strings/flat-swift3-context-localizable-customname.swift +++ b/Tests/Expected/Strings/flat-swift3-context-localizable-customname.swift @@ -7,68 +7,43 @@ import Foundation // swiftlint:disable identifier_name line_length type_body_length enum XCTLoc { /// Some alert body there - case alertMessage + static let alertMessage = XCTLoc.tr("Localizable", "alert_message") /// Title of the alert - case alertTitle + static let alertTitle = XCTLoc.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. - case objectOwnership(Int, String, String) + static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { + return XCTLoc.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } /// Hello, my name is %@ and I'm %d - case `private`(String, Int) + static func `private`(_ p1: String, _ p2: Int) -> String { + return XCTLoc.tr("Localizable", "private", p1, p2) + } /// You have %d apples - case applesCount(Int) + static func applesCount(_ p1: Int) -> String { + return XCTLoc.tr("Localizable", "apples.count", p1) + } /// Those %d bananas belong to %@. - case bananasOwner(Int, String) + static func bananasOwner(_ p1: Int, _ p2: String) -> String { + return XCTLoc.tr("Localizable", "bananas.owner", p1, p2) + } /// Some Reserved Keyword there - case settingsNavigationBarSelf + static let settingsNavigationBarSelf = XCTLoc.tr("Localizable", "settings.navigation-bar.self") /// DeepSettings - case settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep + static let settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = XCTLoc.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") /// Settings - case settingsNavigationBarTitleEvenDeeper + static let settingsNavigationBarTitleEvenDeeper = XCTLoc.tr("Localizable", "settings.navigation-bar.title.even.deeper") /// Here you can change some user profile settings. - case seTTingsUSerProFileSectioNFooterText + static let seTTingsUSerProFileSectioNFooterText = XCTLoc.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - case settingsUserProfileSectionHeaderTitle + static let settingsUserProfileSectionHeaderTitle = XCTLoc.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } +// swiftlint:enable identifier_name line_length type_body_length -extension XCTLoc: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - case .alertMessage: - return XCTLoc.tr(key: "alert_message") - case .alertTitle: - return XCTLoc.tr(key: "alert_title") - case .objectOwnership(let p1, let p2, let p3): - return XCTLoc.tr(key: "ObjectOwnership", p1, p2, p3) - case .`private`(let p1, let p2): - return XCTLoc.tr(key: "private", p1, p2) - case .applesCount(let p1): - return XCTLoc.tr(key: "apples.count", p1) - case .bananasOwner(let p1, let p2): - return XCTLoc.tr(key: "bananas.owner", p1, p2) - case .settingsNavigationBarSelf: - return XCTLoc.tr(key: "settings.navigation-bar.self") - case .settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep: - return XCTLoc.tr(key: "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") - case .settingsNavigationBarTitleEvenDeeper: - return XCTLoc.tr(key: "settings.navigation-bar.title.even.deeper") - case .seTTingsUSerProFileSectioNFooterText: - return XCTLoc.tr(key: "seTTings.uSer-proFile-sectioN.footer_text") - case .settingsUserProfileSectionHeaderTitle: - return XCTLoc.tr(key: "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") - } - } - - private static func tr(key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") +extension XCTLoc { + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") return String(format: format, locale: Locale.current, arguments: args) } } -// swiftlint:enable identifier_name line_length type_body_length - -func tr(_ key: XCTLoc) -> String { - return key.string -} private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift3-context-localizable-no-comments.swift b/Tests/Expected/Strings/flat-swift3-context-localizable-no-comments.swift index 0ec798b..89d2f7a 100644 --- a/Tests/Expected/Strings/flat-swift3-context-localizable-no-comments.swift +++ b/Tests/Expected/Strings/flat-swift3-context-localizable-no-comments.swift @@ -6,58 +6,33 @@ import Foundation // swiftlint:disable identifier_name line_length type_body_length enum L10n { - case alertMessage - case alertTitle - case objectOwnership(Int, String, String) - case `private`(String, Int) - case applesCount(Int) - case bananasOwner(Int, String) - case settingsNavigationBarSelf - case settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep - case settingsNavigationBarTitleEvenDeeper - case seTTingsUSerProFileSectioNFooterText - case settingsUserProfileSectionHeaderTitle -} - -extension L10n: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - case .alertMessage: - return L10n.tr(key: "alert_message") - case .alertTitle: - return L10n.tr(key: "alert_title") - case .objectOwnership(let p1, let p2, let p3): - return L10n.tr(key: "ObjectOwnership", p1, p2, p3) - case .`private`(let p1, let p2): - return L10n.tr(key: "private", p1, p2) - case .applesCount(let p1): - return L10n.tr(key: "apples.count", p1) - case .bananasOwner(let p1, let p2): - return L10n.tr(key: "bananas.owner", p1, p2) - case .settingsNavigationBarSelf: - return L10n.tr(key: "settings.navigation-bar.self") - case .settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep: - return L10n.tr(key: "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") - case .settingsNavigationBarTitleEvenDeeper: - return L10n.tr(key: "settings.navigation-bar.title.even.deeper") - case .seTTingsUSerProFileSectioNFooterText: - return L10n.tr(key: "seTTings.uSer-proFile-sectioN.footer_text") - case .settingsUserProfileSectionHeaderTitle: - return L10n.tr(key: "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") - } + static let alertMessage = L10n.tr("Localizable", "alert_message") + static let alertTitle = L10n.tr("Localizable", "alert_title") + static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) } - - private static func tr(key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") - return String(format: format, locale: Locale.current, arguments: args) + static func `private`(_ p1: String, _ p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) } + static func applesCount(_ p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } + static func bananasOwner(_ p1: Int, _ p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } + static let settingsNavigationBarSelf = L10n.tr("Localizable", "settings.navigation-bar.self") + static let settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let settingsNavigationBarTitleEvenDeeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") + static let seTTingsUSerProFileSectioNFooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + static let settingsUserProfileSectionHeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } // swiftlint:enable identifier_name line_length type_body_length -func tr(_ key: L10n) -> String { - return key.string +extension L10n { + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") + return String(format: format, locale: Locale.current, arguments: args) + } } private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift3-context-localizable.swift b/Tests/Expected/Strings/flat-swift3-context-localizable.swift index bd6f095..a393dc9 100644 --- a/Tests/Expected/Strings/flat-swift3-context-localizable.swift +++ b/Tests/Expected/Strings/flat-swift3-context-localizable.swift @@ -7,68 +7,43 @@ import Foundation // swiftlint:disable identifier_name line_length type_body_length enum L10n { /// Some alert body there - case alertMessage + static let alertMessage = L10n.tr("Localizable", "alert_message") /// Title of the alert - case alertTitle + static let alertTitle = L10n.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. - case objectOwnership(Int, String, String) + static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } /// Hello, my name is %@ and I'm %d - case `private`(String, Int) + static func `private`(_ p1: String, _ p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) + } /// You have %d apples - case applesCount(Int) + static func applesCount(_ p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } /// Those %d bananas belong to %@. - case bananasOwner(Int, String) + static func bananasOwner(_ p1: Int, _ p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } /// Some Reserved Keyword there - case settingsNavigationBarSelf + static let settingsNavigationBarSelf = L10n.tr("Localizable", "settings.navigation-bar.self") /// DeepSettings - case settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep + static let settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") /// Settings - case settingsNavigationBarTitleEvenDeeper + static let settingsNavigationBarTitleEvenDeeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") /// Here you can change some user profile settings. - case seTTingsUSerProFileSectioNFooterText + static let seTTingsUSerProFileSectioNFooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - case settingsUserProfileSectionHeaderTitle + static let settingsUserProfileSectionHeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } +// swiftlint:enable identifier_name line_length type_body_length -extension L10n: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - case .alertMessage: - return L10n.tr(key: "alert_message") - case .alertTitle: - return L10n.tr(key: "alert_title") - case .objectOwnership(let p1, let p2, let p3): - return L10n.tr(key: "ObjectOwnership", p1, p2, p3) - case .`private`(let p1, let p2): - return L10n.tr(key: "private", p1, p2) - case .applesCount(let p1): - return L10n.tr(key: "apples.count", p1) - case .bananasOwner(let p1, let p2): - return L10n.tr(key: "bananas.owner", p1, p2) - case .settingsNavigationBarSelf: - return L10n.tr(key: "settings.navigation-bar.self") - case .settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep: - return L10n.tr(key: "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") - case .settingsNavigationBarTitleEvenDeeper: - return L10n.tr(key: "settings.navigation-bar.title.even.deeper") - case .seTTingsUSerProFileSectioNFooterText: - return L10n.tr(key: "seTTings.uSer-proFile-sectioN.footer_text") - case .settingsUserProfileSectionHeaderTitle: - return L10n.tr(key: "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") - } - } - - private static func tr(key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") +extension L10n { + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") return String(format: format, locale: Locale.current, arguments: args) } } -// swiftlint:enable identifier_name line_length type_body_length - -func tr(_ key: L10n) -> String { - return key.string -} private final class BundleToken {} diff --git a/Tests/Expected/Strings/flat-swift3-context-multiple.swift b/Tests/Expected/Strings/flat-swift3-context-multiple.swift new file mode 100644 index 0000000..43cf41b --- /dev/null +++ b/Tests/Expected/Strings/flat-swift3-context-multiple.swift @@ -0,0 +1,63 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +import Foundation + +// swiftlint:disable file_length + +// swiftlint:disable identifier_name line_length type_body_length +enum L10n { + enum Localizable { + /// Some alert body there + static let alertMessage = L10n.tr("Localizable", "alert_message") + /// Title of the alert + static let alertTitle = L10n.tr("Localizable", "alert_title") + /// These are %3$@'s %1$d %2$@. + static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } + /// Hello, my name is %@ and I'm %d + static func `private`(_ p1: String, _ p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) + } + /// You have %d apples + static func applesCount(_ p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } + /// Those %d bananas belong to %@. + static func bananasOwner(_ p1: Int, _ p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } + /// Some Reserved Keyword there + static let settingsNavigationBarSelf = L10n.tr("Localizable", "settings.navigation-bar.self") + /// DeepSettings + static let settingsNavigationBarTitleDeeperThanWeCanHandleNoReallyThisIsDeep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + /// Settings + static let settingsNavigationBarTitleEvenDeeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") + /// Here you can change some user profile settings. + static let seTTingsUSerProFileSectioNFooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + /// User Profile Settings + static let settingsUserProfileSectionHeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + } + enum LocMultiline { + /// multi\nline + static let multiline = L10n.tr("LocMultiline", "MULTILINE") + /// test + static let multiLineNKey = L10n.tr("LocMultiline", "multiLine\nKey") + /// another\nmulti\n line + static let multiline2 = L10n.tr("LocMultiline", "MULTILINE2") + /// single line + static let singleline = L10n.tr("LocMultiline", "SINGLELINE") + /// another single line + static let singleline2 = L10n.tr("LocMultiline", "SINGLELINE2") + } +} +// swiftlint:enable identifier_name line_length type_body_length + +extension L10n { + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") + return String(format: format, locale: Locale.current, arguments: args) + } +} + +private final class BundleToken {} diff --git a/Tests/Expected/Strings/structured-swift2-context-localizable-customname.swift b/Tests/Expected/Strings/structured-swift2-context-localizable-customname.swift index 4ab4632..3a50363 100644 --- a/Tests/Expected/Strings/structured-swift2-context-localizable-customname.swift +++ b/Tests/Expected/Strings/structured-swift2-context-localizable-customname.swift @@ -7,29 +7,29 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name enum XCTLoc { /// Some alert body there - static let AlertMessage = XCTLoc.tr("alert_message") + static let AlertMessage = XCTLoc.tr("Localizable", "alert_message") /// Title of the alert - static let AlertTitle = XCTLoc.tr("alert_title") + static let AlertTitle = XCTLoc.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { - return XCTLoc.tr("ObjectOwnership", p1, p2, p3) + return XCTLoc.tr("Localizable", "ObjectOwnership", p1, p2, p3) } /// Hello, my name is %@ and I'm %d static func Private(p1: String, p2: Int) -> String { - return XCTLoc.tr("private", p1, p2) + return XCTLoc.tr("Localizable", "private", p1, p2) } enum Apples { /// You have %d apples static func Count(p1: Int) -> String { - return XCTLoc.tr("apples.count", p1) + return XCTLoc.tr("Localizable", "apples.count", p1) } } enum Bananas { /// Those %d bananas belong to %@. static func Owner(p1: Int, p2: String) -> String { - return XCTLoc.tr("bananas.owner", p1, p2) + return XCTLoc.tr("Localizable", "bananas.owner", p1, p2) } } @@ -37,7 +37,7 @@ enum XCTLoc { enum NavigationBar { /// Some Reserved Keyword there - static let `Self` = XCTLoc.tr("settings.navigation-bar.self") + static let `Self` = XCTLoc.tr("Localizable", "settings.navigation-bar.self") enum Title { @@ -59,7 +59,7 @@ enum XCTLoc { enum Is { /// DeepSettings - static let Deep = XCTLoc.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let Deep = XCTLoc.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") } } } @@ -72,24 +72,24 @@ enum XCTLoc { enum Even { /// Settings - static let Deeper = XCTLoc.tr("settings.navigation-bar.title.even.deeper") + static let Deeper = XCTLoc.tr("Localizable", "settings.navigation-bar.title.even.deeper") } } } enum UserProfileSection { /// Here you can change some user profile settings. - static let FooterText = XCTLoc.tr("seTTings.uSer-proFile-sectioN.footer_text") + static let FooterText = XCTLoc.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - static let HeaderTitle = XCTLoc.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + static let HeaderTitle = XCTLoc.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } } } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension XCTLoc { - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") return String(format: format, locale: NSLocale.currentLocale(), arguments: args) } } diff --git a/Tests/Expected/Strings/structured-swift2-context-localizable-no-comments.swift b/Tests/Expected/Strings/structured-swift2-context-localizable-no-comments.swift index 83a622c..ebd8b55 100644 --- a/Tests/Expected/Strings/structured-swift2-context-localizable-no-comments.swift +++ b/Tests/Expected/Strings/structured-swift2-context-localizable-no-comments.swift @@ -6,31 +6,31 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name enum L10n { - static let AlertMessage = L10n.tr("alert_message") - static let AlertTitle = L10n.tr("alert_title") + static let AlertMessage = L10n.tr("Localizable", "alert_message") + static let AlertTitle = L10n.tr("Localizable", "alert_title") static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { - return L10n.tr("ObjectOwnership", p1, p2, p3) + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) } static func Private(p1: String, p2: Int) -> String { - return L10n.tr("private", p1, p2) + return L10n.tr("Localizable", "private", p1, p2) } enum Apples { static func Count(p1: Int) -> String { - return L10n.tr("apples.count", p1) + return L10n.tr("Localizable", "apples.count", p1) } } enum Bananas { static func Owner(p1: Int, p2: String) -> String { - return L10n.tr("bananas.owner", p1, p2) + return L10n.tr("Localizable", "bananas.owner", p1, p2) } } enum Settings { enum NavigationBar { - static let `Self` = L10n.tr("settings.navigation-bar.self") + static let `Self` = L10n.tr("Localizable", "settings.navigation-bar.self") enum Title { @@ -51,7 +51,7 @@ enum L10n { enum This { enum Is { - static let Deep = L10n.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let Deep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") } } } @@ -63,22 +63,22 @@ enum L10n { } enum Even { - static let Deeper = L10n.tr("settings.navigation-bar.title.even.deeper") + static let Deeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") } } } enum UserProfileSection { - static let FooterText = L10n.tr("seTTings.uSer-proFile-sectioN.footer_text") - static let HeaderTitle = L10n.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + static let FooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + static let HeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } } } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension L10n { - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") return String(format: format, locale: NSLocale.currentLocale(), arguments: args) } } diff --git a/Tests/Expected/Strings/structured-swift2-context-localizable.swift b/Tests/Expected/Strings/structured-swift2-context-localizable.swift index bc5f349..219414a 100644 --- a/Tests/Expected/Strings/structured-swift2-context-localizable.swift +++ b/Tests/Expected/Strings/structured-swift2-context-localizable.swift @@ -7,29 +7,29 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name enum L10n { /// Some alert body there - static let AlertMessage = L10n.tr("alert_message") + static let AlertMessage = L10n.tr("Localizable", "alert_message") /// Title of the alert - static let AlertTitle = L10n.tr("alert_title") + static let AlertTitle = L10n.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { - return L10n.tr("ObjectOwnership", p1, p2, p3) + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) } /// Hello, my name is %@ and I'm %d static func Private(p1: String, p2: Int) -> String { - return L10n.tr("private", p1, p2) + return L10n.tr("Localizable", "private", p1, p2) } enum Apples { /// You have %d apples static func Count(p1: Int) -> String { - return L10n.tr("apples.count", p1) + return L10n.tr("Localizable", "apples.count", p1) } } enum Bananas { /// Those %d bananas belong to %@. static func Owner(p1: Int, p2: String) -> String { - return L10n.tr("bananas.owner", p1, p2) + return L10n.tr("Localizable", "bananas.owner", p1, p2) } } @@ -37,7 +37,7 @@ enum L10n { enum NavigationBar { /// Some Reserved Keyword there - static let `Self` = L10n.tr("settings.navigation-bar.self") + static let `Self` = L10n.tr("Localizable", "settings.navigation-bar.self") enum Title { @@ -59,7 +59,7 @@ enum L10n { enum Is { /// DeepSettings - static let Deep = L10n.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let Deep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") } } } @@ -72,24 +72,24 @@ enum L10n { enum Even { /// Settings - static let Deeper = L10n.tr("settings.navigation-bar.title.even.deeper") + static let Deeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") } } } enum UserProfileSection { /// Here you can change some user profile settings. - static let FooterText = L10n.tr("seTTings.uSer-proFile-sectioN.footer_text") + static let FooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - static let HeaderTitle = L10n.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + static let HeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } } } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension L10n { - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") return String(format: format, locale: NSLocale.currentLocale(), arguments: args) } } diff --git a/Tests/Expected/Strings/structured-swift2-context-multiple.swift b/Tests/Expected/Strings/structured-swift2-context-multiple.swift new file mode 100644 index 0000000..1b2e4eb --- /dev/null +++ b/Tests/Expected/Strings/structured-swift2-context-multiple.swift @@ -0,0 +1,111 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +import Foundation + +// swiftlint:disable file_length + +// swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name +enum L10n { + enum Localizable { + /// Some alert body there + static let AlertMessage = L10n.tr("Localizable", "alert_message") + /// Title of the alert + static let AlertTitle = L10n.tr("Localizable", "alert_title") + /// These are %3$@'s %1$d %2$@. + static func ObjectOwnership(p1: Int, p2: String, p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } + /// Hello, my name is %@ and I'm %d + static func Private(p1: String, p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) + } + + enum Apples { + /// You have %d apples + static func Count(p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } + } + + enum Bananas { + /// Those %d bananas belong to %@. + static func Owner(p1: Int, p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } + } + + enum Settings { + + enum NavigationBar { + /// Some Reserved Keyword there + static let `Self` = L10n.tr("Localizable", "settings.navigation-bar.self") + + enum Title { + + enum Deeper { + + enum Than { + + enum We { + + enum Can { + + enum Handle { + + enum No { + + enum Really { + + enum This { + + enum Is { + /// DeepSettings + static let Deep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + } + } + } + } + } + } + } + } + } + + enum Even { + /// Settings + static let Deeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") + } + } + } + + enum UserProfileSection { + /// Here you can change some user profile settings. + static let FooterText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + /// User Profile Settings + static let HeaderTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + } + } + } + enum LocMultiline { + /// multi\nline + static let Multiline = L10n.tr("LocMultiline", "MULTILINE") + /// test + static let MultiLineKey = L10n.tr("LocMultiline", "multiLine\nKey") + /// another\nmulti\n line + static let Multiline2 = L10n.tr("LocMultiline", "MULTILINE2") + /// single line + static let Singleline = L10n.tr("LocMultiline", "SINGLELINE") + /// another single line + static let Singleline2 = L10n.tr("LocMultiline", "SINGLELINE2") + } +} +// swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name + +extension L10n { + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") + return String(format: format, locale: NSLocale.currentLocale(), arguments: args) + } +} + +private final class BundleToken {} diff --git a/Tests/Expected/Strings/structured-swift3-context-localizable-customname.swift b/Tests/Expected/Strings/structured-swift3-context-localizable-customname.swift index f861caa..ea4c7e6 100644 --- a/Tests/Expected/Strings/structured-swift3-context-localizable-customname.swift +++ b/Tests/Expected/Strings/structured-swift3-context-localizable-customname.swift @@ -7,29 +7,29 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name enum XCTLoc { /// Some alert body there - static let alertMessage = XCTLoc.tr("alert_message") + static let alertMessage = XCTLoc.tr("Localizable", "alert_message") /// Title of the alert - static let alertTitle = XCTLoc.tr("alert_title") + static let alertTitle = XCTLoc.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { - return XCTLoc.tr("ObjectOwnership", p1, p2, p3) + return XCTLoc.tr("Localizable", "ObjectOwnership", p1, p2, p3) } /// Hello, my name is %@ and I'm %d static func `private`(_ p1: String, _ p2: Int) -> String { - return XCTLoc.tr("private", p1, p2) + return XCTLoc.tr("Localizable", "private", p1, p2) } enum Apples { /// You have %d apples static func count(_ p1: Int) -> String { - return XCTLoc.tr("apples.count", p1) + return XCTLoc.tr("Localizable", "apples.count", p1) } } enum Bananas { /// Those %d bananas belong to %@. static func owner(_ p1: Int, _ p2: String) -> String { - return XCTLoc.tr("bananas.owner", p1, p2) + return XCTLoc.tr("Localizable", "bananas.owner", p1, p2) } } @@ -37,7 +37,7 @@ enum XCTLoc { enum NavigationBar { /// Some Reserved Keyword there - static let `self` = XCTLoc.tr("settings.navigation-bar.self") + static let `self` = XCTLoc.tr("Localizable", "settings.navigation-bar.self") enum Title { @@ -59,7 +59,7 @@ enum XCTLoc { enum Is { /// DeepSettings - static let deep = XCTLoc.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let deep = XCTLoc.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") } } } @@ -72,24 +72,24 @@ enum XCTLoc { enum Even { /// Settings - static let deeper = XCTLoc.tr("settings.navigation-bar.title.even.deeper") + static let deeper = XCTLoc.tr("Localizable", "settings.navigation-bar.title.even.deeper") } } } enum UserProfileSection { /// Here you can change some user profile settings. - static let footerText = XCTLoc.tr("seTTings.uSer-proFile-sectioN.footer_text") + static let footerText = XCTLoc.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - static let headerTitle = XCTLoc.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + static let headerTitle = XCTLoc.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } } } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension XCTLoc { - fileprivate static func tr(_ key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") return String(format: format, locale: Locale.current, arguments: args) } } diff --git a/Tests/Expected/Strings/structured-swift3-context-localizable-no-comments.swift b/Tests/Expected/Strings/structured-swift3-context-localizable-no-comments.swift index 6cdba1b..31396a7 100644 --- a/Tests/Expected/Strings/structured-swift3-context-localizable-no-comments.swift +++ b/Tests/Expected/Strings/structured-swift3-context-localizable-no-comments.swift @@ -6,31 +6,31 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name enum L10n { - static let alertMessage = L10n.tr("alert_message") - static let alertTitle = L10n.tr("alert_title") + static let alertMessage = L10n.tr("Localizable", "alert_message") + static let alertTitle = L10n.tr("Localizable", "alert_title") static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { - return L10n.tr("ObjectOwnership", p1, p2, p3) + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) } static func `private`(_ p1: String, _ p2: Int) -> String { - return L10n.tr("private", p1, p2) + return L10n.tr("Localizable", "private", p1, p2) } enum Apples { static func count(_ p1: Int) -> String { - return L10n.tr("apples.count", p1) + return L10n.tr("Localizable", "apples.count", p1) } } enum Bananas { static func owner(_ p1: Int, _ p2: String) -> String { - return L10n.tr("bananas.owner", p1, p2) + return L10n.tr("Localizable", "bananas.owner", p1, p2) } } enum Settings { enum NavigationBar { - static let `self` = L10n.tr("settings.navigation-bar.self") + static let `self` = L10n.tr("Localizable", "settings.navigation-bar.self") enum Title { @@ -51,7 +51,7 @@ enum L10n { enum This { enum Is { - static let deep = L10n.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let deep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") } } } @@ -63,22 +63,22 @@ enum L10n { } enum Even { - static let deeper = L10n.tr("settings.navigation-bar.title.even.deeper") + static let deeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") } } } enum UserProfileSection { - static let footerText = L10n.tr("seTTings.uSer-proFile-sectioN.footer_text") - static let headerTitle = L10n.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + static let footerText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + static let headerTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } } } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension L10n { - fileprivate static func tr(_ key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") return String(format: format, locale: Locale.current, arguments: args) } } diff --git a/Tests/Expected/Strings/structured-swift3-context-localizable.swift b/Tests/Expected/Strings/structured-swift3-context-localizable.swift index 9c2fa20..58dd4ac 100644 --- a/Tests/Expected/Strings/structured-swift3-context-localizable.swift +++ b/Tests/Expected/Strings/structured-swift3-context-localizable.swift @@ -7,29 +7,29 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name enum L10n { /// Some alert body there - static let alertMessage = L10n.tr("alert_message") + static let alertMessage = L10n.tr("Localizable", "alert_message") /// Title of the alert - static let alertTitle = L10n.tr("alert_title") + static let alertTitle = L10n.tr("Localizable", "alert_title") /// These are %3$@'s %1$d %2$@. static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { - return L10n.tr("ObjectOwnership", p1, p2, p3) + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) } /// Hello, my name is %@ and I'm %d static func `private`(_ p1: String, _ p2: Int) -> String { - return L10n.tr("private", p1, p2) + return L10n.tr("Localizable", "private", p1, p2) } enum Apples { /// You have %d apples static func count(_ p1: Int) -> String { - return L10n.tr("apples.count", p1) + return L10n.tr("Localizable", "apples.count", p1) } } enum Bananas { /// Those %d bananas belong to %@. static func owner(_ p1: Int, _ p2: String) -> String { - return L10n.tr("bananas.owner", p1, p2) + return L10n.tr("Localizable", "bananas.owner", p1, p2) } } @@ -37,7 +37,7 @@ enum L10n { enum NavigationBar { /// Some Reserved Keyword there - static let `self` = L10n.tr("settings.navigation-bar.self") + static let `self` = L10n.tr("Localizable", "settings.navigation-bar.self") enum Title { @@ -59,7 +59,7 @@ enum L10n { enum Is { /// DeepSettings - static let deep = L10n.tr("settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + static let deep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") } } } @@ -72,24 +72,24 @@ enum L10n { enum Even { /// Settings - static let deeper = L10n.tr("settings.navigation-bar.title.even.deeper") + static let deeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") } } } enum UserProfileSection { /// Here you can change some user profile settings. - static let footerText = L10n.tr("seTTings.uSer-proFile-sectioN.footer_text") + static let footerText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") /// User Profile Settings - static let headerTitle = L10n.tr("SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + static let headerTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") } } } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension L10n { - fileprivate static func tr(_ key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") return String(format: format, locale: Locale.current, arguments: args) } } diff --git a/Tests/Expected/Strings/structured-swift3-context-multiple.swift b/Tests/Expected/Strings/structured-swift3-context-multiple.swift new file mode 100644 index 0000000..6d8f9db --- /dev/null +++ b/Tests/Expected/Strings/structured-swift3-context-multiple.swift @@ -0,0 +1,111 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +import Foundation + +// swiftlint:disable file_length + +// swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name +enum L10n { + enum Localizable { + /// Some alert body there + static let alertMessage = L10n.tr("Localizable", "alert_message") + /// Title of the alert + static let alertTitle = L10n.tr("Localizable", "alert_title") + /// These are %3$@'s %1$d %2$@. + static func objectOwnership(_ p1: Int, _ p2: String, _ p3: String) -> String { + return L10n.tr("Localizable", "ObjectOwnership", p1, p2, p3) + } + /// Hello, my name is %@ and I'm %d + static func `private`(_ p1: String, _ p2: Int) -> String { + return L10n.tr("Localizable", "private", p1, p2) + } + + enum Apples { + /// You have %d apples + static func count(_ p1: Int) -> String { + return L10n.tr("Localizable", "apples.count", p1) + } + } + + enum Bananas { + /// Those %d bananas belong to %@. + static func owner(_ p1: Int, _ p2: String) -> String { + return L10n.tr("Localizable", "bananas.owner", p1, p2) + } + } + + enum Settings { + + enum NavigationBar { + /// Some Reserved Keyword there + static let `self` = L10n.tr("Localizable", "settings.navigation-bar.self") + + enum Title { + + enum Deeper { + + enum Than { + + enum We { + + enum Can { + + enum Handle { + + enum No { + + enum Really { + + enum This { + + enum Is { + /// DeepSettings + static let deep = L10n.tr("Localizable", "settings.navigation-bar.title.deeper.than.we.can.handle.no.really.this.is.deep") + } + } + } + } + } + } + } + } + } + + enum Even { + /// Settings + static let deeper = L10n.tr("Localizable", "settings.navigation-bar.title.even.deeper") + } + } + } + + enum UserProfileSection { + /// Here you can change some user profile settings. + static let footerText = L10n.tr("Localizable", "seTTings.uSer-proFile-sectioN.footer_text") + /// User Profile Settings + static let headerTitle = L10n.tr("Localizable", "SETTINGS.USER_PROFILE_SECTION.HEADER_TITLE") + } + } + } + enum LocMultiline { + /// multi\nline + static let multiline = L10n.tr("LocMultiline", "MULTILINE") + /// test + static let multiLineKey = L10n.tr("LocMultiline", "multiLine\nKey") + /// another\nmulti\n line + static let multiline2 = L10n.tr("LocMultiline", "MULTILINE2") + /// single line + static let singleline = L10n.tr("LocMultiline", "SINGLELINE") + /// another single line + static let singleline2 = L10n.tr("LocMultiline", "SINGLELINE2") + } +} +// swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name + +extension L10n { + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") + return String(format: format, locale: Locale.current, arguments: args) + } +} + +private final class BundleToken {} diff --git a/Tests/TemplatesTests/StringsTests.swift b/Tests/TemplatesTests/StringsTests.swift index 8ed04e6..c3a4833 100644 --- a/Tests/TemplatesTests/StringsTests.swift +++ b/Tests/TemplatesTests/StringsTests.swift @@ -9,7 +9,7 @@ import XCTest class StringsTests: XCTestCase { enum Contexts { - static let all = ["empty", "localizable"] + static let all = ["empty", "localizable", "multiple"] } // generate variations to test customname generation diff --git a/templates/strings/flat-swift2.stencil b/templates/strings/flat-swift2.stencil index f6f4aa5..310571c 100644 --- a/templates/strings/flat-swift2.stencil +++ b/templates/strings/flat-swift2.stencil @@ -1,61 +1,49 @@ // Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen -{% if tables.first.levels %} +{% if tables.count > 0 %} import Foundation // swiftlint:disable file_length -{% macro recursiveBlock item %} - {% for string in item.strings %} - {% if not param.noComments %} - /// {{string.translation}} - {% endif %} - case {{string.key|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}}{% if string.types %}({{string.types|join:", "}}){% endif %} - {% endfor %} - {% for child in item.children %} - {% call recursiveBlock child %} - {% endfor %} +{% macro parametersBlock types %}{% for type in types %}p{{forloop.counter}}: {{type}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} +{% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} +{% macro recursiveBlock table item sp %} +{{sp}} {% for string in item.strings %} +{{sp}} {% if not param.noComments %} +{{sp}} /// {{string.translation}} +{{sp}} {% endif %} +{{sp}} {% if string.types %} +{{sp}} static func {{string.key|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String { +{{sp}} return {{enumName}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %}) +{{sp}} } +{{sp}} {% else %} +{{sp}} static let {{string.key|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}} = {{enumName}}.tr("{{table}}", "{{string.key}}") +{{sp}} {% endif %} +{{sp}} {% endfor %} +{{sp}} {% for child in item.children %} +{{sp}} {% call recursiveBlock table child sp %} +{{sp}} {% endfor %} {% endmacro %} // swiftlint:disable identifier_name line_length type_body_length {% set enumName %}{{param.enumName|default:"L10n"}}{% endset %} enum {{enumName}} { - {% call recursiveBlock tables.first.levels %} -} - -{% macro parametersBlock types %}{% for type in types %}let p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} -{% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} -{% macro recursiveBlock item %} - {% for string in item.strings %} - {% if string.types %} - case .{{string.key|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}}({% call parametersBlock string.types %}): - return {{enumName}}.tr("{{string.key}}", {% call argumentsBlock string.types %}) - {% else %} - case .{{string.key|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}}: - return {{enumName}}.tr("{{string.key}}") - {% endif %} - {% endfor %} - {% for child in item.children %} - {% call recursiveBlock child %} - {% endfor %} -{% endmacro %} -extension {{enumName}}: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - {% call recursiveBlock tables.first.levels %} - } - } - - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") - return String(format: format, locale: NSLocale.currentLocale(), arguments: args) + {% if tables.count > 1 %} + {% for table in tables %} + enum {{table.name|swiftIdentifier|snakeToCamelCase}} { + {% call recursiveBlock table.name table.levels " " %} } + {% endfor %} + {% else %} + {% call recursiveBlock tables.first.name tables.first.levels "" %} + {% endif %} } // swiftlint:enable identifier_name line_length type_body_length -func tr(key: {{enumName}}) -> String { - return key.string +extension {{enumName}} { + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") + return String(format: format, locale: NSLocale.currentLocale(), arguments: args) + } } private final class BundleToken {} diff --git a/templates/strings/flat-swift3.stencil b/templates/strings/flat-swift3.stencil index f72e85b..a9cb406 100644 --- a/templates/strings/flat-swift3.stencil +++ b/templates/strings/flat-swift3.stencil @@ -1,61 +1,49 @@ // Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen -{% if tables.first.levels %} +{% if tables.count > 0 %} import Foundation // swiftlint:disable file_length -{% macro recursiveBlock item %} - {% for string in item.strings %} - {% if not param.noComments %} - /// {{string.translation}} - {% endif %} - case {{string.key|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}}{% if string.types %}({{string.types|join:", "}}){% endif %} - {% endfor %} - {% for child in item.children %} - {% call recursiveBlock child %} - {% endfor %} +{% macro parametersBlock types %}{% for type in types %}_ p{{forloop.counter}}: {{type}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} +{% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} +{% macro recursiveBlock table item sp %} +{{sp}} {% for string in item.strings %} +{{sp}} {% if not param.noComments %} +{{sp}} /// {{string.translation}} +{{sp}} {% endif %} +{{sp}} {% if string.types %} +{{sp}} static func {{string.key|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String { +{{sp}} return {{enumName}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %}) +{{sp}} } +{{sp}} {% else %} +{{sp}} static let {{string.key|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = {{enumName}}.tr("{{table}}", "{{string.key}}") +{{sp}} {% endif %} +{{sp}} {% endfor %} +{{sp}} {% for child in item.children %} +{{sp}} {% call recursiveBlock table child sp %} +{{sp}} {% endfor %} {% endmacro %} // swiftlint:disable identifier_name line_length type_body_length {% set enumName %}{{param.enumName|default:"L10n"}}{% endset %} enum {{enumName}} { - {% call recursiveBlock tables.first.levels %} -} - -{% macro parametersBlock types %}{% for type in types %}let p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} -{% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} -{% macro recursiveBlock item %} - {% for string in item.strings %} - {% if string.types %} - case .{{string.key|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}): - return {{enumName}}.tr(key: "{{string.key}}", {% call argumentsBlock string.types %}) - {% else %} - case .{{string.key|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}}: - return {{enumName}}.tr(key: "{{string.key}}") - {% endif %} - {% endfor %} - {% for child in item.children %} - {% call recursiveBlock child %} - {% endfor %} -{% endmacro %} -extension {{enumName}}: CustomStringConvertible { - var description: String { return self.string } - - var string: String { - switch self { - {% call recursiveBlock tables.first.levels %} - } - } - - private static func tr(key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") - return String(format: format, locale: Locale.current, arguments: args) + {% if tables.count > 1 %} + {% for table in tables %} + enum {{table.name|swiftIdentifier|snakeToCamelCase}} { + {% call recursiveBlock table.name table.levels " " %} } + {% endfor %} + {% else %} + {% call recursiveBlock tables.first.name tables.first.levels "" %} + {% endif %} } // swiftlint:enable identifier_name line_length type_body_length -func tr(_ key: {{enumName}}) -> String { - return key.string +extension {{enumName}} { + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") + return String(format: format, locale: Locale.current, arguments: args) + } } private final class BundleToken {} diff --git a/templates/strings/structured-swift2.stencil b/templates/strings/structured-swift2.stencil index 1b202b1..0fc37d9 100644 --- a/templates/strings/structured-swift2.stencil +++ b/templates/strings/structured-swift2.stencil @@ -1,29 +1,29 @@ // Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen -{% if tables.first.levels %} +{% if tables.count > 0 %} import Foundation // swiftlint:disable file_length {% macro parametersBlock types %}{% for type in types %}p{{forloop.counter}}: {{type}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} {% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} -{% macro recursiveBlock item sp %} +{% macro recursiveBlock table item sp %} {{sp}} {% for string in item.strings %} {{sp}} {% if not param.noComments %} {{sp}} /// {{string.translation}} {{sp}} {% endif %} {{sp}} {% if string.types %} {{sp}} static func {{string.name|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String { -{{sp}} return {{enumName}}.tr("{{string.key}}", {% call argumentsBlock string.types %}) +{{sp}} return {{enumName}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %}) {{sp}} } {{sp}} {% else %} -{{sp}} static let {{string.name|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}} = {{enumName}}.tr("{{string.key}}") +{{sp}} static let {{string.name|swiftIdentifier|snakeToCamelCase|escapeReservedKeywords}} = {{enumName}}.tr("{{table}}", "{{string.key}}") {{sp}} {% endif %} {{sp}} {% endfor %} {{sp}} {% for child in item.children %} {{sp}} enum {{child.name|swiftIdentifier|snakeToCamelCase}} { {{sp}} {% set sp2 %}{{sp}} {% endset %} -{{sp}} {% call recursiveBlock child sp2 %} +{{sp}} {% call recursiveBlock table child sp2 %} {{sp}} } {{sp}} {% endfor %} {% endmacro %} @@ -31,13 +31,21 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name {% set enumName %}{{param.enumName|default:"L10n"}}{% endset %} enum {{enumName}} { - {% call recursiveBlock tables.first.levels sp %} + {% if tables.count > 1 %} + {% for table in tables %} + enum {{table.name|swiftIdentifier|snakeToCamelCase}} { + {% call recursiveBlock table.name table.levels " " %} + } + {% endfor %} + {% else %} + {% call recursiveBlock tables.first.name tables.first.levels "" %} + {% endif %} } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension {{enumName}} { - private static func tr(key: String, _ args: CVarArgType...) -> String { - let format = NSLocalizedString(key, bundle: NSBundle(forClass: BundleToken.self), comment: "") + private static func tr(table: String, _ key: String, _ args: CVarArgType...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: NSBundle(forClass: BundleToken.self), comment: "") return String(format: format, locale: NSLocale.currentLocale(), arguments: args) } } diff --git a/templates/strings/structured-swift3.stencil b/templates/strings/structured-swift3.stencil index 926b37f..def9f25 100644 --- a/templates/strings/structured-swift3.stencil +++ b/templates/strings/structured-swift3.stencil @@ -1,29 +1,29 @@ // Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen -{% if tables.first.levels %} +{% if tables.count > 0 %} import Foundation // swiftlint:disable file_length {% macro parametersBlock types %}{% for type in types %}_ p{{forloop.counter}}: {{type}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} {% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %} -{% macro recursiveBlock item sp %} +{% macro recursiveBlock table item sp %} {{sp}} {% for string in item.strings %} {{sp}} {% if not param.noComments %} {{sp}} /// {{string.translation}} {{sp}} {% endif %} {{sp}} {% if string.types %} {{sp}} static func {{string.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String { -{{sp}} return {{enumName}}.tr("{{string.key}}", {% call argumentsBlock string.types %}) +{{sp}} return {{enumName}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %}) {{sp}} } {{sp}} {% else %} -{{sp}} static let {{string.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = {{enumName}}.tr("{{string.key}}") +{{sp}} static let {{string.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = {{enumName}}.tr("{{table}}", "{{string.key}}") {{sp}} {% endif %} {{sp}} {% endfor %} {{sp}} {% for child in item.children %} {{sp}} enum {{child.name|swiftIdentifier|snakeToCamelCase}} { {{sp}} {% set sp2 %}{{sp}} {% endset %} -{{sp}} {% call recursiveBlock child sp2 %} +{{sp}} {% call recursiveBlock table child sp2 %} {{sp}} } {{sp}} {% endfor %} {% endmacro %} @@ -31,13 +31,21 @@ import Foundation // swiftlint:disable explicit_type_interface identifier_name line_length nesting type_body_length type_name {% set enumName %}{{param.enumName|default:"L10n"}}{% endset %} enum {{enumName}} { - {% call recursiveBlock tables.first.levels sp %} + {% if tables.count > 1 %} + {% for table in tables %} + enum {{table.name|swiftIdentifier|snakeToCamelCase}} { + {% call recursiveBlock table.name table.levels " " %} + } + {% endfor %} + {% else %} + {% call recursiveBlock tables.first.name tables.first.levels "" %} + {% endif %} } // swiftlint:enable explicit_type_interface identifier_name line_length nesting type_body_length type_name extension {{enumName}} { - fileprivate static func tr(_ key: String, _ args: CVarArg...) -> String { - let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "") + fileprivate static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String { + let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "") return String(format: format, locale: Locale.current, arguments: args) } }