[Enhancement] String localisation #21

Merged
javier merged 6 commits from enhancement/localisable-strings into main 2023-05-19 16:58:51 +00:00
4 changed files with 116 additions and 0 deletions
Showing only changes of commit 9913c73174 - Show all commits

View 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
}

View 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"
}
}

View File

@ -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)
}
}
}

View 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.";