Skip to content

Commit

Permalink
Added staticTranslationsFileURL
Browse files Browse the repository at this point in the history
  • Loading branch information
basvankuijck committed Oct 10, 2024
1 parent 5b71f4d commit 7c5617a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Prontalize iOS SDK

##1.0.2
- Added `staticTranslationsFileURL`

##1.0.1
- Removed `Text` from library

Expand Down
13 changes: 2 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
{
"pins" : [
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version" : "1.0.0"
}
},
{
"identity" : "viewinspector",
"kind" : "remoteSourceControl",
"location" : "https://github.com/nalexn/ViewInspector.git",
"state" : {
"revision" : "ac7df67c4e0593470eda90a550f8493a81609745",
"version" : "0.9.2"
"revision" : "5acfa0a3c095ac9ad050abe51c60d1831e8321da",
"version" : "0.10.0"
}
}
],
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:6.0
import PackageDescription

let package = Package(
Expand Down Expand Up @@ -31,5 +31,5 @@ let package = Package(
]
)
],
swiftLanguageVersions: [ .v5 ]
)
swiftLanguageModes: [ .v6 ]
)
32 changes: 22 additions & 10 deletions Sources/Prontalize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public class Prontalize: ObservableObject {
public class Prontalize: ObservableObject, @unchecked Sendable {
public static let instance = Prontalize()

private static let userDefaultUUIDKey = "prontalize-uuid"
Expand All @@ -30,6 +30,9 @@ public class Prontalize: ObservableObject {
}
}

/// Instead of using the prontalize API, point to the location of the json file
public var staticTranslationsFileURL: String?

/// The Bundle that points to the Prontalize resources, if no bundle is available. Use `fallbackBundle`
private(set) public var bundle: Bundle = Bundle.main

Expand Down Expand Up @@ -72,7 +75,7 @@ public class Prontalize: ObservableObject {
/// - apiToken: The API_TOKEN provided from prontalize.nl
/// - projectID: The PROJECT_ID provided from prontalize.nl (you can find this in the url of your project)
/// - fallbackBundle: If the prontalize bundle hasn't been loaded yet, fallback to this bundle to obtain resources from there (defaults to .main)
public func setup(apiToken: String, projectID: String, fallbackBundle: Bundle = Bundle.main) {
public func setup(apiToken: String = "", projectID: String = "", fallbackBundle: Bundle = Bundle.main) {
self.bundle = fallbackBundle
self.fallbackBundle = fallbackBundle
self.apiToken = apiToken
Expand Down Expand Up @@ -137,16 +140,25 @@ public class Prontalize: ObservableObject {
}

private func fetch() async throws {
precheck()
let urlString = "https://prontalize.nl/api/projects/\(projectID)/translation_keys"
guard let url = URL(string: urlString) else {
return
var urlRequest: URLRequest
if let staticTranslationsFileURL {
guard let url = URL(string: staticTranslationsFileURL) else {
return
}
urlRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)

} else {
precheck()
let urlString = "https://prontalize.nl/api/projects/\(projectID)/translation_keys"
guard let url = URL(string: urlString) else {
return
}

urlRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
urlRequest.setValue("Bearer \(apiToken)", forHTTPHeaderField: "Authorization")
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
}

var urlRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
urlRequest.setValue("Bearer \(apiToken)", forHTTPHeaderField: "Authorization")
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

log("Retrieving translations ...")
let (data, _) = try await URLSession.shared.data(for: urlRequest)
log("Success - Succesfully received translations from prontalize")
Expand Down
12 changes: 6 additions & 6 deletions TestSources/ProntalizeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import XCTest
import SwiftUI
import ViewInspector
@testable import Prontalize

Expand Down Expand Up @@ -81,21 +82,20 @@ class ProntalizeTests: XCTestCase {
}
let view = ProntalizedText("welcome")

let sut = try view.inspect()
let string1 = try sut.text().string()
var sut = try view.inspect().find(ViewType.Text.self)
let string1 = try sut.string()
XCTAssertEqual(string1, "Welcome")

let data = try Data(contentsOf: url)
try Prontalize.instance.decodeAndWrite(data: data)

let string2 = try sut.text().string()
sut = try view.inspect().find(ViewType.Text.self)
let string2 = try sut.string()
XCTAssertEqual(string2, "Welcome again")

let pluralView = ProntalizedText("apples", pluralCount: 3)

let string3 = try pluralView.inspect().text().string()
let string3 = try pluralView.inspect().find(ViewType.Text.self).string()
XCTAssertEqual(string3, "3 apples")
}
}

extension ProntalizedText: Inspectable { }

0 comments on commit 7c5617a

Please sign in to comment.