java - Change context language according to the current device language -
alright know it's okay have many languages in single app . couldnt find proper way . here want start , want activity detect android device language , change simple text
"@string/hello"
according . know method locale.getdefault().getlanguage() dont know how apply . secondly want give user option change language if wants .
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); locale.getdefault().getlanguage(); }
here simple layout
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.vermelha.tourword.mainactivity"> <textview android:text="@string/hello" android:layout_height="wrap_content" android:layout_width="wrap_content"/> </relativelayout>
values
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">tour word</string> <string name="title_activity_main">mainactivity</string> <string name="hello">hello</string> <string name="action_settings">settings</string> </resources>
values-tr
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">tour word</string> <string name="title_activity_main">mainactivity</string> <string name="hello">merhaba!</string> <string name="action_settings">ayralar</string> </resources>
you have create new folder in res: values-es example spanish, values-pt example portuguese
create new strings.xml file
put same string there
values/strings.xml
<string name="app_about_title">about</string>
values-es/strings.xml
<string name="app_about_title">acerca</string>
values-pt/strings.xml
<string name="app_about_title">sobre</string>
automatically when use @string/app_about_title
or via code r.string.app_about_title
translated if use language doesn't has translation, use standard (values fodler)
Comments
Post a Comment