Skip to content

Commit

Permalink
feat: Xcstrings (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-londar authored Apr 30, 2024
1 parent b3ebfff commit 09ed0de
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import BaseAPI

typealias CrowdinAPIFileDataCompletion = ((Data?, String?, Error?) -> Void)
typealias CrowdinAPIStringsCompletion = (([String: String]?, String?, Error?) -> Void)
typealias CrowdinAPIPluralsCompletion = (([AnyHashable: Any]?, String?, Error?) -> Void)
typealias CrowdinAPIXliffCompletion = (([AnyHashable: Any]?, String?, Error?) -> Void)
Expand Down Expand Up @@ -87,6 +88,18 @@ class CrowdinContentDeliveryAPI: BaseAPI {
}
}

// MARK - Localization download methods:
func getFileData(filePath: String, etag: String?, timestamp: TimeInterval?, completion: @escaping CrowdinAPIFileDataCompletion) {
self.getFile(filePath: filePath, etag: etag, timestamp: timestamp) { (data, response, error) in
let etag = (response as? HTTPURLResponse)?.allHeaderFields[Strings.etag.rawValue] as? String
if let data {
completion(data, etag, nil)
} else {
completion(nil, etag, error)
}
}
}

func getPlurals(filePath: String, etag: String?, timestamp: TimeInterval?, completion: @escaping CrowdinAPIPluralsCompletion) {
self.getFile(filePath: filePath, etag: etag, timestamp: timestamp) { (data, response, error) in
let etag = (response as? HTTPURLResponse)?.allHeaderFields[Strings.etag.rawValue] as? String
Expand Down
16 changes: 16 additions & 0 deletions Sources/CrowdinSDK/CrowdinFileSystem/ReadWriteProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ extension CodableWrapper: ReadWriteProtocol {
return self.init(object: object)
}
}

extension Data: ReadWriteProtocol {
public func write(to path: String) {
do {
let url = URL(fileURLWithPath: path)
try Folder(path: url.deletingLastPathComponent().relativePath).create()
try self.write(to: url)
} catch {
print(error)
}
}

public static func read(from path: String) -> Data? {
try? Data(contentsOf: URL(fileURLWithPath: path))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class RURemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
let fileDownloader: RUFilesDownloader
let manifestManager: ManifestManager

init(localization: String, hash: String, projectId: String, organizationName: String?) {
init(localization: String, sourceLanguage: String, hash: String, projectId: String, organizationName: String?) {
self.localization = localization
self.hash = hash
manifestManager = ManifestManager.manifest(for: hash, organizationName: organizationName)
manifestManager = ManifestManager.manifest(for: hash, sourceLanguage: sourceLanguage, organizationName: organizationName)
self.fileDownloader = RUFilesDownloader(projectId: projectId, manifestManager: manifestManager, organizationName: organizationName)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RealtimeUpdateFeature: RealtimeUpdateFeatureProtocol {
return CrowdinSDK.currentLocalization ?? Bundle.main.preferredLanguage(with: localizations)
}
var hashString: String
let sourceLanguage: String
let organizationName: String?

var distributionResponse: DistributionsResponse? = nil
Expand All @@ -60,6 +61,7 @@ class RealtimeUpdateFeature: RealtimeUpdateFeatureProtocol {

required init(hash: String, sourceLanguage: String, organizationName: String?) {
self.hashString = hash
self.sourceLanguage = sourceLanguage
self.organizationName = organizationName
self.mappingManager = CrowdinMappingManager(hash: hash, sourceLanguage: sourceLanguage, organizationName: organizationName)
}
Expand Down Expand Up @@ -134,7 +136,7 @@ class RealtimeUpdateFeature: RealtimeUpdateFeatureProtocol {

func setupRealtimeUpdatesLocalizationProvider(with projectId: String, completion: @escaping () -> Void) {
oldProvider = Localization.current.provider
Localization.current.provider = LocalizationProvider(localization: self.localization, localStorage: RULocalLocalizationStorage(localization: self.localization), remoteStorage: RURemoteLocalizationStorage(localization: self.localization, hash: self.hashString, projectId: projectId, organizationName: self.organizationName))
Localization.current.provider = LocalizationProvider(localization: self.localization, localStorage: RULocalLocalizationStorage(localization: self.localization), remoteStorage: RURemoteLocalizationStorage(localization: self.localization, sourceLanguage: sourceLanguage, hash: self.hashString, projectId: projectId, organizationName: self.organizationName))

Localization.current.provider.refreshLocalization { [weak self] error in
guard let self = self else { return }
Expand Down Expand Up @@ -163,7 +165,7 @@ class RealtimeUpdateFeature: RealtimeUpdateFeatureProtocol {

func setupSocketManager(with projectId: String, projectWsHash: String, userId: String, wsUrl: String) {
// Download manifest if it is not initialized.
let manifestManager = ManifestManager.manifest(for: hashString, organizationName: organizationName)
let manifestManager = ManifestManager.manifest(for: hashString, sourceLanguage: sourceLanguage, organizationName: organizationName)
guard manifestManager.downloaded else {
manifestManager.download { [weak self] in
guard let self = self else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CrowdinRemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
self.localization = localization
self.hashString = config.hashString
self.organizationName = config.organizationName
self.manifestManager = ManifestManager.manifest(for: config.hashString, organizationName: config.organizationName)
self.manifestManager = ManifestManager.manifest(for: config.hashString, sourceLanguage: config.sourceLanguage, organizationName: config.organizationName)
self.crowdinDownloader = CrowdinLocalizationDownloader(manifestManager: manifestManager)
self.localizations = self.manifestManager.iOSLanguages
self.crowdinSupportedLanguages = CrowdinSupportedLanguages(organizationName: config.organizationName)
Expand Down Expand Up @@ -58,13 +58,13 @@ class CrowdinRemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
})
}

required init(localization: String, enterprise: Bool, organizationName: String?) {
required init(localization: String, sourceLanguage: String, organizationName: String?) {
self.localization = localization
guard let hashString = Bundle.main.crowdinDistributionHash else {
fatalError("Please add CrowdinDistributionHash key to your Info.plist file")
}
self.hashString = hashString
self.manifestManager = ManifestManager.manifest(for: hashString, organizationName: organizationName)
self.manifestManager = ManifestManager.manifest(for: hashString, sourceLanguage: sourceLanguage, organizationName: organizationName)
self.crowdinDownloader = CrowdinLocalizationDownloader(manifestManager: self.manifestManager)
self.localizations = []
self.crowdinSupportedLanguages = CrowdinSupportedLanguages(organizationName: organizationName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,31 @@ enum FileExtensions: String {
case stringsdict
case xliff
case json
case xcstrings

var `extension`: String {
return ".\(self.rawValue)"
}
}

extension String {
var isStrings: Bool {
return self.hasSuffix(FileExtensions.strings.rawValue)
hasSuffix(FileExtensions.strings.extension)
}

var isStringsDict: Bool {
return self.hasSuffix(FileExtensions.stringsdict.rawValue)
hasSuffix(FileExtensions.stringsdict.extension)
}

var isXliff: Bool {
return self.hasSuffix(FileExtensions.xliff.rawValue)
hasSuffix(FileExtensions.xliff.extension)
}

var isJson: Bool {
return self.hasSuffix(FileExtensions.json.rawValue)
hasSuffix(FileExtensions.json.extension)
}

var isXcstrings: Bool {
hasSuffix(FileExtensions.xcstrings.extension)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ class CrowdinLocalizationDownloader: CrowdinDownloaderProtocol {
let plurals = files.filter({ $0.isStringsDict })
let xliffs = files.filter({ $0.isXliff })
let jsons = files.filter({ $0.isJson })
self.download(strings: strings, plurals: plurals, xliffs:xliffs, jsons: jsons, with: hash, timestamp: timestamp, for: localization)
let xcstrings = files.filter({ $0.isXcstrings })
self.download(strings: strings, plurals: plurals, xliffs:xliffs, jsons: jsons, xcstrings: xcstrings, with: hash, timestamp: timestamp, for: localization)
} else if let error = error {
self.errors = [error]
self.completion?(nil, nil, self.errors)
}
}
}

func download(strings: [String], plurals: [String], xliffs: [String], jsons: [String], with hash: String, timestamp: TimeInterval?, for localization: String) {
func download(strings: [String], plurals: [String], xliffs: [String], jsons: [String], xcstrings: [String], with hash: String, timestamp: TimeInterval?, for localization: String) {
self.operationQueue.cancelAllOperations()

self.contentDeliveryAPI = CrowdinContentDeliveryAPI(hash: hash, session: URLSession.shared)
Expand Down Expand Up @@ -96,6 +97,23 @@ class CrowdinLocalizationDownloader: CrowdinDownloaderProtocol {
completionBlock.addDependency(download)
operationQueue.addOperation(download)
}

xcstrings.forEach { filePath in
let download = CrowdinXcstringsDownloadOperation(filePath: filePath,
localization: localization,
language: manifestManager.xcstringsLanguage,
timestamp: timestamp,
contentDeliveryAPI: contentDeliveryAPI)
download.completion = { [weak self] (strings, plurals, error) in
guard let self = self else { return }
self.add(strings: strings)
self.add(plurals: plurals)
self.add(error: error)
}
completionBlock.addDependency(download)
operationQueue.addOperation(download)
}

operationQueue.operations.forEach({ $0.qualityOfService = .userInitiated })
operationQueue.addOperation(completionBlock)
}
Expand Down
Loading

0 comments on commit 09ed0de

Please sign in to comment.