From 9913c731743b7378bd36f3d07423245e68b4c289 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Fri, 19 May 2023 16:26:47 +0200 Subject: [PATCH] Implemented the "localisation(for:)" function of the Bundle+LocalisationBundle extension for the Core library. --- Sources/Core/Errors/BundleError.swift | 15 ++++++ .../Bundle+LocalisationBundle.swift | 38 ++++++++++++++ .../Bundle+LocalisationBundleTests.swift | 49 +++++++++++++++++++ .../Resources/en.lproj/Localizable.strings | 14 ++++++ 4 files changed, 116 insertions(+) create mode 100644 Sources/Core/Errors/BundleError.swift create mode 100644 Sources/Core/Extensions/Bundle+LocalisationBundle.swift create mode 100644 Tests/Core/Cases/Extensions/Bundle+LocalisationBundleTests.swift create mode 100644 Tests/Core/Resources/en.lproj/Localizable.strings diff --git a/Sources/Core/Errors/BundleError.swift b/Sources/Core/Errors/BundleError.swift new file mode 100644 index 0000000..ee4dc76 --- /dev/null +++ b/Sources/Core/Errors/BundleError.swift @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftLibs open source project +// +// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftLibs project authors +// +//===----------------------------------------------------------------------===// + +public enum BundleError: Error { + case bundleNotFound +} diff --git a/Sources/Core/Extensions/Bundle+LocalisationBundle.swift b/Sources/Core/Extensions/Bundle+LocalisationBundle.swift new file mode 100644 index 0000000..0b3b73a --- /dev/null +++ b/Sources/Core/Extensions/Bundle+LocalisationBundle.swift @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftLibs open source project +// +// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftLibs project authors +// +//===----------------------------------------------------------------------===// + +import Foundation + +public extension Bundle { + + // MARK: Functions + + func localisation(for languageCode: String) throws -> Bundle { + guard + let path = path(forResource: languageCode, ofType: .ResourceType.localisationBundle), + let bundle = Bundle(path: path) + else { + throw BundleError.bundleNotFound + } + + return bundle + } + +} + +// MARK: - String+Constants + +private extension String { + enum ResourceType { + static let localisationBundle = "lproj" + } +} diff --git a/Tests/Core/Cases/Extensions/Bundle+LocalisationBundleTests.swift b/Tests/Core/Cases/Extensions/Bundle+LocalisationBundleTests.swift new file mode 100644 index 0000000..dc9b8f5 --- /dev/null +++ b/Tests/Core/Cases/Extensions/Bundle+LocalisationBundleTests.swift @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftLibs open source project +// +// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftLibs project authors +// +//===----------------------------------------------------------------------===// + +import Core +import Foundation +import XCTest + +final class Bundle_LocalisationBundleTests: XCTestCase { + + // MARK: Properties + + private let bundle = Bundle.module + + private var languageCode: String! + + // MARK: Tests + + func test_localisation_withExistingLocalisationBundle() throws { + // GIVEN + languageCode = "en" + + // WHEN + let localisationBundle = try bundle.localisation(for: languageCode) + + // THEN + XCTAssertNotNil(localisationBundle) + XCTAssertEqual(localisationBundle.bundleURL.lastPathComponent, "en.lproj") + } + + func test_localisation_withNonExistingLocalisationBundle() throws { + // GIVEN + languageCode = "nl" + + // WHEN & THEN + XCTAssertThrowsError(try bundle.localisation(for: languageCode)) { error in + XCTAssertEqual(error as? BundleError, .bundleNotFound) + } + } + +} diff --git a/Tests/Core/Resources/en.lproj/Localizable.strings b/Tests/Core/Resources/en.lproj/Localizable.strings new file mode 100644 index 0000000..66ea830 --- /dev/null +++ b/Tests/Core/Resources/en.lproj/Localizable.strings @@ -0,0 +1,14 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftLibs open source project +// +// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftLibs project authors +// +//===----------------------------------------------------------------------===// + +"test.core.bundle.some-localisable-string" = "Some localisable string to use for testing purposes."; +"test.core.bundle.other-localisable-string" = "Other localisable string to use for testing purposes.";