3

I previously tested these codes, but gave the error an obsolete locale

public static void setLanguage(Context context, String languageCode){
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;  // Deprecated !!
context.getApplicationContext().getResources().updateConfiguration(config,
        context.getResources().getDisplayMetrics());

}

1 Answers1

0

You should have in your application a file called String.xml located in res/values/strings.xml

You should place all your text in that file

eg.

<resources>     
  <string name="title">Test</string>
  <string name="message">Test</string>
</resources>

this is how you would set the string value to your Textview

eg.

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"     
        android:text="@string/title" />

so when you decide that you want to add more languages you will need to create other string.xml files in your resource folder and add the translated strings there. the example below the folder "values-hi" refers to Hindi,

eg.

MyProject/
res/
   values/
       strings.xml
   values-hi/
       strings.xml

then android would take care to localize the language automatically based on the device configuration.

for more detailed information please refer to this link https://developer.android.com/training/basics/supporting-devices/languages.html

Ruan_Lopes
  • 1,381
  • 13
  • 18
  • Yes , I have String,xml and the values that you says. But my problem is the obsolete local error. local is deprecated – Rasool Fazeli Apr 24 '18 at 21:27
  • you don't need to use that. could you create a new string.xml file as the example above? What dialects are you trying to translate your app for? – Ruan_Lopes Apr 24 '18 at 21:29
  • I know the language of the app will change based on the language of the phone. But I want to create a program with different dialects, not in different languages – Rasool Fazeli Apr 24 '18 at 21:36
  • could you give an example of the dialect you are trying to translate for? – Ruan_Lopes Apr 24 '18 at 21:46
  • Check if this answer helps you https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077 – Ruan_Lopes Apr 24 '18 at 22:02
  • Thank you for your answer, but this code is a bit complicated and I am an amateur and do not understand. – Rasool Fazeli Apr 24 '18 at 22:55