Skip to content

Commit 59325de

Browse files
authored
feat: Localization - AuthUIStringProvider and locale override (#2221)
* feat: added context AuthUIConfiguration and default string provider * add/expose existing localized strings to allow overrides * added custom string provider sample, tests for locale overrides * chore: code cleanup
1 parent c180522 commit 59325de

File tree

14 files changed

+647
-162
lines changed

14 files changed

+647
-162
lines changed

auth/src/main/java/com/firebase/ui/auth/compose/configuration/AuthUIConfiguration.kt

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@
1414

1515
package com.firebase.ui.auth.compose.configuration
1616

17+
import android.content.Context
1718
import java.util.Locale
1819
import com.google.firebase.auth.ActionCodeSettings
1920
import androidx.compose.ui.graphics.vector.ImageVector
21+
import com.firebase.ui.auth.compose.configuration.stringprovider.AuthUIStringProvider
22+
import com.firebase.ui.auth.compose.configuration.stringprovider.DefaultAuthUIStringProvider
2023

21-
fun actionCodeSettings(
22-
block: ActionCodeSettings.Builder.() -> Unit
23-
) = ActionCodeSettings.newBuilder().apply(block).build()
24+
fun actionCodeSettings(block: ActionCodeSettings.Builder.() -> Unit) =
25+
ActionCodeSettings.newBuilder().apply(block).build()
2426

25-
fun authUIConfiguration(block: AuthUIConfigurationBuilder.() -> Unit): AuthUIConfiguration {
26-
val builder = AuthUIConfigurationBuilder()
27-
builder.block()
28-
return builder.build()
29-
}
27+
fun authUIConfiguration(block: AuthUIConfigurationBuilder.() -> Unit) =
28+
AuthUIConfigurationBuilder().apply(block).build()
3029

3130
@DslMarker
3231
annotation class AuthUIConfigurationDsl
3332

3433
@AuthUIConfigurationDsl
3534
class AuthUIConfigurationBuilder {
35+
var context: Context? = null
3636
private val providers = mutableListOf<AuthProvider>()
3737
var theme: AuthUITheme = AuthUITheme.Default
38-
var stringProvider: AuthUIStringProvider? = null
3938
var locale: Locale? = null
39+
var stringProvider: AuthUIStringProvider? = null
4040
var isCredentialManagerEnabled: Boolean = true
4141
var isMfaEnabled: Boolean = true
4242
var isAnonymousUpgradeEnabled: Boolean = false
@@ -48,36 +48,16 @@ class AuthUIConfigurationBuilder {
4848
var isDisplayNameRequired: Boolean = true
4949
var isProviderChoiceAlwaysShown: Boolean = false
5050

51-
fun providers(block: AuthProvidersBuilder.() -> Unit) {
52-
val builder = AuthProvidersBuilder()
53-
builder.block()
54-
providers.addAll(builder.build())
55-
}
51+
fun providers(block: AuthProvidersBuilder.() -> Unit) =
52+
providers.addAll(AuthProvidersBuilder().apply(block).build())
5653

5754
internal fun build(): AuthUIConfiguration {
58-
validate()
59-
return AuthUIConfiguration(
60-
providers = providers.toList(),
61-
theme = theme,
62-
stringProvider = stringProvider,
63-
locale = locale,
64-
isCredentialManagerEnabled = isCredentialManagerEnabled,
65-
isMfaEnabled = isMfaEnabled,
66-
isAnonymousUpgradeEnabled = isAnonymousUpgradeEnabled,
67-
tosUrl = tosUrl,
68-
privacyPolicyUrl = privacyPolicyUrl,
69-
logo = logo,
70-
actionCodeSettings = actionCodeSettings,
71-
isNewEmailAccountsAllowed = isNewEmailAccountsAllowed,
72-
isDisplayNameRequired = isDisplayNameRequired,
73-
isProviderChoiceAlwaysShown = isProviderChoiceAlwaysShown
74-
)
75-
}
55+
val context = requireNotNull(context) {
56+
"Application context is required"
57+
}
7658

77-
private fun validate() {
78-
// At least one provider
79-
if (providers.isEmpty()) {
80-
throw IllegalArgumentException("At least one provider must be configured")
59+
require(providers.isNotEmpty()) {
60+
"At least one provider must be configured"
8161
}
8262

8363
// No unsupported providers
@@ -113,13 +93,36 @@ class AuthUIConfigurationBuilder {
11393
else -> null
11494
}
11595
}
96+
97+
return AuthUIConfiguration(
98+
context = context,
99+
providers = providers.toList(),
100+
theme = theme,
101+
locale = locale,
102+
stringProvider = stringProvider ?: DefaultAuthUIStringProvider(context, locale),
103+
isCredentialManagerEnabled = isCredentialManagerEnabled,
104+
isMfaEnabled = isMfaEnabled,
105+
isAnonymousUpgradeEnabled = isAnonymousUpgradeEnabled,
106+
tosUrl = tosUrl,
107+
privacyPolicyUrl = privacyPolicyUrl,
108+
logo = logo,
109+
actionCodeSettings = actionCodeSettings,
110+
isNewEmailAccountsAllowed = isNewEmailAccountsAllowed,
111+
isDisplayNameRequired = isDisplayNameRequired,
112+
isProviderChoiceAlwaysShown = isProviderChoiceAlwaysShown
113+
)
116114
}
117115
}
118116

119117
/**
120118
* Configuration object for the authentication flow.
121119
*/
122120
class AuthUIConfiguration(
121+
/**
122+
* Application context
123+
*/
124+
val context: Context,
125+
123126
/**
124127
* The list of enabled authentication providers.
125128
*/
@@ -131,14 +134,14 @@ class AuthUIConfiguration(
131134
val theme: AuthUITheme = AuthUITheme.Default,
132135

133136
/**
134-
* A custom provider for localized strings.
137+
* The locale for internationalization.
135138
*/
136-
val stringProvider: AuthUIStringProvider? = null,
139+
val locale: Locale? = null,
137140

138141
/**
139-
* The locale for internationalization.
142+
* A custom provider for localized strings.
140143
*/
141-
val locale: Locale? = null,
144+
val stringProvider: AuthUIStringProvider = DefaultAuthUIStringProvider(context, locale),
142145

143146
/**
144147
* Enables integration with Android's Credential Manager API. Defaults to true.

auth/src/main/java/com/firebase/ui/auth/compose/configuration/AuthUIStringProvider.kt

Lines changed: 0 additions & 81 deletions
This file was deleted.

auth/src/main/java/com/firebase/ui/auth/compose/configuration/PasswordRule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.firebase.ui.auth.compose.configuration
1616

17+
import com.firebase.ui.auth.compose.configuration.stringprovider.AuthUIStringProvider
18+
1719
/**
1820
* An abstract class representing a set of validation rules that can be applied to a password field,
1921
* typically within the [AuthProvider.Email] configuration.

0 commit comments

Comments
 (0)