Skip to content

Commit

Permalink
Merge pull request #45 from stelabouras/stelios/update/swift-modules-…
Browse files Browse the repository at this point in the history
…limitations

Swift Modules update
  • Loading branch information
Nikos Vasileiou authored Nov 28, 2022
2 parents a63243c + 1d96b37 commit e983af3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@
- When rendering a translation the logic now first uses the original source
string as a key to look up to the cache and falls back to the generated hash
key if the entry is not found.

## Transifex iOS SDK 1.0.2

*November 28, 2022*

- Adds method to activate SDK from a Swift Package.
- Adds reference to SwiftUI limitation in README.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ Description strings) that are included in the `InfoPList.strings` file.
* Localized entried found in the `Root.plist` of the `Settings.bundle` of an app that
exposes its Settings to the iOS Settings app that are included in the `Root.strings` file.

### SwiftUI

The SDK does not currently support SwiftUI views.

### ICU support

Also, currently SDK supports only supports the platform rendering strategy, so if the ICU
Expand Down
26 changes: 25 additions & 1 deletion Sources/Transifex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ render '\(stringToRender)' locale code: \(localeCode) params: \(params). Error:
/// A static class that is the main point of entry for all the functionality of Transifex Native throughout the SDK.
public final class TXNative : NSObject {
/// The SDK version
internal static let version = "1.0.1"
internal static let version = "1.0.2"

/// The filename of the file that holds the translated strings and it's bundled inside the app.
public static let STRINGS_FILENAME = "txstrings.json"
Expand Down Expand Up @@ -430,6 +430,30 @@ token: \(token)
renderingStrategy: .platform)
}

/// Activate the SDK for a certain Bundle. Use this method to activate the SDK for a Swift package in
/// case multiple Swift packages are used as modules for an application.
///
/// Only call this method from each module, and not from the main application, by passing the
/// `Bundle.module` as the argument:
///
/// ```swift
/// TXNative.activate(bundle: .module)
/// ```
///
/// Make sure that this method is called after the SDK has been initialized.
///
/// - Parameter bundle: the bundle to be activated. Pass `.bundle` when calling this method
/// from a Swift package.
@objc
public static func activate(bundle: Bundle) {
guard tx != nil else {
Logger.error("Transifex Native is not initialized")
return
}

Swizzler.activate(bundles: [bundle])
}

/// Return the translation of the given source string on a certain locale.
///
/// - Parameters:
Expand Down
22 changes: 13 additions & 9 deletions Sources/Transifex/Swizzler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ import TransifexObjCRuntime
/// Swizzles all localizedString() calls made either by Storyboard files or by the use of NSLocalizedString()
/// function in code.
class SwizzledBundle : Bundle {
/// By setting a static method we are ensuring that this logic will only be executed one (much like the
/// dispatch_once in ObjC).
public static let activate: () = {
allBundles.forEach({ (bundle) in
object_setClass(bundle, SwizzledBundle.self)
})
}()

override func localizedString(forKey key: String,
value: String?,
table tableName: String?) -> String {
Expand Down Expand Up @@ -64,7 +56,7 @@ class Swizzler {

self.translationProvider = translationProvider

SwizzledBundle.activate
activate(bundles: Bundle.allBundles)

TXNativeObjcSwizzler.activate {
return self.localizedString(format: $0,
Expand All @@ -74,6 +66,18 @@ class Swizzler {
activated = true
}

/// Swizzles the passed bundles so that their localization methods are intercepted.
///
/// - Parameter bundles: The Bundle that will be swizzled
internal static func activate(bundles: [Bundle]) {
bundles.forEach({ (bundle) in
guard !bundle.isKind(of: SwizzledBundle.self) else {
return
}
object_setClass(bundle, SwizzledBundle.self)
})
}

/// Centralized method that all swizzled or overriden localizedStringWithFormat: methods will call once
/// Swizzler is activated.
///
Expand Down

0 comments on commit e983af3

Please sign in to comment.