Saravanan
Saravanan

Reputation: 45

Android Multi Language Support

In my android application, I've to provide two language versions (GERMAN,ENGLISH) of the application.In the German Version ,Some extra categories(extra screens) are available compared to English Version.What are the best practices,I should follow?

Upvotes: 2

Views: 5865

Answers (5)

fasika million
fasika million

Reputation: 1

// I added this code in every activity and it worked and ofcoarse u need to add a translation string first

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_log_in) 

    changeLang = findViewById(R.id.changeLang)
    changeLang.setOnClickListener {
        baseContext.setAppLocale("am")
        recreate() }

}

private fun Context.setAppLocale(language: String): Context {

    val locale = Locale(language)
    Locale.setDefault(locale)
    val config = resources.configuration
    config.setLocale(locale)
    config.setLayoutDirection(locale)
    return createConfigurationContext(config)

}

Upvotes: 0

santoshnisal007
santoshnisal007

Reputation: 99

You will have to create two folder's in your resource directory named as 'values' and 'values-de' for English and German Language respectively. Also, you can design layouts based on device language. for ex. layout folder for English and layout-de folder for the German Language. Same way to set the font sizes of TextViews you can create a file named as dimen.xml in values folder for respective languages.

Upvotes: 0

santosh kumar
santosh kumar

Reputation: 2972

You need to create string.xml file for both GERMAN,ENGLISH language with same key with different language text.

Upvotes: 0

Aman Shekhar
Aman Shekhar

Reputation: 2780

You have to make two folder in the res directory.

values (This is the by default, put English Language here.)

values-in(This is for Indonesia language), similarlyyou can find for other languages too.

In both the folder, you will having one file named strings.xml.

Now put all your strings in this two files, with the same refrence name.

Like below.

in values-in/strings.xml

<string name="date">Tanggal</string>

in values/strings.xml

<string name="date">Date</string>

and use this method in your MainActivity Class.

 public void changeLanguage(String lang) {
        Locale myLocale;
        if (lang.equalsIgnoreCase(""))
            return;
        myLocale = new Locale(lang);
        Locale.setDefault(myLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

    }

Upvotes: 2

AshAR
AshAR

Reputation: 228

You can get the user language using -

Locale.getDefault().getDisplayLanguage();

and then maybe using if else and a boolean data type you may program your system accordingly.

Or you may want the user to select his or her language themselves using RadioButtons.

Use @strings for the Text Content as then you can translate them easily using coding

Upvotes: 0

Related Questions