Class TxNative


public class TxNative extends Object
The entry point of TransifexNative SDK.
  • Field Details

    • TAG

      public static final String TAG
  • Constructor Details

    • TxNative

      public TxNative()
  • Method Details

    • init

      public static void init(@NonNull Context applicationContext, @NonNull LocaleState locales, @NonNull String token, @Nullable String cdsHost, @Nullable TxCache cache, @Nullable MissingPolicy missingPolicy)
      Initialize the SDK.

      Should be called in Application.onCreate().

      applicationContext - The application context.
      locales - Configures the locales supported by the SDK.
      token - The Transifex token that can be used for retrieving translations from CDS.
      cdsHost - An optional host for the Content Delivery Service; if set to null, the production host provided by Transifex is used.
      cache - The translation cache that holds the translations from the CDS; TxStandardCache is used if set to null.
      missingPolicy - Determines how to handle translations that are not available; SourceStringPolicy is used if set to null.
    • setTestMode

      public static void setTestMode(boolean enabled)
      When test mode is enabled, TransifexNative functionality is disabled: the translations provided by the SDK are not used. The original strings, as provided by Android's localization system, are returned after being prefixed with "test:".

      Test mode can be toggled multiple times while the app is running. The activity has to be recreated so that the strings are reloaded.

    • setSupportSpannable

      public static void setSupportSpannable(boolean enabled)
      If enabled, the getText() method can return a SpannedString where all tags are parsed into spans. If disabled, a String is returned at all times and all tags are kept as plain text. It's enabled by default.

      Leave it enabled, if you have strings that contain HTML tags using "<" and ">" characters and you want HTML styling to be applied when the strings are referenced in your layout.

      Disable it if your strings are HTML-escaped such as the following:

         <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
      In this case, you can use HtmlCompat.fromHtml(String, int) on the result of getText() or getString() to get a SpannedString, which you can set to a view programmatically.
      See Also:
    • fetchTranslations

      public static void fetchTranslations(@Nullable String localeCode, @Nullable Set<String> tags)
      Fetches the translations from CDS and updates the cache.

      The call returns instantly and fetches the translations asynchronously. If the translations are fetched successfully, the cache is updated.

      Note that updating the cache may or may not affect the translations shown in the app's UI. This depends on the cache's implementation. Read here for the default cache implementation used by the TxNative SDK.

      localeCode - An optional locale to fetch translations for; if set to null, it will fetch translations for all locales as defined in the SDK configuration.
      tags - An optional set of tags. If defined, only strings that have all of the given tags will be fetched.
    • fetchTranslations

      public static void fetchTranslations(@Nullable String localeCode)
      Fetches the translations from CDS and updates the cache.
      See Also:
    • wrap

      public static Context wrap(@NonNull Context context)
      Wraps the activity's base context to enable TransifexNative functionality inside activities.

      Check out the installation guide regarding the usage of this method.

      context - The activity context to wrap.
      The wrapped context.
    • generalWrap

      public static Context generalWrap(@NonNull Context context)
      Wraps a context to enable TransifexNative functionality in services and other scopes besides activities.

      Warning: You should use getBaseContext(), instead of getApplicationContext() when using string methods in services.

      Check out the installation guide regarding the usage of this method.

      context - The service context to wrap.
      The wrapped context.