android中多语言的本地化

时间:2014-02-07 07:27:44

标签: android localization multilingual

这个问题对我来说非常有帮助。我从未尝试过应用程序的本地化。所以我在这里全新。但我仍面临一些问题。 我有这样的语言列表;

<string-array name="languages">
    <item>English</item>
    <item>Finnish</item>
    <item>French</item>
    <item>German</item>
    <item>Slovakian</item>
    <item>Polish</item>
</string-array>

我想在应用程序启动时将默认设置为英语。当语言发生变化时,我希望在整个活动中运行相同的语言。在上面的链接中,在方法 public void setLocale(String localeCode)中,在我的情况下, localeCode 是什么。这里的区域设置代码是什么, Locale locale = new Locale(localeCode); 。我知道这里可能是 Locale.setDefault(Locale.ENGLISH); 。到目前为止,我已经做到了这一点,我知道这是垃圾,因为它无法正常工作。你能帮我一个正确的道路吗?提前致谢

public class Base_Activity extends Activity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    final Spinner spinner = (Spinner) menu.getItem(0).getActionView()
            .findViewById(R.id.spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.languages, R.layout.spinner_row);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        // private String currentLanguage;

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            // currentLanguage = spinner.getSelectedItem().toString();

            Locale locale = null;
            switch (arg2) {
            case 0:
                locale = new Locale("en_US");
                break;
            case 1:
                locale = new Locale("fi");
                break;
            case 2:
                locale = new Locale("fr");
                break;
            case 3:
                locale = new Locale("de");
                break;
            case 4:
                locale = new Locale("sk");

                break;
            case 5:
                locale = new Locale("pl");
                break;

            default:
                locale = new Locale("en_US");
                break;
            }
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());

            startActivity();

        }

        private void startActivity() {
            overridePendingTransition(0, 0);
            Log.i("DEBUG", "GATEWAY");

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Locale.setDefault(Locale.ENGLISH);

        }

    });
    return super.onCreateOptionsMenu(menu);
}}

2 个答案:

答案 0 :(得分:0)

在onITemSelectedListener

中尝试此操作
Locale locale = null ;

    switch (arg0) {
    case 0:
        locale = new Locale("en_US"); 
        break;
    case 1:
        locale = new Locale("fi"); 
        break;
    case 2:
        locale = new Locale("fr"); 
        break;
    case 3:
        locale = new Locale("de"); 
        break;
    case 4: 
        locale = new Locale("sk"); 

        break;
    case 5:
        locale = new Locale("pl"); 
        break;

    default:
        locale = new Locale("en_US"); 
        break;
    }

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    this.getApplicationContext().getResources().updateConfiguration(config, null);

答案 1 :(得分:0)

检查我的方式。  1.您必须在资源文件夹下为不同语言创建不同的“值”文件夹:         ie ... values - fr(for france),values-en(for engish)  2.现在将不同语言的字符串数据放在“string.xml”中,以用于相关值文件夹

  1. 然后根据本地语言将数据设置为视图。 当您更改语言时,请按如下方式刷新数据字段:

    gameTitle.setString(GameActivity.gameActivity.getResources()的getString(R.string.Jimmy_Jigsaw);

    希望它能帮到你