TXNative
public final class TXNative : NSObject
A static class that is the main point of entry for all the functionality of Transifex Native throughout the SDK.
-
The filename of the file that holds the translated strings and it’s bundled inside the app.
Declaration
Swift
public static let STRINGS_FILENAME: String -
XML name to be used for the root XML element when the pluralization rule is not supported by CDS and has to be uploaded as XML instead of the ICU format.
Declaration
Swift
public static let CDS_XML_ROOT_TAG_NAME: String -
XML name to be used for the child XML elements when the pluralization rule is not supported by CDS and has to be uploaded as XML instead of the ICU format.
Declaration
Swift
public static let CDS_XML_TAG_NAME: String -
XML attribute to be used in the CDS_XML_TAG_NAME elements when the pluralization rule is not supported by CDS and has to be uploaded as XML instead of the ICU format.
Declaration
Swift
public static let CDS_XML_ID_ATTRIBUTE: String -
The available and current locales
Declaration
Swift
@objc public static var locales: TXLocaleState? { get } -
initialize(locales:token: secret: cdsHost: session: cache: missingPolicy: errorPolicy: renderingStrategy: filterTags: filterStatus: ) Designated initializer of the TXNative SDK.
Do not call initialize() twice without calling dispose() first to deconstruct the previous singleton instance.
Declaration
Swift
@objc public static func initialize( locales: TXLocaleState, token: String, secret: String? = nil, cdsHost: String? = nil, session: URLSession? = nil, cache: TXCache? = nil, missingPolicy: TXMissingPolicy? = nil, errorPolicy: TXErrorPolicy? = nil, renderingStrategy: TXRenderingStategy = .platform, filterTags: [String]? = nil, filterStatus: String? = nil )Parameters
localeskeeps track of the available and current locales
tokenthe Transifex token that can be used for retrieving translations from CDS
secretthe Transifex secret that can be used for pushing source strings to CDS
cdsHostthe host of the CDS service; defaults to a production CDS service hosted by Transifex
sessionOptional URLSession to be used for all the requests made to the CDS service. If no session is provided, an ephemeral URLSession with no cache will be created and used.
cacheholds the available translations in various locales. If nil (default) the internal cache mechanism will be activated, otherwise the provided cache will be used.
missingPolicydetermines how to handle translations that are not available
errorPolicydetermines how to handle exceptions when rendering a problematic translation (used for ICU rendering strategy)
renderingStrategydetermines which strategy to be used when rendering the final string; defaults to platform strategy
filterTagsAn optional list of tags so that only strings that have all of the given tags are fetched.
filterStatusAn optional status so that only strings matching translation status are fetched.
-
Designated initializer of the Transifex SDK using the platform rendering strategy and only the required fields (locale state and token).
For a more involved SDK initialization, you can use the
initialize(locales:token:secret:cdsHost:session:cache:missingPolicy:errorPolicy:renderingStrategy:)method.Declaration
Swift
@objc public static func initialize( locales: TXLocaleState, token: String )Parameters
localeskeeps track of the available and current locales
tokenthe Transifex token that can be used for retrieving translations from CDS
-
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.moduleas the argument:TXNative.activate(bundle: .module)Make sure that this method is called after the SDK has been initialized.
Declaration
Swift
@objc public static func activate(bundle: Bundle)Parameters
bundlethe bundle to be activated. Pass
.bundlewhen calling this method from a Swift package. -
Deactivates swizzling for the Bundle previously passed in the
activate(bundle:)method.Declaration
Swift
@objc public static func deactivate(bundle: Bundle)Parameters
bundlethe bundle to be deactivated.
-
Return the translation of the given source string on a certain locale.
Declaration
Swift
@objc public static func translate(sourceString: String, localeCode: String? = nil, params: [String: Any], context: String? ) -> String?Parameters
sourceStringthe string in the source locale
localeCodean optional locale to translate to; defaults to the current app locale
paramsa dictionary with optional parameters to use for rendering a string e.g. variable placeholders, character limit, etc
contextan optional context that describes the source string (comma separated strings)
Return Value
the final string to display to the user
-
Helper method used when translation is not possible (e.g. in SwiftUI views).
This method applies the translation using the currently selected locale. For pluralization use the
localizedString(format:arguments:)method.Make sure that this method is called after the SDK has been initialized, otherwise “
” string will be shown instead. Declaration
Swift
public static func t(_ sourceString: String) -> StringParameters
sourceStringThe source string to be translated
Return Value
The translated string
-
Used by the Swift localizedString(format:arguments:) methods found in the TXExtensions.swift file.
Declaration
Swift
public static func localizedString(format: String, arguments: [Any]) -> String? -
Fetches the translations from CDS.
Declaration
Swift
@objc public static func fetchTranslations(_ localeCode: String? = nil, tags: [String]? = nil, status: String? = nil, completionHandler: TXPullCompletionHandler? = nil)Parameters
localeCodeIf not provided, it will fetch translations for all locales defined in the app configuration.
tagsAn optional list of tags so that only strings that have all of the given tags are fetched.
statusAn optional status so that only strings matching translation status are fetched.
completionHandlerThe completion handler that informs the caller when the operation is complete, reporting the new translations and a list of possible errors that might have occured. The completion handler is called from a background thread.
-
Pushes the base translations to CDS.
Declaration
Swift
@objc public static func pushTranslations(_ translations: [TXSourceString], configuration: TXPushConfiguration = TXPushConfiguration(), completionHandler: @escaping (Bool, [Error], [Error]) -> Void)Parameters
translationsA list of
TXSourceStringobjects.configurationA configuration object containing all the options that will be used alongside the push operation (see
TXPushConfiguration).completionHandlerA callback to be called when the push operation is complete with a boolean argument that informs the caller that the operation was successful (true) or not (false) and an array that may or may not contain any errors produced during the push operation and an array of non-blocking errors (warnings) that may have been generated during the push procedure. The completion handler is called from a background thread.
-
Forces CDS cache invalidation.
Declaration
Swift
@objc public static func forceCacheInvalidation(completionHandler: @escaping (Bool) -> Void)Parameters
completionHandlerA callback to be called when force cache invalidation is complete with a boolean argument that informs the caller that the operation was successful (true) or not (false). The completion handler is called from a background thread.
-
Destructs the TXNative singleton instance so that another one can be used. Reverts swizzled classes and methods.
Declaration
Swift
@objc public static func dispose()
View on GitHub