java 使用字符串文件更改android应用程序中的语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34573201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change language in android application using string file
提问by kcNeko
I'm new in java and actually developing a game app and I wanted add a feature which could change languages in the game.
我是 Java 新手,实际上正在开发一个游戏应用程序,我想添加一个可以更改游戏语言的功能。
I already made 2 strings.xml
. One is the default (English), the other one is the translated version (File)
我已经做了 2 strings.xml
。一个是默认(英文),另一个是翻译版本(文件)
Here's my code
这是我的代码
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class LanguageActivity extends Activity {
private static Button button_fil;
private static Button button_eng;
public void onButtonClickListener() {
button_fil = (Button) findViewById(R.id.btnFilipino);
button_fil.setOnClickListener(
new View.OnClickListener() {@
Override
public void onClick(View v) {
Toast.makeText(LanguageActivity.this, "Filipino Language", Toast.LENGTH_SHORT).show();
}
}
);
button_eng = (Button) findViewById(R.id.btnEnglish);
button_eng.setOnClickListener(
new View.OnClickListener() {@
Override
public void onClick(View v) {
Toast.makeText(LanguageActivity.this, "English Language", Toast.LENGTH_SHORT).show();
}
}
);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language);
onButtonClickListener();
}
Thanks a lot!
非常感谢!
回答by Mr Robot
Try this example please. Maybe it will help you. Here i used a spinner for selecting language.
请试试这个例子。也许它会帮助你。在这里,我使用了一个微调器来选择语言。
In your actvity
在你的活动中
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class AndroidLocalize extends Activity {
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerctrl = (Spinner) findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected Tamil", Toast.LENGTH_SHORT)
.show();
setLocale("ta");
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"You have selected Hindi", Toast.LENGTH_SHORT)
.show();
setLocale("hi");
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
}
}
in your XML
在你的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/greet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/greet"
android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/langselection"
android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/languages"
android:gravity="center" android:paddingTop="25sp" />
</LinearLayout>
and create folders in your res like
then add strings.xml for your language like
然后为您的语言添加strings.xml,例如
<resources>
<string name="app_name">Androidlocalization</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_android_localize">AndroidLocalize</string>
<string name="greet">???? ???? !!</string>
<string name="langselection">??? ???? ??? ?? ???? ?? ??????? ???? ????? ??? ?? ??? ????!!!!</string>
<string name="chooselang">Choose the language</string>
<string-array name="languages">
<item>Select language</item>
<item>?????</item>
<item>?????</item>
<item>English</item>
</string-array>
</resources>
please update your manifest also , i hope that will resolve your problem..
也请更新您的清单,我希望能解决您的问题..
update like this.
像这样更新。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".AndroidLocalize"
android:label="@string/title_activity_android_localize"
android:configChanges="locale|orientation|keyboardHidden"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
回答by Zumry Mohamed
回答by AndroidMechanic - Viral Patel
This is a methodi wrote and is working perfectly well for me for changing the language from app (and JUST FOR A SINGLE APP - not the entire device):
这是我编写的一种方法,对我来说非常适合从应用程序更改语言(并且仅用于单个应用程序 - 而不是整个设备):
private void setLanguageForApp(String languageToLoad){
Locale locale;
if(languageToLoad.equals("not-set")){ //use any value for default
locale = Locale.getDefault();
}
else {
locale = new Locale(languageToLoad);
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
NOTE:Call this method before setContentView()
in the first activity's onCreate()
everytime when the app is opened.
注意:每次打开应用程序时,setContentView()
在第一个活动的之前调用此方法onCreate()
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setLanguageForApp("en"); //hard-coded here - get from whereever you stored
setContentView(R.layout.activity_category_list);
...
...
...
Store the selected locale code in shared preferences and retrieve to pass as parameter.
将选定的区域设置代码存储在共享首选项中并检索以作为参数传递。
Method for language selection dialog:(Note: it reloads app after language change to bring the language change in effect)
语言选择对话框的方法:(注意:语言更改后重新加载应用程序以使语言更改生效)
private void showLanguageChangePopup() {
CharSequence languages[] = new CharSequence[] {
"English",
"à¤1à¤?à¤?à¤|ॠ(Hindi)",
"Fran?§ais (French)",
"Italiano (Italian)",
"Deutsch (German)",
"Espa?±ol (Spanish)",
"?—¥???èa? (Japanese)",
"í??êμ-ì–′ (Korean)",
"Nederlands (Dutch)",
"Portugu?as (Portuguese)",
"?????DoD?D1 (Russian)",
"??-?–? (Chinese)",
"?§ù??1?±?¨ù??? (Arabic)"
};
final String codes[] = new String[] {
"en",
"hi",
"fr",
"it",
"de",
"es",
"ja",
"ko",
"nl",
"pt",
"ru",
"zh",
"ar"
};
int currentLangIndex = Prefs.getUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, getBaseContext());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.text_select_language);
builder.setSingleChoiceItems(languages, currentLangIndex, null);
builder.setNegativeButton(R.string.text_translate_cancel, null);
builder.setPositiveButton(R.string.action_change_language, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
int selectedIndex = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
Prefs.setUserPreferenceStringValue(Prefs.Key.LANGUAGE, codes[selectedIndex], getBaseContext());
Prefs.setUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, selectedIndex, getBaseContext());
Intent i = new Intent(CategoryListActivity.this, CategoryListActivity.class);
startActivity(i);
finish();
}
});
builder.show();
}
回答by Krishna
When you are supporting multiple languages , You need to create separate values folder like values-fr for instance and put your stings.xml file inside this folder . Should work. Hope this helps!
当您支持多种语言时,您需要创建单独的值文件夹,例如 values-fr 并将您的 stings.xml 文件放在此文件夹中。应该管用。希望这可以帮助!
回答by Vinayagam.D
<resources>
<string name="app_name">Androidlocalization</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_android_localize">AndroidLocalize</string>
<string name="greet">???? ???? !!</string>
<string name="langselection">??? ???? ??? ?? ???? ?? ??????? ???? ????? ??? ?? ??? ????!!!!</string>
<string name="chooselang">Choose the language</string>
<string-array name="languages">
<item>Select language</item>
<item>?????</item>
<item>?????</item>
<item>English</item>
</string-array>
</resources>
Each code is in same folder for different language add different value folder
For example value folder for hindi goes inside value-hi
每个代码在同一文件夹中为不同的语言添加不同的值文件夹
例如,印地语的值文件夹进入 value-hi