From 60af2ef07963aa73c12042b4d0806067e47c6fed Mon Sep 17 00:00:00 2001 From: Guillaume Louel Date: Sun, 11 Nov 2018 16:36:01 +0100 Subject: [PATCH 1/2] Beta 2 with support for community translations in /locale folder in cache --- .../PreferencesWindowController.swift | 4 +- .../Models/Cache/PoiStringProvider.swift | 70 +++++++++++++++---- Aerial/Source/Models/ManifestLoader.swift | 8 ++- Resources/PreferencesWindow.xib | 34 +++++---- 4 files changed, 87 insertions(+), 29 deletions(-) diff --git a/Aerial/Source/Controllers/PreferencesWindowController.swift b/Aerial/Source/Controllers/PreferencesWindowController.swift index 2667522c..250733fd 100644 --- a/Aerial/Source/Controllers/PreferencesWindowController.swift +++ b/Aerial/Source/Controllers/PreferencesWindowController.swift @@ -179,6 +179,8 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource, @IBOutlet weak var cacheSizeTextField: NSTextField! @IBOutlet var newVideosModePopup: NSPopUpButton! + @IBOutlet var lastCheckedVideosLabel: NSTextField! + var player: AVPlayer = AVPlayer() var videos: [AerialVideo]? @@ -509,7 +511,7 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource, extraCornerPopup.selectItem(at: preferences.extraCorner!) newVideosModePopup.selectItem(at: preferences.newVideosMode!) - + lastCheckedVideosLabel.stringValue = "Last checked on " + preferences.lastVideoCheck! colorizeProjectPageLinks() if let cacheDirectory = VideoCache.cacheDirectory { diff --git a/Aerial/Source/Models/Cache/PoiStringProvider.swift b/Aerial/Source/Models/Cache/PoiStringProvider.swift index 83883312..91cff838 100644 --- a/Aerial/Source/Models/Cache/PoiStringProvider.swift +++ b/Aerial/Source/Models/Cache/PoiStringProvider.swift @@ -29,7 +29,7 @@ class PoiStringProvider { var stringDict: NSDictionary? var communityStrings = [CommunityStrings]() - + var communityLanguage = "" // MARK: - Lifecycle init() { debugLog("Poi Strings Provider initialized") @@ -86,7 +86,7 @@ class PoiStringProvider { let locale: NSLocale = NSLocale(localeIdentifier: Locale.preferredLanguages[0]) if #available(OSX 10.12, *) { - if preferences.localizeDescriptions && locale.languageCode != "en" { + if preferences.localizeDescriptions && locale.languageCode != communityLanguage { return stringBundle!.localizedString(forKey: key, value: "", table: "Localizable.nocache") } } @@ -121,7 +121,7 @@ class PoiStringProvider { let locale: NSLocale = NSLocale(localeIdentifier: Locale.preferredLanguages[0]) if #available(OSX 10.12, *) { - if preferences.localizeDescriptions && locale.languageCode != "en" { + if preferences.localizeDescriptions && locale.languageCode != communityLanguage { return video.poi } } @@ -135,20 +135,60 @@ class PoiStringProvider { // MARK: - Community data - // Load the community strings - private func loadCommunity() { + private func getCommunityPathForLocale() -> String { let preferences = Preferences.sharedInstance + let locale: NSLocale = NSLocale(localeIdentifier: Locale.preferredLanguages[0]) - var bundlePath: String - if preferences.localizeDescriptions { - bundlePath = Bundle(for: PoiStringProvider.self).path(forResource: "en", ofType: "json")! - //bundlePath = Bundle.main.path(forResource: "en", ofType: "json")! - } else { - // TODO - bundlePath = Bundle(for: PoiStringProvider.self).path(forResource: "en", ofType: "json")! - //bundlePath = Bundle.main.path(forResource: "en", ofType: "json")! + if #available(OSX 10.12, *) { + // First we look in the Cache Folder for a locale directory + let cacheDirectory = VideoCache.cacheDirectory! + var cacheResourcesString = cacheDirectory + cacheResourcesString.append(contentsOf: "/locale") + let cacheUrl = URL(fileURLWithPath: cacheResourcesString) + + if cacheUrl.hasDirectoryPath { + debugLog("Aerial cache directory contains /locale") + var cc = locale.countryCode!.lowercased() + if !preferences.localizeDescriptions { + cc = "en" + } + debugLog("Looking for \(cc).json") + + let fileUrl = URL(fileURLWithPath: cacheResourcesString.appending("/\(cc).json")) + debugLog(fileUrl.absoluteString) + let fileManager = FileManager.default + if fileManager.fileExists(atPath: fileUrl.path) { + debugLog("Locale description found") + communityLanguage = cc + return fileUrl.path + } else { + debugLog("Locale description not found") + } + } + debugLog("Defaulting to bundle") + let cc = locale.countryCode!.lowercased() + if preferences.localizeDescriptions { + let path = Bundle(for: PoiStringProvider.self).path(forResource: cc, ofType: "json") + if path != nil { + let fileManager = FileManager.default + if fileManager.fileExists(atPath: path!) { + communityLanguage = cc + return path! + } + } + } } + + // Fallback to english in bundle + communityLanguage = "en" + return Bundle(for: PoiStringProvider.self).path(forResource: "en", ofType: "json")! + } + + // Load the community strings + private func loadCommunity() { + let bundlePath = getCommunityPathForLocale() debugLog("path : \(bundlePath)") + do { let data = try Data(contentsOf: URL(fileURLWithPath: bundlePath), options: .mappedIfSafe) let batches = try JSONSerialization.jsonObject(with: data, options: .allowFragments) @@ -168,9 +208,9 @@ class PoiStringProvider { } } catch { // handle error - errorLog("Community JSON ERROR") + errorLog("Community JSON ERROR : \(error)") } - debugLog("Community JSON : \(communityStrings.count)") + debugLog("Community JSON : \(communityStrings.count) entries") } func getCommunityName(id: String) -> String? { diff --git a/Aerial/Source/Models/ManifestLoader.swift b/Aerial/Source/Models/ManifestLoader.swift index 8d956fcb..c6414637 100644 --- a/Aerial/Source/Models/ManifestLoader.swift +++ b/Aerial/Source/Models/ManifestLoader.swift @@ -254,6 +254,11 @@ class ManifestLoader { } else { // Ok then, we fetch them... debugLog("Fetching missing manifests online") + let dateFormatter = DateFormatter() + let current = Date() + dateFormatter.dateFormat = "yyyy-MM-dd" + preferences.lastVideoCheck = dateFormatter.string(from: current) + let downloadManager = DownloadManager() var urls: [URL] = [] @@ -322,7 +327,8 @@ class ManifestLoader { // Fallback on earlier versions } - if Int((dateObj?.timeIntervalSinceNow)!) > dayCheck * 1440 { + debugLog("Interval : \(String(describing: dateObj?.timeIntervalSinceNow))") + if Int((dateObj?.timeIntervalSinceNow)!) < -dayCheck * 86400 { // We need to redownload debugLog("Checking for new videos") moveOldManifests() diff --git a/Resources/PreferencesWindow.xib b/Resources/PreferencesWindow.xib index 602d1bae..58ba01e3 100644 --- a/Resources/PreferencesWindow.xib +++ b/Resources/PreferencesWindow.xib @@ -58,6 +58,7 @@ + @@ -905,7 +906,7 @@ should appear - + @@ -1322,7 +1323,7 @@ Shift, but macOS 10.12.4 or above and a compatible Mac are required) - + @@ -1349,7 +1350,7 @@ Shift, but macOS 10.12.4 or above and a compatible Mac are required) - + @@ -1390,7 +1391,7 @@ Shift, but macOS 10.12.4 or above and a compatible Mac are required) - + - + @@ -1414,11 +1415,11 @@ Shift, but macOS 10.12.4 or above and a compatible Mac are required) - + - + @@ -1427,7 +1428,7 @@ Shift, but macOS 10.12.4 or above and a compatible Mac are required)