Skip to content

Commit

Permalink
Only use fallback error messages for non-Spain Spanish
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Aug 14, 2023
1 parent 849fbc3 commit d5f671a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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
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 d5f671a

Please sign in to comment.