Skip to content

Commit

Permalink
[CM -866] Expand localizable with tableName
Browse files Browse the repository at this point in the history
  • Loading branch information
PanchamiShenoy authored and PanchamiShenoy committed Oct 28, 2022
1 parent 41aa357 commit 6007543
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 9 additions & 1 deletion Sources/YCoreUI/Protocols/Localizable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
}
11 changes: 11 additions & 0 deletions Tests/YCoreUITests/Assets/Strings/en.lproj/Settings.strings
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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" }
}

0 comments on commit 6007543

Please sign in to comment.