gpt4 book ai didi

java - 从语言显示名称获取语言环境

转载 作者:行者123 更新时间:2023-12-03 18:04:33 24 4
gpt4 key购买 nike

我正在尝试从其显示名称设置“文字转语音”语言。问题在于文本到语音转换需要setLanguage的语言环境。如何从显示语言转换为语言环境?我首先以这种方式设置回收者列表:

 public void getSystemLanguages() {
systemLanguages = Resources.getSystem().getAssets().getLocales();
for (int i = 1; i < systemLanguages.length; i++) {
String sL = systemLanguages[i];
Locale loc = Locale.forLanguageTag(sL);
String locDisplayResults = loc.getDisplayName();
languagesList.add(new Languages(locDisplayResults));
Collections.sort(languagesList, new Comparator<Languages>() {
@Override
public int compare(Languages languages1, Languages languages2) {
return languages1.getLocaleDisplay().compareTo(languages2.getLocaleDisplay());
}
});
recyclerAdapter.notifyDataSetChanged();
}
}


然后,我有一个看起来像这样的回收站适配器:

public void onClick(View view) {
final Languages currentLanguages = languagesList.get(getAdapterPosition());
Snackbar snackbar;
snackbar = Snackbar.make(view, currentLanguages.getLocaleDisplay() + " selected", Snackbar.LENGTH_INDEFINITE)
.setAction("Next", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
chosenLanguage = localeDisplay.getText().toString();
intent.putExtra("language_key", chosenLanguage);
v.getContext().startActivity(intent);
}
});

View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(grey_blue);
snackbar.show();
}


我认为应该很容易实现,尽管我花了一个多星期没有成功。任何帮助都太棒了!

编辑:我添加我的语言类,以防有人可以帮助我解决Andreas的问题:

    public class Language {

public String localeDisplay;

public Language (String localeDisplay) {
this.localeDisplay = localeDisplay;
}

public String getLocaleDisplay() {
return localeDisplay;


}
}
干杯!

这是我尝试过的:

public class Language {

public String localeDisplay;
public Locale mLocale;


public Language(String localeDisplay) {
this.localeDisplay = localeDisplay;
}

public Language(Locale mLocale){
this.mLocale = mLocale;
}

public String getLocaleDisplay() {
return localeDisplay;
}

public void setmLocale(Locale mLocale) {
this.mLocale = mLocale;
}

public Locale getmLocale() {
return mLocale;
}


}

新修改:这是正确的版本:

public class Language implements Comparable <Language> {

public String localeDisplay;
public Locale mLocale;

public Language(Locale mLocale, String localeDisplay) {
if (mLocale != null)
this.mLocale = mLocale;
if (localeDisplay != null)
this.localeDisplay = localeDisplay;
}

public Locale getmLocale(){
return mLocale;
}

public String getLocaleDisplay(){
return localeDisplay;
}

public int compareTo(Language l){
return localeDisplay.compareTo(l.localeDisplay);
}


}

最佳答案

您可以让广播接收器侦听区域设置更改。
https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED

此广播接收器将填充一个变量,该变量将用于填充此适配器。

请注意,更改新的语言环境后,您将需要notifyDatasetChanged()。

祝你好运,我希望这会有所帮助:)

关于java - 从语言显示名称获取语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40810537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com