Skip to content

Commit

Permalink
Only use fallback error messages for non-Spain Spanish (#7147)
Browse files Browse the repository at this point in the history
* Only use fallback error messages for non-Spain Spanish

* Add tests

* Update changelog
  • Loading branch information
tillh-stripe authored and fionnbarrett-stripe committed Aug 17, 2023
1 parent ca96453 commit de448cd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## XX.XX.XX - 2023-XX-XX

### PaymentSheet
* [FIXED][7147](https://github.com/stripe/stripe-android/pull/7147) Fixed an issue where we displayed incorrect error messages for some languages.

## 20.28.1 - 2023-08-09

### PaymentSheet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package com.stripe.android.networking
import android.content.Context
import com.stripe.android.R
import com.stripe.android.core.StripeError
import java.util.Locale
import com.stripe.android.uicore.R as UiCoreR

@Suppress("ComplexMethod")
internal fun StripeError.withLocalizedMessage(context: Context): StripeError {
val newMessage = context.mapErrorCodeToLocalizedMessage(code) ?: message
val newMessage = if (shouldFallBackToLocalizedError) {
context.mapErrorCodeToLocalizedMessage(code)
} else {
message ?: context.mapErrorCodeToLocalizedMessage(code)
}

return copy(message = newMessage)
}

Expand All @@ -29,3 +35,14 @@ internal fun Context.mapErrorCodeToLocalizedMessage(code: String?): String? {
}
return messageResourceId?.let { getString(it) }
}

/**
* For some language tags, our backend is unable to provide translated error messages. For these
* languages, we fall back to local error messages. As of right now, the only languages for which we
* are aware of this issue are Spanish languages outside of Spain, such as in Argentina or Chile.
*/
private val shouldFallBackToLocalizedError: Boolean
get() {
val locale = Locale.getDefault()
return locale.language.lowercase() == "es" && locale.country.lowercase() != "es"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.stripe.android

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.stripe.android.core.StripeError
import com.stripe.android.networking.withLocalizedMessage
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.util.Locale

@RunWith(RobolectricTestRunner::class)
class StripeErrorMappingTest {

private val context: Context = ApplicationProvider.getApplicationContext()

@Test
fun `Uses backend message for non-Spanish locale`() {
withLocale(Locale.US) {
val error = StripeError(
code = "incorrect_number",
message = "The backend message",
)
val updatedError = error.withLocalizedMessage(context)
assertThat(updatedError).isEqualTo(error)
}
}

@Test
fun `Uses backend message for Spanish locale from Spain`() {
withLocale(Locale("es", "es")) {
val error = StripeError(
code = "incorrect_number",
message = "The backend message",
)
val updatedError = error.withLocalizedMessage(context)
assertThat(updatedError).isEqualTo(error)
}
}

@Test
fun `Uses client message for Spanish locale from outside Spain`() {
withLocale(Locale("es", "ar")) {
val error = StripeError(
code = "incorrect_number",
message = "The backend message",
)
val updatedError = error.withLocalizedMessage(context)
assertThat(updatedError).isNotEqualTo(error)
}
}

private inline fun <T> withLocale(locale: Locale, block: () -> T): T {
val original = Locale.getDefault()
Locale.setDefault(locale)
val result = block()
Locale.setDefault(original)
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ public void createTokenSynchronous_withInvalidCardNumber_throwsCardException() {
CardException.class,
() -> defaultStripe.createCardTokenSynchronous(cardParams)
);
assertEquals("Your card's number is invalid.", cardException.getMessage());
assertEquals("Your card number is incorrect.", cardException.getMessage());
}

@Test
Expand Down

0 comments on commit de448cd

Please sign in to comment.