From 3b5350eced150715f984ec125782e63032833dcb Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Wed, 25 Jan 2023 10:23:01 -0800 Subject: [PATCH] modify language unit tests to work on all locales --- .../OmnivoreKit/Sources/Views/LocalText.swift | 2 +- .../Resources/en.lproj/Localizable.strings | 8 +++++ .../Resources/es.lproj/Localizable.strings | 8 +++++ .../zh-Hans.lproj/Localizable.strings | 8 +++++ .../Tests/ViewsTests/LocalTextTests.swift | 35 ++++++++++--------- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Views/LocalText.swift b/apple/OmnivoreKit/Sources/Views/LocalText.swift index 148be5252..b4382db02 100644 --- a/apple/OmnivoreKit/Sources/Views/LocalText.swift +++ b/apple/OmnivoreKit/Sources/Views/LocalText.swift @@ -1,7 +1,7 @@ import Foundation public enum LocalText { - public static func localText(key: String, comment: String? = nil) -> String { + private static func localText(key: String, comment: String? = nil) -> String { NSLocalizedString(key, bundle: .module, comment: comment ?? "no comment provided by developer") } diff --git a/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings b/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings index daabd3fa9..25186c59c 100644 --- a/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings +++ b/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings @@ -1,3 +1,7 @@ +// Unit test Entry -- Do not remove this or add entries before this one. +// This allows us to check for syntax errors in this file with a unit test +"unitTestLeadingEntry" = "For testing purposes only."; + // share extension "saveArticleSavedState" = "Saved to Omnivore"; "saveArticleProcessingState" = "Saved to Omnivore"; @@ -190,3 +194,7 @@ "errorNetwork" = "We are having trouble connecting to the internet."; // TODO: search navigationTitle, toggle, section, button, Label + +// Unit test Entry -- Do not remove this or add entries after this one. +// This allows us to check for syntax errors in this file with a unit test +"unitTestTrailingEntry" = "For testing purposes only."; diff --git a/apple/OmnivoreKit/Sources/Views/Resources/es.lproj/Localizable.strings b/apple/OmnivoreKit/Sources/Views/Resources/es.lproj/Localizable.strings index 211e0e6ca..8180cd519 100644 --- a/apple/OmnivoreKit/Sources/Views/Resources/es.lproj/Localizable.strings +++ b/apple/OmnivoreKit/Sources/Views/Resources/es.lproj/Localizable.strings @@ -1 +1,9 @@ +// Unit test Entry -- Do not remove this or add entries before this one. +// This allows us to check for syntax errors in this file with a unit test +"unitTestLeadingEntry" = "For testing purposes only."; + "googleSignInButton" = "Continuar con Google"; + +// Unit test Entry -- Do not remove this or add entries after this one. +// This allows us to check for syntax errors in this file with a unit test +"unitTestTrailingEntry" = "For testing purposes only."; diff --git a/apple/OmnivoreKit/Sources/Views/Resources/zh-Hans.lproj/Localizable.strings b/apple/OmnivoreKit/Sources/Views/Resources/zh-Hans.lproj/Localizable.strings index 6f62d6537..40169cd31 100644 --- a/apple/OmnivoreKit/Sources/Views/Resources/zh-Hans.lproj/Localizable.strings +++ b/apple/OmnivoreKit/Sources/Views/Resources/zh-Hans.lproj/Localizable.strings @@ -1,3 +1,7 @@ +// Unit test Entry -- Do not remove this or add entries before this one. +// This allows us to check for syntax errors in this file with a unit test +"unitTestLeadingEntry" = "For testing purposes only."; + // share extension "saveArticleSavedState" = "正在保存到 Omnivore 中"; "saveArticleProcessingState" = "已经保存到 Omnivore 中"; @@ -198,3 +202,7 @@ // "username.validation.error.tooshort" = "用户名应至少包含 3 个英文字符."; // "username.validation.error.toolong" = "用户名不能超过 15 个英文字符."; // "saveArticleSavingState" = "保存中..."; + +// Unit test Entry -- Do not remove this or add entries after this one. +// This allows us to check for syntax errors in this file with a unit test +"unitTestTrailingEntry" = "For testing purposes only."; diff --git a/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift b/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift index dcd626dab..08800e54d 100644 --- a/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift +++ b/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift @@ -7,25 +7,28 @@ final class LocalTextTests: XCTestCase { // Testing the first and last entry in teh strings file is adequate for finding syntax errors. // If any entry is not proper than the key will be returned and the test will fail. - // English (test default) - XCTAssertNotEqual(LocalText.saveArticleSavedState, "saveArticleSavedState") - XCTAssertNotEqual(LocalText.errorNetwork, "errorNetwork") - - // Simple Chinese - XCTAssertNotEqual(simpleChineseText(key: "saveArticleSavedState"), "saveArticleSavedState") - XCTAssertNotEqual(simpleChineseText(key: "errorNetwork"), "errorNetwork") - } - - private func simpleChineseText(key: String) -> String { - guard - let bundlePath = Bundle.module.path(forResource: "zh-Hans", ofType: "lproj"), - let bundle = Bundle(path: bundlePath) - else { return key } - - return NSLocalizedString(key, bundle: bundle, comment: "") + for languageCode in LanguageCode.allCases { + XCTAssertNotEqual(languageCode.translation(key: "unitTestLeadingEntry"), "unitTestLeadingEntry") + XCTAssertNotEqual(languageCode.translation(key: "unitTestTrailingEntry"), "unitTestTrailingEntry") + } } static var allTests = [ ("testThatLocalTextFindsStrings", testThatLocalTextFindsStrings) ] } + +private enum LanguageCode: String, CaseIterable { + case english = "en" + case simpleChinese = "zh-Hans" + case spanish = "es" + + func translation(key: String) -> String { + guard + let bundlePath = Bundle.module.path(forResource: rawValue, ofType: "lproj"), + let bundle = Bundle(path: bundlePath) + else { return key } + + return NSLocalizedString(key, bundle: bundle, comment: "") + } +}