• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Translate android app with kotlin from English to Arabic

 
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help please, I'm really stuck
 
Saloon Keeper
Posts: 14795
333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14795
333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14795
333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14795
333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 660
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 77559
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does that code answer Stephan's question, even after Randy gave you that hint?
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic