使微调器有选择地翻译字符串

make a spinner selectively translate strings

我正在尝试制作一个微调器,我select可以有效地翻译页面上的字符串。我已将我的应用程序设置为根据区域设置语言翻译大部分页面。在下图中,区域设置语言将影响顶部字符串,但我的主页上有一个微调器,其中包含所有可用的 android 语言,我希望用户能够 select微调器项目之一,因此下图中的底部字符串将被翻译成该语言。我找不到设置它的方法,到目前为止我看到的所有建议都只是如何配置应用程序以设置语言环境。任何方向将不胜感激!

我想你可以使用 Android-LocalizationActivity

这是其自述文件的一部分:

It's basic for android application to be supported multiple languages. Yeah! It's very easy because android has String Resource. Developer just had to prepare the text for different languages then android system will use itself. But frequently problem is "On-time Language Changing". Because the String Resource was designed to be depending on current device language. but if we want to change the language by click some button. It will be difficult to handle it. This problem will solved because I have created a new library to handle application language. It called "Localization Activity" library.

实现起来就是这么简单。只需将 Activity 扩展为 LocalizationActivity。这里有一个带有按钮的示例代码:

import android.os.Bundle;
import android.view.View;

import com.akexorcist.localizationactivity.LocalizationActivity;

public class MainActivity extends LocalizationActivity implements View.OnClickListener {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_simple);

    findViewById(R.id.btn_th).setOnClickListener(this);
    findViewById(R.id.btn_en).setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.btn_en) {
      setLanguage("en");
    } else if (id == R.id.btn_th) {
      setLanguage("th");
    }
  }
}

当用户点击btn_th(泰国)时,所有语言将被翻译成泰语。所以你只需要用微调器调整它。