In my Android app with Kotlin minSdkVersion 26 and targetSdkVersion 28 , I want to translate all my app from English to Arabic language. In AndroidManifest.xml I add android:supportsRtl="true" I use in my app, a navigation bottom, that contains two items: one for home and another for account user. In the account Fragment, I just add a Text which when I click on, the language change from English to Arabic or vice versa. I create a "strings.xml" in a value folder that contains all text fields in English, after that I use the translation editor in Android Studio, I get the strings.xml(ar).
Here's the following fragment code:
and here's the setSavedLanguage function in Utility class :
But, the translation still didn't work in my app. I would like to know where's the problem in my code and how can I correct it.
Tim Moores wrote:"It doesn't work" is a rather useless problem description. What, exactly, is happening, and what, exactly, where you expecting to happen instead?
I mean the translation is not done, and nothing change in the text fields, still remains in anglais
Where does the context variable come from? How is your AccountFragment used? At what point do you observe that the language isn't changed (i.e. do you close the activity, do you start a new one)?
Anyway, the documentation for createConfigurationContext() states that it creates a new Context object. I don't think it actually installs it anywhere.
You must override the attachBaseContext() on whatever class you use as the base class for all your activities. Create a new Context with the required locale from the base context, and pass it to super.attachBaseContext(). To get the required locale when creating the new context, retrieve it from wherever your account activity stores it.
BTW, if you're targeting SDK version 28, why are you using the deprecated Fragment class?
This may not be your immediate issue, but the above says to me "if the chosen language is "En" then use the "ar" location and language, otherwise use the "en" ones".
I assume that's not correct.
It probably functions correctly if the OP intended the button to toggle the current language. The problem is that the variable name is misleading. It should probably be named 'currentLanguage'.
I resolve the problem I just change the code as the following :
But, now I have another error, the menu items title in the bottom navigation bar didn't translate
how can I make it translate
ANd how can I change the direction of icon for example ">" I want to be like this "<"
No, I meant the context variable from which you are getting the resources. I'm not familiar with Kotlin though, and I assume that it's syntactic sugar for calling the getContext() method.
Anyway, try clearing your existing menu, and then calling the inflateMenu() method on your navigation drawer.
In my app, I have a Main activity, that contains a bottomNavigationBar "Home","Account"
And two fragment, AccountFragment and HomeFragment
In the account Fragment, I add a textView "textChangeLang" : display "A" if the language is Arabic and "3" if the language is English and when I click on the language change.
I change my code as the following :
I don't get the result what I want, the Account fragment is translated But the direction of fileds not change (the rtl )
and the title in the botton navigation bar is not translated.
How can I correct my code to make all the fragment translated and change the direction
I need help please