Quickstart: Transifex Native and Android
Note
You can start using Transifex Native by creating a Transifex Native project.
This is a quick start guide to get you through the whole Transifex Native experience on Android. Follow the steps below to start testing Transifex Native on your Android application. The SDK allows you to keep using the same string methods that Android provides, such as getString(int id)
, getText(int id)
, etc.
You can read all details on how the Android SDK works in this GitHub documentation.
Setup Android SDK
The Android Native SDK requires 18 (Android 4.3) and up, and is using appcompat 1.2.0.
To complete the setup you will need to:
- Include the SDK as a package dependency,
implementation 'com.transifex.txnative:txsdk:0.x.y'
and replace `x` and `y` with the latest version numbers - Configure the SDK in your Application class and replace the
<transifex_token>
with the one provided in your Transifex Native project.
Here is a configuration example:
@Override
public void onCreate() {
super.onCreate();
// Initialize TxNative
String token = "<transifex_token>";
LocaleState localeState = new LocaleState(getApplicationContext(),
"en", // source locale
new String[]{<supported_locales>}, // supported locales
null);
TxNative.init(
getApplicationContext(), // application context
localeState, // a LocaleState instance
token, // token
null, // cdsHost URL
null, // a TxCache implementation
null); // a MissingPolicy implementation
// Fetch all translations from CDS
TxNative.fetchTranslations(null);
}
You will need to replace <supported_locales>
with the languages you want your app displayed to. You can find all supported locales here.
If you are interested in setting up the SDK for your libraries or avoid interaction with 3rd party libs, you can look into the related section in the documentation.
Internationalize your code
The SDK is working on top of the existing string resource-related methods like getString()
, getText()
offering the additional features that Transifex Native has, such as OTA translations.
To use the SDK functionality for localization in more areas of your code, like inside a Service or excluding the SDK functionality check the detailed documentation.
Push source content to Content Delivery Service
To send your strings for translation you will need the CLI tool that collects the app content you specify and sends it to Transifex for translation. To perform this action you will first need to run the CLI tool as seen below, replacing /path/to/ with the folder you added the tool in.
java -jar /path/to/transifex.jar
Then use the push command providing the transifex_token and transifex_secret that you created in your Transifex Native project. Also replace app_module_name
with the name of your module where the strings.xml, hosting your content that needs to be translated, resides.
transifex push -t <transifex_token> -s <transifex_secret> -m <app_module_name>
You can read all the details about the CLI tool in the documentation.
Display translated content
By default, the Android Native SDK is using the translations already cached in the app, and the existing strings.xml files if no cached translations are found.
You can pre-populate the cache from your development environment using the CLI tool to perform a pull, like seen below, replacing <transifex_token>
with your Native Project token, <app_module_name>
with the name of your app module and finally <locale>
with the target languages you want to get translations for.
transifex pull -t <transifex_token> -m <app_module_name> -l <locale>
To get the latest translations for all locales in your app, you will need to use the fetchTranslations()
command of the SDK in your code. The new translations will be available the next time your app runs.
It is suggested that you call fetchTranslations()
when your app is starting to always have the latest translations available.
Worth noting is that you can use the SDK to create your own caching strategy. Read more on alternate caching in the documentation.
Check out the Android Native GitHub documentation for all the details on setting up and managing your Android application using Native SDK.