I would like to create a multilingual android application.
Is there a way to detect what language the user prefers?
Is there a recommended way to manage multiple languages on Android or should I reinvent the wheel?
I would like to create a multilingual android application.
Is there a way to detect what language the user prefers?
Is there a recommended way to manage multiple languages on Android or should I reinvent the wheel?
Yes, there is a recommended way to manage multiple languages
Multilanguage support is easy done for android. Create a new values directory for the language with the suffix of the language code. For german: values-de or french: values-fr than copy your string.xml into that and translate each entry. Thats all you need.
Providing you follow the recommendations, detecting which language the user prefers is automatic.
Have a read of this:
http://developer.android.com/guide/topics/resources/localization.html
In Activity file
public boolean onOptionsItemSelected(MenuItem item)
{
String languageToLoad="en";
switch (item.getItemId()) {
case R.id.eng:
languageToLoad = "en";
break;
case R.id.hn:
languageToLoad = "hi";
break;
case R.id.te:
languageToLoad = "te";
break;
case R.id.ta:
languageToLoad = "ta";
break;
case R.id.ka:
languageToLoad = "kan";
break;
case R.id.ml:
languageToLoad = "ml";
break;
case R.id.mr:
languageToLoad = "mr";
break;
default:
break;
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config,getResources().getDisplayMetrics());
}
In res\menu\menus.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.connect.OrderProcess">
<item
android:title="Language"
app:showAsAction="never">
<menu>
<item
android:id="@+id/eng"
android:title="English"/>
<item
android:id="@+id/hn"
android:title="Hindi"/>
<item
android:id="@+id/te"
android:title="Telugu"/>
<item
android:id="@+id/ta"
android:title="Tamil"/>
<item
android:id="@+id/ka"
android:title="Kannada"/>
<item
android:id="@+id/ml"
android:title="Malayalam"/>
<item
android:id="@+id/mr"
android:title="Marathi"/>
</menu>
</item>
</menu>
AND Create folder and file
res\values\string.xml (English)
res\values-hi\string.xml (Hindi)
res\values-kan\string.xml (Kannada)
res\values-te\string.xml (Telugu)
res\values-ta\string.xml (Tamil)
res\values-ml\string.xml(Malayalam)
res\values-mr\string.xml (Marathi)
In string.xml (Hindi)
<resources>
<string name="email">ईमेल</string>
<string name="password">पासवर्ड </string>
</resources>
Know that it's a late post, thought of sharing a demo application, so it will be helpful for some people.
Multi-language app using shared preferences
This demo showcase the following two scenario's,
The selected language is stored in shared preferences. So next time once the app is open, the prefered language will be choosen automatically.
Source code - https://github.com/anurajr1/Multi-language_App
© 2022 - 2024 — McMap. All rights reserved.