ReadKeep/readeck/UI/Utils/URLUtil.swift
Ilyas Hallak db3cbf41d4 Fix URL label translation and formatting
- Add localization keys for "open_url" and "open_original_page" in EN/DE
- Create URLUtil.openUrlLabel() helper function for consistent formatting
- Replace incorrect string concatenation with proper localized labels
- Fix: "example.comopen" now displays as "Open example.com" (EN) or "example.com öffnen" (DE)
- Update BookmarkDetailLegacyView, BookmarkDetailView2, and BookmarkCardView
2025-10-30 21:14:40 +01:00

56 lines
2.1 KiB
Swift

import UIKit
import SafariServices
struct URLUtil {
static func open(url: String, urlOpener: UrlOpener = .inAppBrowser) {
// Could be extended to open in other browsers like Firefox, Brave etc. if somebody has a multi browser setup
// and wants readeck links to always opened in a specific browser
switch urlOpener {
case .defaultBrowser:
openUrlInDefaultBrowser(url: url)
default:
openUrlInInAppBrowser(url: url)
}
}
static func openUrlInDefaultBrowser(url: String) {
guard let url = URL(string: url) else { return }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
static func openUrlInInAppBrowser(url: String) {
guard let url = URL(string: url) else { return }
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first,
let rootViewController = window.rootViewController {
let safariViewController = SFSafariViewController(url: url)
safariViewController.preferredBarTintColor = UIColor.systemBackground
safariViewController.preferredControlTintColor = UIColor.tintColor
// Finde den präsentierenden View Controller
var presentingViewController = rootViewController
while let presented = presentingViewController.presentedViewController {
presentingViewController = presented
}
presentingViewController.present(safariViewController, animated: true)
}
}
static func extractDomain(from urlString: String) -> String? {
guard let url = URL(string: urlString), let host = url.host else { return nil }
return host.replacingOccurrences(of: "www.", with: "")
}
static func openUrlLabel(for urlString: String) -> String {
if let domain = extractDomain(from: urlString) {
return String(format: "open_url".localized, domain)
} else {
return "open_original_page".localized
}
}
}