• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Translate android app with kotlin from English to Arabic

 
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 6622
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"It doesn't work" is a rather useless problem description. What, exactly, is happening, and what, exactly, where you expecting to happen instead?
 
Maha Sakka
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Tim Moores
Saloon Keeper
Posts: 6622
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A search for "android change language programmatically" finds https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android which indicates that calling  updateConfiguration is involved.
 
Maha Sakka
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:  updateConfiguration is involved.


updateConfiguration is deprecated, and I change it with        
 
Maha Sakka
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help please, I'm really stuck
 
Saloon Keeper
Posts: 12389
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Master Rancher
Posts: 4694
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Stephan van Hulst
Saloon Keeper
Posts: 12389
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Maha Sakka
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "<"
 
Stephan van Hulst
Saloon Keeper
Posts: 12389
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, where did you get the context from?
 
Maha Sakka
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Once again, where did you get the context from?


did you mean the import ??
import android.content.Context
I change the following code :

How can I refresh the fragment and all the app ?
 
Stephan van Hulst
Saloon Keeper
Posts: 12389
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 489
7
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Once again, where did you get the context from?


The context is getting called from Fragment.java. We can use context as long as our class is extends to Fragment.
 
Maha Sakka
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 70591
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does that code answer Stephan's question, even after Randy gave you that hint?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic