Skip to content

Commit 6007543

Browse files
PanchamiShenoyPanchamiShenoy
PanchamiShenoy
authored andcommitted
[CM -866] Expand localizable with tableName
1 parent 41aa357 commit 6007543

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

Diff for: Sources/YCoreUI/Extensions/Foundation/String+Localizable.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import Foundation
1010

1111
public extension String {
1212
/// Gets a localized string resource using the string's current value as key
13-
/// - Parameter bundle: the bundle containing the localized string resource to use. Default = main app bundle.
13+
/// - Parameters:
14+
/// - bundle: the bundle containing the localized string resource to use. Default = main app bundle.
15+
/// - tableName: the name of the `.strings` file containing the localized strings for this enum.
1416
/// - Returns: the localized string or else itself if it is not localized.
15-
func localized(bundle: Bundle = .main) -> String {
16-
NSLocalizedString(self, bundle: bundle, comment: self)
17+
func localized(bundle: Bundle = .main, tableName: String? = nil) -> String {
18+
NSLocalizedString(self, tableName: tableName, bundle: bundle, comment: self)
1719
}
1820
}

Diff for: Sources/YCoreUI/Protocols/Localizable.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public protocol Localizable: RawRepresentable where RawValue == String {
1515

1616
/// A localized display string for this value
1717
var localized: String { get }
18+
19+
/// The name of the `.strings` file containing the localized strings for this enum.
20+
/// `nil` means use the default `Localizable.strings` file
21+
static var tableName: String? { get }
1822
}
1923

2024
extension Localizable {
@@ -23,6 +27,10 @@ extension Localizable {
2327

2428
/// A localized display string for this value
2529
public var localized: String {
26-
rawValue.localized(bundle: Self.bundle)
30+
rawValue.localized(bundle: Self.bundle, tableName: Self.tableName)
2731
}
32+
33+
/// The name of the `.strings` file containing the localized strings for this enum.
34+
/// Returns `nil` to use the default `Localizable.strings` file
35+
static var tableName: String? { nil }
2836
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Settings.strings
3+
YCoreUITests
4+
5+
Created by Panchami Shenoy on 27/10/22.
6+
Copyright © 2022 Y Media Labs. All rights reserved.
7+
*/
8+
9+
"Settings_Title" = "Settings";
10+
"Settings_Font" = "Font";
11+
"Settings_Color" = "Color";

Diff for: Tests/YCoreUITests/Extensions/Foundation/String+LocalizedTests.swift

+25-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ final class StringLocalizedTests: XCTestCase {
2525
XCTAssertNotEqual($0.rawValue, string)
2626
}
2727
}
28+
29+
func testTableName() {
30+
XCTAssertNil(MainBundleConstants.tableName)
31+
XCTAssertNil(StringConstants.tableName)
32+
XCTAssertEqual(SettingConstants.tableName, "Settings")
33+
34+
SettingConstants.allCases.forEach {
35+
// Given a localized string constant
36+
let string = $0.localized
37+
// it should not be empty
38+
XCTAssertFalse(string.isEmpty)
39+
// and it should not equal its key
40+
XCTAssertNotEqual($0.rawValue, string)
41+
}
42+
}
2843
}
2944

3045
enum MainBundleConstants: String, Localizable {
@@ -37,5 +52,14 @@ enum StringConstants: String, Localizable, CaseIterable {
3752
case pen = "Pen_Noun"
3853
case ambulance = "Ambulance_Noun"
3954

40-
public static var bundle: Bundle { .module }
55+
static var bundle: Bundle { .module }
56+
}
57+
58+
enum SettingConstants: String, Localizable, CaseIterable {
59+
case title = "Settings_Title"
60+
case font = "Settings_Font"
61+
case color = "Settings_Color"
62+
63+
static var bundle: Bundle { .module }
64+
static var tableName: String? { "Settings" }
4165
}

0 commit comments

Comments
 (0)