Skip to content

[CM -866] Expand localizable with tableName #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }
}