AppSystemResourcesExtension
IAPMiniProgram SDK provides several ways to dynamically adjust the language display based on the user's language preferences to support multilingual culture. For details, see the Language document.
This topic introduces the way that IAPMiniProgram SDK can customize application resources (Resources
) for super app to configure its language flexibly. Follow this topic to realize this ability by implementing the specific interface.
Proceduresapp
To realize the customization, follow these steps:
Step 1. Implement the AppSystemResourcesExtension interface
Implement the AppSystemResourcesExtension interface and write code in the required methods according to your business logic. See the following example for how to implement this interface.
class CustomeGetAppConfigExtensionImpl : AppSystemResourcesExtension {
override fun getAppResource():Resources{
return context.applicationContext.resources;
}
override fun canRefreshAppResource(): Boolean {
return true
}
}
Step 2. Register CustomeGetAppConfigExtensionImpl
Refer to the following sample code and call the registerExtension interface to register after initializing the SDK.
IAPConnect.init(application, initConfig, object : InitCallback {
override fun onSuccess() {
//ยทยทยท
Griver.registerExtension(
GriverExtensionManifest(
AppSystemResourcesExtension::class.java,
CustomeGetAppConfigExtensionImpl()
)
)
}
})
Step 3. Update application resources (Resources
) after super app language is changed
After the language of super app is changed, need to refresh the Configuration
of the application resources (Resources
).
Take the following code as an exmaple. First, it retrieves the application resources for the current context. Then, it obtains the current configuration information. Next, it sets the locale to English (United States). Finally, it updates the application resources using the updated configuration information,
// Update locale
val resources: Resources = context.applicationContext.resources
val configuration: Configuration = resources.getConfiguration()
configuration.setLocale(Locale.forLanguageTag("en-US"))
resources.updateConfiguration(configuration, resources.getDisplayMetrics())
Interface
AppSystemResourcesExtension interface
The definition of this interface is shown in the following codes:
interface AppSystemResourcesExtension : GriverExtension {
fun getAppResource(): Resources?
fun canRefreshAppResource(): Boolean
}
Based on the definition, this interface provides the following methods:
Method | Description |
getAppResource | When the SDK needs to use a If this method returns |
canRefreshAppResource | When the Configuration changes, the SDK determines whether to update the language in the SDK based on the return value of this method. |