Class TxNative

java.lang.Object
com.transifex.txnative.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. The method should only be called once.

      Should be called in Application.onCreate() or ContextWrapper.attachBaseContext(Context).

      Parameters:
      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.
    • isInitialized

      public static boolean isInitialized()
      Checks if the SDK has been initialized by a previous call to init(Context, LocaleState, String, String, TxCache, MissingPolicy).
      Returns:
      true if the SDK has been initialized, false otherwise.
    • 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:

      
       <resources>
         <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
       </resources>
       
       
      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.

      Parameters:
      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 a context to enable TransifexNative functionality inside activities, services or other scopes.

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

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

      Parameters:
      context - The context to wrap.
      Returns:
      The wrapped context.
    • generalWrap

      @Deprecated public static Context generalWrap(@NonNull Context context)
      Deprecated.
      Use wrap(Context) instead.
      Wraps a context to enable TransifexNative functionality in services and other scopes besides activities.
      Returns:
      The wrapped context.
    • wrapAppCompatDelegate

      @NonNull public static AppCompatDelegate wrapAppCompatDelegate(@NonNull AppCompatDelegate delegate, @NonNull Context baseContext)
      Wraps the AppCompatDelegate to enable TransifexNative functionality in an activity that extends AppCompatActivity.

      This method should be called in AppCompatActivity.getDelegate().

      If your activity is extending TxBaseAppCompatActivity you don't have to call this method.

      Parameters:
      delegate - The activity's AppCompatDelegate.
      baseContext - The activity's base context.
      Returns:
      The wrapped AppCompatDelegate.