1

I'm developing a multi language app and I'm using code below and it was working well but now it has problem in android api 24 and below without any error. it just translate strings but I need my app be rtl. what should I use instead of my code?

fun setNewLocale(mContext: Context, language: String): Context {
      setLanguagePref(mContext, language)
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
           return updateResources(mContext, getLanguagePref(mContext)!!)             
      }
      return updateResourcesLegacy(mContext, getLanguagePref(mContext)!!)
}   

@TargetApi(Build.VERSION_CODES.N)   
private fun updateResources(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)
        val configuration = context.resources.configuration
        configuration.setLocale(locale)

        return context.createConfigurationContext(configuration)
}

private fun updateResourcesLegacy(context: Context, language: String): Context {
       val locale = Locale(language)
       Locale.setDefault(locale)
       val resources = context.resources
       val configuration = resources.configuration
       configuration.locale = locale

       resources.updateConfiguration(configuration, resources.displayMetrics)

       return context
 }
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

1 Answers1

0

For supporting multi language, if your users choose a language that uses right-to-left (RTL) scripts, such as Persian or Arabic, in the first you must support RTL in your app with lower API.

You can check official documents here and for your case, this section provide more details.

BTW you can check this and this ones examples which provide some details and all you need to know.

Happy coding! movafagh bashi :)

Naruto Uzumaki
  • 2,019
  • 18
  • 37