Class TxNative
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidfetchTranslations(String localeCode) Fetches the translations from CDS and updates the cache.static voidfetchTranslations(String localeCode, Set<String> tags) Fetches the translations from CDS and updates the cache.static ContextgeneralWrap(Context context) Deprecated.static voidinit(Context applicationContext, LocaleState locales, String token, String cdsHost, TxCache cache, MissingPolicy missingPolicy) Initialize the SDK.static booleanChecks if the SDK has been initialized by a previous call toinit(Context, LocaleState, String, String, TxCache, MissingPolicy).static voidsetSupportSpannable(boolean enabled) If enabled, thegetText()method can return aSpannedStringwhere all tags are parsed into spans.static voidsetTestMode(boolean enabled) When test mode is enabled, TransifexNative functionality is disabled: the translations provided by the SDK are not used.static ContextWraps a context to enable TransifexNative functionality inside activities, services or other scopes.static AppCompatDelegatewrapAppCompatDelegate(AppCompatDelegate delegate, Context baseContext) Wraps theAppCompatDelegateto enable TransifexNative functionality in an activity that extendsAppCompatActivity.
-
Field Details
-
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()orContextWrapper.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 tonull, the production host provided by Transifex is used.cache- The translation cache that holds the translations from the CDS;TxStandardCacheis used if set tonull.missingPolicy- Determines how to handle translations that are not available;SourceStringPolicyis used if set tonull.
-
isInitialized
public static boolean isInitialized()Checks if the SDK has been initialized by a previous call toinit(Context, LocaleState, String, String, TxCache, MissingPolicy).- Returns:
trueif the SDK has been initialized,falseotherwise.
-
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, thegetText()method can return aSpannedStringwhere all tags are parsed into spans. If disabled, aStringis 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:
In this case, you can use<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string> </resources>HtmlCompat.fromHtml(String, int)on the result ofgetText()orgetString()to get a SpannedString, which you can set to a view programmatically.- See Also:
-
- https://developer.android.com/guide/topics/resources/string-resource#StylingWithHTML
NativeCore.getSpannedString(String)
-
fetchTranslations
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
herefor the default cache implementation used by the TxNative SDK.- Parameters:
localeCode- An optional locale to fetch translations for; if set tonull, 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
Fetches the translations from CDS and updates the cache.- See Also:
-
wrap
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 ofgetApplicationContext()when using string methods in services.- Parameters:
context- The context to wrap.- Returns:
- The wrapped context.
-
generalWrap
Deprecated.Usewrap(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 theAppCompatDelegateto enable TransifexNative functionality in an activity that extendsAppCompatActivity.This method should be called in
AppCompatActivity.getDelegate().If your activity is extending
TxBaseAppCompatActivityyou don't have to call this method.- Parameters:
delegate- The activity's AppCompatDelegate.baseContext- The activity's base context.- Returns:
- The wrapped AppCompatDelegate.
-
wrap(Context)instead.