Class TxNative
- java.lang.Object
-
- com.transifex.txnative.TxNative
-
public class TxNative extends Object
The entry point of TransifexNative SDK.
-
-
Constructor Summary
Constructors Constructor Description TxNative()
-
Method Summary
Modifier and Type Method Description static void
fetchTranslations(String localeCode)
Fetches the translations from CDS and updates the cache.static void
fetchTranslations(String localeCode, Set<String> tags)
Fetches the translations from CDS and updates the cache.static Context
generalWrap(Context context)
Wraps a context to enable TransifexNative functionality in services and other scopes besides activities.static void
init(Context applicationContext, LocaleState locales, String token, String cdsHost, TxCache cache, MissingPolicy missingPolicy)
Initialize the SDK.static void
setSupportSpannable(boolean enabled)
If enabled, thegetText()
method can return aSpannedString
where all tags are parsed into spans.static void
setTestMode(boolean enabled)
When test mode is enabled, TransifexNative functionality is disabled: the translations provided by the SDK are not used.static Context
wrap(Context context)
Wraps the activity's base context to enable TransifexNative functionality inside activities.
-
-
-
Field Detail
-
TAG
public static final String TAG
-
-
Method Detail
-
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()
.- 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;TxStandardCache
is used if set tonull
.missingPolicy
- Determines how to handle translations that are not available;SourceStringPolicy
is used if set tonull
.
-
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 aSpannedString
where all tags are parsed into spans. If disabled, aString
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:
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
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 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
public static void fetchTranslations(@Nullable String localeCode)
Fetches the translations from CDS and updates the cache.- See Also:
fetchTranslations(String, Set)
-
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.
- Parameters:
context
- The activity context to wrap.- Returns:
- 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 ofgetApplicationContext()
when using string methods in services.Check out the installation guide regarding the usage of this method.
- Parameters:
context
- The service context to wrap.- Returns:
- The wrapped context.
-
-