如何在其他活动中创建多种语言?

时间:2018-02-14 07:57:55

标签: android multiple-languages

在其他问题中,我有回答创建多种语言。但并未用于所有活动/页面的应用程序。如何更改其他活动/页面中的语言?

我只有主要活动/页面的代码,如何在其他活动/页面中创建?

我使用此代码更改语言:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(LocaleHelper.onAttach(newBase, "en"));
}

private void updateView(String lang) {
    Context context = LocaleHelper.setLocale(this, lang);
    Resources resources = context.getResources();

    navigation_view.getMenu().findItem(R.id.account).setTitle(resources.getString(R.string.menuAccount));
    navigation_view.getMenu().findItem(R.id.settingPassword).setTitle(resources.getString(R.string.menuSettingPass));
    navigation_view.getMenu().findItem(R.id.about).setTitle(resources.getString(R.string.menuAbout));
    navigation_view.getMenu().findItem(R.id.logout).setTitle(resources.getString(R.string.menuLogout));
    navigation_view.getMenu().findItem(R.id.login).setTitle(resources.getString(R.string.menuLogin));
}

我使用这个triger来改变语言:

Paper.book().write("language", "id");
updateView((String)Paper.book().read("language"));
navigation_view.getMenu().findItem(R.id.language_in).setVisible(false);
navigation_view.getMenu().findItem(R.id.language_en).setVisible(true);

源代码来自: source code full from EDMT Dev

2 个答案:

答案 0 :(得分:3)

将您的字符串资源放在不同的文件夹中:

  • /res/values/strings.xml
  • /res/values-en/strings.xml
  • /res/values-es/strings.xml
  • /res/values-jp/strings.xml

系统会根据您的语言环境自动加载正确的字符串资源。 您需要在所有活动中使用<!--Problem 2 - MIME Type Error -The src attribute normally in the <video> tag has been replaced by 2 <source> tags. -The optimal video MIME types can be narrowed down to two types: video/mp4 and video/webm. -Have a copy of each MP4 encoded as a WebM video file and set the src of each <source> tag accordingly. Set the type attribute to video/mp4 and video/webm. -NOTE: in this demo the webm file extension is *.mp4. DO NOT DO THIS. They are webm files with *.mp4 extensions because the host doesn't recognize webm files. --> <video id='vid0' width='320' controls> <source id='src0' src='https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005609.mp4' type='video/mp4'> <source id='src1' src='https://storage04.dropshots.com/photos7000/photos/1381926/20180214/041157.mp4' type='video/webm'> </video> <ol> <li> <a href='https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005609.mp4' data-webm='photos7000/photos/1381926/20180214/041157.mp4'>leader00</a> </li> <li> <a href='https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005610.mp4' data-webm='photos7000/photos/1381926/20180214/041153.mp4'>leader01</a> </li> <li> <a href='https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005611.mp4' data-webm='photos7000/photos/1381926/20180214/041154.mp4'>leader02</a> </li> <li> <a href='https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005612.mp4' data-webm='https://storage04.dropshots.com/photos7000/photos/1381926/20180214/041156.mp4'>leader03</a> </li> </ol>方法。为了更清晰的代码,您可以使用attachBaseContext方法创建BaseActivity类,并从此基类扩展您的所有活动:

attachBaseContext

答案 1 :(得分:0)

使用正确配置的资源创建一个新的Context,并通过attachBaseContext将其作为Application和Activity的基础。

public class App extends Application {

    @Override
    protected void attachBaseContext(Context base) {
       super.attachBaseContext(LocaleHelper.setLocale(base));
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
       super.onConfigurationChanged(newConfig);
       LocaleManager.setLocale(this);
    }
...

}

其他活动的基础活动

public abstract class BaseActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      .....
   }