Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Include the dependency:
implementation 'com.transifex.txnative:txsdk:x.y.z'
```

Please replace `x`, `y` and `z` with the latest version numbers: [![Maven Central](https://img.shields.io/maven-central/v/com.transifex.txnative/txsdk?color=32c955)](https://maven-badges.herokuapp.com/maven-central/com.transifex.txnative/txsdk)
Please replace `x`, `y` and `z` with the latest version numbers: [![Maven Central](https://img.shields.io/maven-central/v/com.transifex.txnative/txsdk?color=32c955)](https://central.sonatype.com/artifact/com.transifex.txnative/txsdk)


The library's minimum supported SDK is 18 (Android 4.3).
Expand Down Expand Up @@ -57,19 +57,11 @@ public void onCreate() {
new String[]{"en", "el", "de", "fr", "ar", "sl", "es_ES", "es_MX"},
null);

TxNative.init(
// application context
getApplicationContext(),
// a LocaleState instance
localeState,
// token
token,
// cdsHost URL
null,
// a TxCache implementation
null,
// a MissingPolicy implementation
null);
TxNative.initializer(base, localeState, token)
.withCdsHost(null) // cdsHost URL
.withCache(null) // a TxCache implementation
.withMissingPolicy(null) // a MissingPolicy implementation
.init();

// Fetch all translations from CDS
TxNative.fetchTranslations(null, null);
Expand Down
5 changes: 4 additions & 1 deletion TransifexNativeSDK/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
buildConfig = true
}
}

// Uncomment in combination with "setCompatVectorFromResourcesEnabled()" in "MyApplication" to
Expand All @@ -50,7 +54,6 @@ android {
//}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "androidx.appcompat:appcompat:$versions.appcompat"
implementation "com.google.android.material:material:$versions.material"
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ protected void attachBaseContext(Context base) {
new String[]{"en", "el", "de", "fr", "ar", "sl"},
null);

TxNative.init(
base, // application context
localeState, // a LocaleState instance
token, // token
null, // cdsHost URL
null, // a TxCache implementation
null); // a MissingPolicy implementation
TxNative.initializer(base, localeState, token)
.withCdsHost(null) // cdsHost URL
.withCache(null) // a TxCache implementation
.withMissingPolicy(null) // a MissingPolicy implementation
.init();

// OPTIONAL:
// Wrap the application's base context to allow TxNative to intercept all string resource
Expand All @@ -64,7 +62,8 @@ protected void attachBaseContext(Context base) {
// strings translated.
super.attachBaseContext(TxNative.wrap(base));

// SAFER: Do not wrap the application's base context.
// SAFER: Do not wrap the application's base context. Just pass the base context as you
// would normally do.
// super.attachBaseContext(base);
}
}
6 changes: 3 additions & 3 deletions TransifexNativeSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ buildscript {
]
ext {
sdkVersionCode = 11 // version code for txsdk
sdkVersion = '1.4.0' // version for txsdk and common
sdkVersion = '1.4.1' // version for txsdk and common
pomGroupID = "com.transifex.txnative" // pom group id for txsdk and common

cliVersion = '1.4.0' // clitool version
cliVersion = '1.4.1' // clitool version
}
repositories {
google()
Expand All @@ -33,7 +33,7 @@ buildscript {

plugins {
id "io.github.gradle-nexus.publish-plugin" version "2.0.0"
id 'com.android.application' version '8.1.0' apply false
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

import javax.naming.TimeLimitExceededException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import picocli.CommandLine;
Expand Down Expand Up @@ -212,11 +211,11 @@ public Integer call() throws Exception {

// Push to CDS
CDSHandler cdsHandler = new CDSHandler(null, token, secret,
mainClass.hostURL);
mainClass.hostURL, null);
LocaleData.TxJobStatus jobStatus = null;
try {
jobStatus = cdsHandler.pushSourceStrings(postData);
} catch (TimeLimitExceededException e) {
} catch (TimeoutException e) {
System.out.println("Strings are queued for processing");
return 0;
}
Expand Down Expand Up @@ -266,11 +265,11 @@ public Integer call() throws Exception {

// Push to CDS
CDSHandler cdsHandler = new CDSHandler(null, token, secret,
mainClass.hostURL);
mainClass.hostURL, null);
LocaleData.TxJobStatus jobStatus = null;
try {
jobStatus = cdsHandler.pushSourceStrings(postData);
} catch (TimeLimitExceededException e) {
} catch (TimeoutException e) {
System.out.println("Source string clearing is queued for processing");
return 0;
}
Expand Down Expand Up @@ -359,7 +358,7 @@ public Integer call() throws Exception {

// Pull from CDS
CDSHandler cdsHandler = new CDSHandler(translatedLocales, token, null,
mainClass.hostURL);
mainClass.hostURL, null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);
HashMap<String, File> downloadedFiles = downloader.downloadTranslations(null, tags, outDir, OUT_FILE_NAME);

Expand Down Expand Up @@ -446,4 +445,4 @@ String getErrorString(@NonNull LocaleData.TxJobStatus jobStatus) {
private static @NonNull String getNonDownloadedLocalesString(@NonNull HashSet<String> nonDownloadedLocales) {
return "The translations for the following locales were not downloaded: " + Arrays.toString(nonDownloadedLocales.toArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.TimeLimitExceededException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -53,6 +52,9 @@ public class CDSHandler {
// The host of the Content Delivery Service
private final String mCdsHost;

// A custom authorization header key
private final String mCustomAuthorizationHeaderKey;

private final Gson mGson;

/**
Expand Down Expand Up @@ -100,18 +102,21 @@ void onTranslationFetched(@Nullable InputStream inputStream, @NonNull String loc
/**
* Creates a CDSHandler instance.
*
* @param localeCodes An array of locale codes that can be downloaded from CDS. The source
* locale can also be included.
* @param token The API token to use for connecting to the CDS.
* @param secret The API secret to use for connecting to the CDS.
* @param csdHost The host of the Content Delivery Service.
* @param localeCodes An array of locale codes that can be downloaded from CDS. The source
* locale can also be included.
* @param token The API token to use for connecting to the CDS.
* @param secret The API secret to use for connecting to the CDS.
* @param csdHost The host of the Content Delivery Service.
* @param customAuthorizationHeaderKey A custom HTTP header name used to pass the token to CDS.
*/
public CDSHandler(@Nullable String[] localeCodes,
@NonNull String token, @Nullable String secret, @NonNull String csdHost) {
@NonNull String token, @Nullable String secret, @NonNull String csdHost,
@Nullable String customAuthorizationHeaderKey) {
mLocaleCodes = localeCodes;
mToken = token;
mSecret = secret;
mCdsHost = csdHost;
mCustomAuthorizationHeaderKey = customAuthorizationHeaderKey;

mGson = new Gson();
}
Expand Down Expand Up @@ -345,11 +350,11 @@ public LocaleData.TranslationMap fetchTranslations(@Nullable String localeCode,
* The job status can be either <code>"completed"</code> or <code>"failed"</code>.
* If everything fails, <code>null</code> is returned.
*
* @throws TimeLimitExceededException When the server takes longer than 20 seconds to complete
* @throws TimeoutException When the server takes longer than 20 seconds to complete
* processing the job.
*/
public @Nullable
LocaleData.TxJobStatus pushSourceStrings(@NonNull LocaleData.TxPostData postData) throws TimeLimitExceededException {
LocaleData.TxJobStatus pushSourceStrings(@NonNull LocaleData.TxPostData postData) throws TimeoutException {
LocaleData.TxPostResponseData response = pushSourceStringsInternal(postData);
if (response == null) {
return null;
Expand Down Expand Up @@ -462,13 +467,13 @@ LocaleData.TxJobStatus pushSourceStrings(@NonNull LocaleData.TxPostData postData
* @return The job status object or <code>null</code> if everything failed. The job status can
* be either <code>"completed"</code> or <code>"failed"</code>.
*
* @throws TimeLimitExceededException When the server takes longer than 20 seconds to complete
* @throws TimeoutException When the server takes longer than 20 seconds to complete
* processing the job.
*
* @see <a href="https://github.com/transifex/transifex-delivery/#job-status">
* https://github.com/transifex/transifex-delivery/#job-status</a>
*/
private @Nullable LocaleData.TxJobStatus getJobStatus(@NonNull LocaleData.TxPostResponseData responseData) throws TimeLimitExceededException {
private @Nullable LocaleData.TxJobStatus getJobStatus(@NonNull LocaleData.TxPostResponseData responseData) throws TimeoutException {
URL url = null;
try {
URI cdsContentURI = new URI(mCdsHost);
Expand Down Expand Up @@ -531,7 +536,7 @@ LocaleData.TxJobStatus pushSourceStrings(@NonNull LocaleData.TxPostData postData
}
}

throw new TimeLimitExceededException("Server is still processing the pushed strings");
throw new TimeoutException("Server is still processing the pushed strings");
}

/**
Expand All @@ -548,11 +553,13 @@ private void addHeaders(@NonNull HttpURLConnection connection, boolean withSecre

connection.addRequestProperty("Content-type", "application/json; charset=utf-8");

String authorizationKey = (mCustomAuthorizationHeaderKey != null) ?
mCustomAuthorizationHeaderKey : "Authorization";
if (withSecret) {
connection.addRequestProperty("Authorization", "Bearer " + mToken + ":" + mSecret);
connection.addRequestProperty(authorizationKey, "Bearer " + mToken + ":" + mSecret);
}
else {
connection.addRequestProperty("Authorization", "Bearer " + mToken);
connection.addRequestProperty(authorizationKey, "Bearer " + mToken);
}

connection.addRequestProperty("x-native-sdk", "mobile/android/" + BuildProperties.getSDKVersion());
Expand Down
Loading
Loading