From 6e3c7a88706ff8f98dc7152d6455fccd82563020 Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Sun, 7 Jan 2024 22:51:20 +0100 Subject: [PATCH] :recycle: (l10n) Auto detect Bundle at call site See https://forums.swift.org/t/accessing-the-bundle-of-the-call-site/4634/9 --- Modules/LocalizationKit/Sources/LocalizationKit.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Modules/LocalizationKit/Sources/LocalizationKit.swift b/Modules/LocalizationKit/Sources/LocalizationKit.swift index 0682cae3d3..068a892da8 100644 --- a/Modules/LocalizationKit/Sources/LocalizationKit.swift +++ b/Modules/LocalizationKit/Sources/LocalizationKit.swift @@ -9,11 +9,17 @@ import Foundation public enum l10n { public static func LocalizedString( - _ key: StaticString, value: String.LocalizationValue, comment: StaticString + _ key: StaticString, value: String.LocalizationValue, bundle: Bundle? = nil, comment: StaticString, dsoHandle: UnsafeRawPointer = #dsohandle ) -> AttributedString { - let string = String(localized: key, defaultValue: value, comment: comment) + var dlInformation = dl_info() + let _ = dladdr(dsoHandle, &dlInformation) + let path = String(cString: dlInformation.dli_fname) + let url = URL(fileURLWithPath: path).deletingLastPathComponent() + let bundle = bundle ?? Bundle(url: url) + + let string = String(localized: key, defaultValue: value, bundle: bundle, comment: comment) let markdown = (try? AttributedString( markdown: string,