我想让Spinner
更改应用的语言。我正在创建一个演示应用程序,在此我有多个strings.xml
文件。我创建了一个Spinner
并添加了语言列表。
单击某种语言(微调项目)我想更改应用程序的语言。
怎么做?
的strings.xml
<string name="app_name">Multi Language App</string>
<string name="action_settings">Settings</string>
<string name="welcome">Welcome!</string>
<string name="email">Email Address</string>
<string name="password">Password</string>
<string name="login">Login</string>
<string name="signup">Don\'t have account? Sign Up</string>
<string-array name="languages">
<item name="English"></item>
<item name="French"></item>
<item name="Hindi"></item>
<item name="Japanese"></item>
</string-array>
的strings.xml(DE)
<string name="welcome">Willkommen!</string>
<string name="email">Email Addresse</string>
<string name="password">passowrd</string>
<string name="login">Login</string>
<string name="signup">müssen nicht angemeldet? Anmeldung</string>
的strings.xml(FR)
<string name="welcome">accueil</string>
<string name="email">adresse e-mail</string>
<string name="password">mot de passe</string>
<string name="login">connexion</string>
<string name="signup">Ne pas avoir un compte? signer</string>
stringd.xml(HI)
<string name="welcome">स्वागतम</string>
<string name="email">ईमेल पता</string>
<string name="password">पासवर्ड</string>
<string name="login">लॉगिन</string>
<string name="signup">खाता नहीं है? साइन अप करें</string>
MainActivity.java
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
Spinner spinner = (Spinner) findViewById(R.id.spinnerLanguage);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.languages, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
if(pos == 0) {
//what to be done here to chnage the app's language?
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
任何人都可以帮忙吗?
答案 0 :(得分:2)
您需要以编程方式更改设备的区域设置:
export class ErrorHandlerService implements HttpInterceptor {
constructor() { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next
.handle(req)
.catch(err => {
/* Do what you want here */
/* The return a thrown Observable, containing whatever you want */
return Observable.throw(err);
});
}
}
如果有必要,请刷新您的UI。
答案 1 :(得分:0)
我认为此链接可以帮助您Localize the UI with Translations Editor
Littel注意到德国人更好:)
<string name="welcome">Willkommen!</string>
<string name="email">Email Adresse</string>
<string name="password">Password</string>
<string name="login">Login</string>
<string name="signup">Sie sind nicht angemeldet? Anmelden</string>
答案 2 :(得分:0)
setLanguage(ApplicationHelper.Language.DE);
[...]
public void setLanguage(ApplicationHelper.Language l)
{
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
答案 3 :(得分:0)
首先在findViewById
之后添加此行。
Spinner spinner = (Spinner) findViewById(R.id.spinnerLanguage);
spinner.setOnItemSelectedListener(this);
然后在onItemSelected()
String lang= parent.getItemAtPosition(pos).toString();
String languageToLoad = null;
if(lang.equals("Hindi")){
languageToLoad="hi";
}else if(lang.equals("English")){
languageToLoad="en";
}else if(lang.equals("French")){
languageToLoad="fr";
}else if(lang.equals("Japanese")){
languageToLoad="de";
}
if(languageToLoad!=null) {
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
希望它对你有所帮助。