Class TxNative
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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) Deprecated.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
Wraps a context to enable TransifexNative functionality inside activities, services or other scopes.static AppCompatDelegate
wrapAppCompatDelegate
(AppCompatDelegate delegate, Context baseContext) Wraps theAppCompatDelegate
to 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.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
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
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 activity context to wrap.- Returns:
- The wrapped context.
-
generalWrap
Deprecated.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.
-
wrapAppCompatDelegate
@NonNull public static AppCompatDelegate wrapAppCompatDelegate(@NonNull AppCompatDelegate delegate, @NonNull Context baseContext) Wraps theAppCompatDelegate
to enable TransifexNative functionality in an activity that extendsAppCompatActivity
.This method should be called in
AppCompatActivity.getDelegate()
.- Parameters:
delegate
- The activity's AppCompatDelegate.baseContext
- The activity's base context.- Returns:
- The wrapped AppCompatDelegate.
-