-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ErrorMessageTranslator and default implementation (#807)
**Summary** ErrorMessageTranslator is an interface that a user can implement to specify custom translations for server-provided errors. **Motivation** Stripe error messages are in English. In a few places, we show these messages on screen in dialogs. An app developer may wish to provide their own translation of these messages so that are legibile to their customers. Fixes #680
- Loading branch information
1 parent
8251321
commit ea40ee8
Showing
9 changed files
with
162 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
stripe/src/main/java/com/stripe/android/view/i18n/ErrorMessageTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.stripe.android.view.i18n; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.stripe.android.StripeError; | ||
|
||
public interface ErrorMessageTranslator { | ||
/** | ||
* See https://stripe.com/docs/api/errors for a list of error codes and associated messages | ||
* | ||
* @param httpCode The HTTP code associated with the error response. | ||
* @param errorMessage A human-readable message providing more details about the error. | ||
* For card errors, these messages can be shown to your users. | ||
* @param stripeError The {@link StripeError} that represents detailed information about the | ||
* error. Specifically, {@link StripeError#code} is useful for understanding | ||
* the underlying error (e.g. "payment_method_unactivated"). | ||
* | ||
* @return a non-null error message | ||
*/ | ||
@NonNull | ||
String translate(int httpCode, @Nullable String errorMessage, | ||
@Nullable StripeError stripeError); | ||
|
||
class Default implements ErrorMessageTranslator { | ||
@NonNull | ||
@Override | ||
public String translate(int httpCode, @Nullable String errorMessage, | ||
@Nullable StripeError stripeError) { | ||
return errorMessage == null ? "" : errorMessage; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
stripe/src/main/java/com/stripe/android/view/i18n/TranslatorManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.stripe.android.view.i18n; | ||
|
||
import android.app.Application; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* A class that provides a {@link ErrorMessageTranslator} for translating server-provided error | ||
* messages, as defined in https://stripe.com/docs/api/errors. | ||
* | ||
* See {@link com.stripe.android.view.PaymentMethodsActivity} for example usage. | ||
* | ||
* To use a custom {@link ErrorMessageTranslator} in your app, | ||
* override {@link Application#onCreate()} in your app's Application subclass and call | ||
* {@link #setErrorMessageTranslator(ErrorMessageTranslator)}. | ||
* | ||
* <pre> | ||
* <code> | ||
* public class MyApp extends Application { | ||
* public void onCreate() { | ||
* super.onCreate(); | ||
* TranslatorManager.setErrorMessageTranslator(new MyErrorMessageTranslator()); | ||
* } | ||
* } | ||
* </code> | ||
* </pre> | ||
*/ | ||
public class TranslatorManager { | ||
@NonNull | ||
private static final ErrorMessageTranslator DEFAULT_ERROR_MESSAGE_TRANSLATOR = | ||
new ErrorMessageTranslator.Default(); | ||
|
||
@Nullable | ||
private static ErrorMessageTranslator mErrorMessageTranslator; | ||
|
||
private TranslatorManager() { | ||
} | ||
|
||
@NonNull | ||
public static ErrorMessageTranslator getErrorMessageTranslator() { | ||
return mErrorMessageTranslator != null ? | ||
mErrorMessageTranslator : DEFAULT_ERROR_MESSAGE_TRANSLATOR; | ||
} | ||
|
||
public static void setErrorMessageTranslator( | ||
@Nullable ErrorMessageTranslator errorMessageTranslator) { | ||
mErrorMessageTranslator = errorMessageTranslator; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
stripe/src/test/java/com/stripe/android/view/i18n/TranslatorManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.stripe.android.view.i18n; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.stripe.android.StripeError; | ||
import com.stripe.android.StripeErrorFixtures; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TranslatorManagerTest { | ||
private static final StripeError STRIPE_ERROR = StripeErrorFixtures.INVALID_REQUEST_ERROR; | ||
|
||
@Before | ||
public void setup() { | ||
TranslatorManager.setErrorMessageTranslator(null); | ||
} | ||
|
||
@Test | ||
public void testDefaultErrorMessageTranslator() { | ||
assertEquals("error!", | ||
TranslatorManager.getErrorMessageTranslator() | ||
.translate(0, "error!", STRIPE_ERROR)); | ||
} | ||
|
||
@Test | ||
public void testCustomErrorMessageTranslator() { | ||
TranslatorManager.setErrorMessageTranslator(new ErrorMessageTranslator() { | ||
@NonNull | ||
@Override | ||
public String translate(int httpCode, @Nullable String errorMessage, | ||
@Nullable StripeError stripeError) { | ||
return "custom message"; | ||
} | ||
}); | ||
assertEquals("custom message", TranslatorManager.getErrorMessageTranslator() | ||
.translate(0, "original message", STRIPE_ERROR)); | ||
} | ||
} |