支持不同的语言

时间:2012-09-07 05:04:12

标签: android

我有textView和ListView。我在listview中列出了支持langugage的列表

当我点击任何语言时Textview文本需要更改

代码

   String lang[] = new String[]{"English","French"};
        ListView listView = (ListView) findViewById(android.R.id.list);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lang);

        listView.setAdapter(adapter);

        TextView text = (TextView)findViewById(R.id.textbox);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        TextView tv = (TextView)v;
        String selected_lang = tv.getText().toString();
        Toast.makeText(this, selected_lang, Toast.LENGTH_LONG).show();
    }

注意:   需要翻译从一种语言到另一种语言而不使用strings.xml和GoogleApi

有人知道Google如何将一种语言翻译成其他语言?

3 个答案:

答案 0 :(得分:3)

在您的目录附近创建新值目录值-fr,其中包含文件strings.xml和内容,如

<resources>
    <string name="some_string_vith_localization">French translation</string>
</resources>

并在您的代码中

String lang[] = new String[]{"en","fr"};
        ListView listView = (ListView) findViewById(R.id.my_list_id);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lang);

        listView.setAdapter(adapter);

        TextView text = (TextView)findViewById(R.id.textbox);

    }
            @Override
            protected void onListItemClick(ListView l, View v, int position, long id) {
                TextView tv = (TextView)view.findViewById(R.id.text_view_to_change);
                String selected_lang = tv.getText().toString();

                Locale locale2 = new Locale(selected_lang);
                Locale.setDefault(locale2);
                Configuration config2 = new Configuration();
                config2.locale = locale2;
                getBaseContext().getResources().updateConfiguration(config2,
                        getBaseContext().getResources().getDisplayMetrics());
                tv.setText(getResources().getString(R.string.some_string_vith_localization));

            }

答案 1 :(得分:1)

以下是您需要的Multiple Language Support in Android的完整示例。


现在,

因为需要了解的事情很少,

res/values文件夹,您可以创建尽可能多的文件夹,并将相应的语言字符串值保留在文件夹下的Strings.xml文件中。

现在,当用户选择其选择的任何语言时,请更改configuration

我还建议您在完成此主题后再进行一次:Localization

答案 2 :(得分:0)

好的...尝试使用android-translate-api-1.1.jar

链接:http://code.google.com/p/android-translate-api/

Locale curLocale = this.getResources().getConfiguration().locale;
I18nTranslator i18nTranslator = new I18nTranslator(curLocale .getLanguage());

String text = i18nTranslator.translateString("YOUR-TEXT-HERE");
相关问题