From 60075432c0c3d4c459a6b03c19d50c0107f0a587 Mon Sep 17 00:00:00 2001 From: PanchamiShenoy Date: Thu, 27 Oct 2022 15:31:41 +0530 Subject: [PATCH] [CM -866] Expand localizable with tableName --- .../Foundation/String+Localizable.swift | 8 +++--- Sources/YCoreUI/Protocols/Localizable.swift | 10 ++++++- .../Assets/Strings/en.lproj/Settings.strings | 11 ++++++++ .../Foundation/String+LocalizedTests.swift | 26 ++++++++++++++++++- 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 Tests/YCoreUITests/Assets/Strings/en.lproj/Settings.strings diff --git a/Sources/YCoreUI/Extensions/Foundation/String+Localizable.swift b/Sources/YCoreUI/Extensions/Foundation/String+Localizable.swift index 576e140..92b85c5 100644 --- a/Sources/YCoreUI/Extensions/Foundation/String+Localizable.swift +++ b/Sources/YCoreUI/Extensions/Foundation/String+Localizable.swift @@ -10,9 +10,11 @@ import Foundation public extension String { /// Gets a localized string resource using the string's current value as key - /// - Parameter bundle: the bundle containing the localized string resource to use. Default = main app bundle. + /// - Parameters: + /// - bundle: the bundle containing the localized string resource to use. Default = main app bundle. + /// - tableName: the name of the `.strings` file containing the localized strings for this enum. /// - Returns: the localized string or else itself if it is not localized. - func localized(bundle: Bundle = .main) -> String { - NSLocalizedString(self, bundle: bundle, comment: self) + func localized(bundle: Bundle = .main, tableName: String? = nil) -> String { + NSLocalizedString(self, tableName: tableName, bundle: bundle, comment: self) } } diff --git a/Sources/YCoreUI/Protocols/Localizable.swift b/Sources/YCoreUI/Protocols/Localizable.swift index ef325b1..1f44705 100644 --- a/Sources/YCoreUI/Protocols/Localizable.swift +++ b/Sources/YCoreUI/Protocols/Localizable.swift @@ -15,6 +15,10 @@ public protocol Localizable: RawRepresentable where RawValue == String { /// A localized display string for this value var localized: String { get } + + /// The name of the `.strings` file containing the localized strings for this enum. + /// `nil` means use the default `Localizable.strings` file + static var tableName: String? { get } } extension Localizable { @@ -23,6 +27,10 @@ extension Localizable { /// A localized display string for this value public var localized: String { - rawValue.localized(bundle: Self.bundle) + rawValue.localized(bundle: Self.bundle, tableName: Self.tableName) } + + /// The name of the `.strings` file containing the localized strings for this enum. + /// Returns `nil` to use the default `Localizable.strings` file + static var tableName: String? { nil } } diff --git a/Tests/YCoreUITests/Assets/Strings/en.lproj/Settings.strings b/Tests/YCoreUITests/Assets/Strings/en.lproj/Settings.strings new file mode 100644 index 0000000..149c2e2 --- /dev/null +++ b/Tests/YCoreUITests/Assets/Strings/en.lproj/Settings.strings @@ -0,0 +1,11 @@ +/* + Settings.strings + YCoreUITests + + Created by Panchami Shenoy on 27/10/22. + Copyright © 2022 Y Media Labs. All rights reserved. +*/ + +"Settings_Title" = "Settings"; +"Settings_Font" = "Font"; +"Settings_Color" = "Color"; diff --git a/Tests/YCoreUITests/Extensions/Foundation/String+LocalizedTests.swift b/Tests/YCoreUITests/Extensions/Foundation/String+LocalizedTests.swift index 6c978a5..d9da0f7 100644 --- a/Tests/YCoreUITests/Extensions/Foundation/String+LocalizedTests.swift +++ b/Tests/YCoreUITests/Extensions/Foundation/String+LocalizedTests.swift @@ -25,6 +25,21 @@ final class StringLocalizedTests: XCTestCase { XCTAssertNotEqual($0.rawValue, string) } } + + func testTableName() { + XCTAssertNil(MainBundleConstants.tableName) + XCTAssertNil(StringConstants.tableName) + XCTAssertEqual(SettingConstants.tableName, "Settings") + + SettingConstants.allCases.forEach { + // Given a localized string constant + let string = $0.localized + // it should not be empty + XCTAssertFalse(string.isEmpty) + // and it should not equal its key + XCTAssertNotEqual($0.rawValue, string) + } + } } enum MainBundleConstants: String, Localizable { @@ -37,5 +52,14 @@ enum StringConstants: String, Localizable, CaseIterable { case pen = "Pen_Noun" case ambulance = "Ambulance_Noun" - public static var bundle: Bundle { .module } + static var bundle: Bundle { .module } +} + +enum SettingConstants: String, Localizable, CaseIterable { + case title = "Settings_Title" + case font = "Settings_Font" + case color = "Settings_Color" + + static var bundle: Bundle { .module } + static var tableName: String? { "Settings" } }