Implemented the "localisation(for:)" function of the Bundle+LocalisationBundle extension for the Core library.
This commit is contained in:
parent
5286f72f05
commit
9913c73174
15
Sources/Core/Errors/BundleError.swift
Normal file
15
Sources/Core/Errors/BundleError.swift
Normal file
@ -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
|
||||||
|
}
|
38
Sources/Core/Extensions/Bundle+LocalisationBundle.swift
Normal file
38
Sources/Core/Extensions/Bundle+LocalisationBundle.swift
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
Tests/Core/Resources/en.lproj/Localizable.strings
Normal file
14
Tests/Core/Resources/en.lproj/Localizable.strings
Normal file
@ -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.";
|
Loading…
x
Reference in New Issue
Block a user