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 }
  • 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

    locales

    keeps track of the available and current locales

    token

    the Transifex token that can be used for retrieving translations from CDS

    secret

    the Transifex secret that can be used for pushing source strings to CDS

    cdsHost

    the host of the CDS service; defaults to a production CDS service hosted by Transifex

    session

    Optional 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.

    cache

    holds the available translations in various locales. If nil (default) the internal cache mechanism will be activated, otherwise the provided cache will be used.

    missingPolicy

    determines how to handle translations that are not available

    errorPolicy

    determines how to handle exceptions when rendering a problematic translation (used for ICU rendering strategy)

    renderingStrategy

    determines which strategy to be used when rendering the final string; defaults to platform strategy

    filterTags

    An optional list of tags so that only strings that have all of the given tags are fetched.

    filterStatus

    An 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

    locales

    keeps track of the available and current locales

    token

    the 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.module as 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

    bundle

    the bundle to be activated. Pass .bundle when 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

    bundle

    the 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

    sourceString

    the string in the source locale

    localeCode

    an optional locale to translate to; defaults to the current app locale

    params

    a dictionary with optional parameters to use for rendering a string e.g. variable placeholders, character limit, etc

    context

    an 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) -> String

    Parameters

    sourceString

    The 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

    localeCode

    If not provided, it will fetch translations for all locales defined in the app configuration.

    tags

    An optional list of tags so that only strings that have all of the given tags are fetched.

    status

    An optional status so that only strings matching translation status are fetched.

    completionHandler

    The 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

    translations

    A list of TXSourceString objects.

    configuration

    A configuration object containing all the options that will be used alongside the push operation (see TXPushConfiguration).

    completionHandler

    A 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

    completionHandler

    A 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()